#! /bin/bash
# Last edited on 2016-02-04 18:26:18 by stolfilocal

# Arguments:
#
#   {SHOW} {C} {K} {TSTEP}  {MINDATE} {MAXDATE}  {PRESDATE} {PRESVALUE}  {TITLE} {DPREF} {OPREF}
#
# where
#
#   {SHOW}      "SHOW" to display, "NOSHOW" not to.
#   {C}         the parameter for the future variance formula.
#   {K}         the number of standard deviations to use for the confidence range.
#   {TSTEP}     input sampling interval (seconds).
#   {MINDATE}   min date to plot.
#   {MAXDATE}   max date to plot.
#   {PRESDATE}  the date at which the prediction should start.
#   {PRESVALUE} the weighted mean price at {PRESDATE} to assume for the predicted lines.
#   {TITLE}     the plot title.
#   {DPREF}     the input file name prefix.
#   {OPREF}     the output file name prefix.
#
# All dates should be in the format "{YYYY}-{MM}-{DD} {HH}:{MM}:{SS}".
# 
# Reads trade summary data at equal intervals from "{DPREF}.txt" 
# Writes {OPREF}.png with the plot of that data, plus the predicted {PRESVALUE ± K*sigma(n)} interval
# as a function of the number {n} of elapsed intervals since {PRESDATE},
# assuming that {sigma(n) = sqrt(C*n)}. 

show=$1; shift
C="$1"; shift
K="$1"; shift
tstep="$1"; shift
mindate="$1"; shift
maxdate="$1"; shift
presdate="$1"; shift
presvalue="$1"; shift
title="$1"; shift
dpref="$1"; shift
opref="$1"; shift

ifile="${dpref}.txt"
if [[ ! ( -e ${ifile}) ]]; then
  echo "** no file \"${ifile}\"" 1>&2 ; exit 1
fi

tmp="/tmp/$$"

pngfile="${opref}.png"
rm -f ${pngfile}

export GDFONTPATH=.

gnuplot <<EOF
set term png size 2400,1800 font "courbd,24"
set output "${tmp}.png"
set title "Predicted price range from Brownian model - ${title}"

tformat = "%Y-%m-%d %H:%M:%S"
set timefmt "%Y-%m-%d %H:%M:%S"

C = ${C}
K = ${K}
tstep = 0.0 + ${tstep}
mintime = strptime(tformat, "${mindate}");
maxtime = strptime(tformat, "${maxdate}");
prestime = strptime(tformat, "${presdate}");
presvalue = ${presvalue}

predtitle = sprintf("predicted %.1f*sigma range", K)

price(k) = (column(k) <= 0.0 ? NaN : column(k))
e10(x) = exp(log(10)*x)
range(tt,m) = (tt < prestime - tstep ? NaN : presvalue*(tt < prestime ? 1.0 : e10(m*sqrt(C*(tt - prestime)/tstep))))
shoot(tt,m) = presvalue + 0.5*m*(tt - prestime)/tstep

# Input time format:

set samples 6000
set xrange [(mintime):(maxtime)]
set xdata time
set format x "%Y-%m-%d"

set xtics 4*7*24*60*60
set mxtics 4*7

set yrange [-0.001:]
set ytics 100
set mytics 1

set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

set key left

plot \
  (range(x,+K)) \
      title predtitle  with lines  lt 1 lw 2.0 lc rgb '#227700', \
  (range(x,-K)) \
      notitle          with lines  lt 1 lw 2.0 lc rgb '#227700', \
  (range(x,0)) \
      notitle          with lines  lt 1 lw 2.0 lc rgb '#aa2200', \
  "< grep -e '[|]' ${ifile}" using (timecolumn(1)):(price(16))  \
      title "actual price"     with linespoints pt 7 ps 0.8 lt 1 lw 1.0 lc rgb '#3300bb' \

EOF

if [[ -s ${tmp}.png ]]; then 
  convert ${tmp}.png -resize '50%' ${pngfile}

  if [[ "/${show}" == "/SHOW" ]]; then
    display ${pngfile}
  fi
fi

rm -fv ${tmp}{-*,}.*
