#! /bin/bash
# Last edited on 2017-05-14 22:43:36 by stolfilocal

# Plots the ratio of prices at two exchanges.
# 
# Arguments: {TITLE} {TSTEP} {TOFF} {MXTICS} \
#    {DMIN} {TMIN} {DMAX} {TMAX} \
#    {VMIN} {VMAX} \
#    {FNAM[0]} {LABEL[0]} {RATE[0]} \
#    {FNAM[1]} {LABEL[1]} {RATE[1]}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {TSTEP} is the time step in minutes, hours, or days;
#   {TOFF} is an offset to be subtracted from the time axis;
#   {MXTICS} is the number of X grid subintervals for each grid interval; 
#   {DMIN},{TMIN} are the first date and time to consider ("%Y-%m-%d %H:%M:%S"),
#   {DMAX},{TMAX} are the last date and time to consider ("%Y-%m-%d %H:%M:%S"),
#   {VMIN},{VMAX} is the range of prices for the vertical scale of the plot; 
#   {FNAM[i]} is a data file with summary trade data (open, high, low etc.); 
#   {LABEL[i]} is the label to show on the plot's key; 
#   {RATE[i]} is a factor to be divided into the price as read from the file.
#   
# The horizontal axis will show {(J*TSTEP)-TOFF} where {J} is the 
# data line number (from 1).
#
# Currently uses the weighted mean price column.  !!! Should be a parameter. !!!

title="$1"; shift # Plot title.
tstep="$1"; shift # Time step.
toff="$1"; shift # Time offset.
mxtics="$1"; shift # Number of minor tic intervals per major tic interval.
dmin="$1"; shift # Min date to plot.
tmin="$1"; shift # Min time to plot on date {dmin}.
dmax="$1"; shift # Max date to plot.
tmax="$1"; shift # Max time to plot on date {dmax}.
vmin="$1"; shift # Min price for plot range.
vmax="$1"; shift # Max price for plot range.
fname=()
label=()
rate=()
i=0;
while [[ $# -gt 1 ]]; do
  fname[$i]="$1"; shift # File name.
  label[$i]="$1"; shift # Plot label.
  rate[$i]="$1"; shift # Currency conversion factor.
  i=$(( $i + 1 ))
done
nfiles=$i
if [[ ${nfiles} -ne 2 ]]; then
  echo "** must specify exactly two files" 1>&2; exit 1
fi

show="SHOW"

tmp="/tmp/$$"

# Merge the two files and compute the ratio:
for i in 0 1 ; do 
  cat ${fname[${i}]} \
   | egrep -e '[|]' \
   | gawk \
       -v dtmin="${dmin} ${tmin}" \
       -v dtmax="${dmax} ${tmax}" \
       ' // { dt=($1 " " $2); if ((dt >= dtmin) && (dt <= dtmax)) { print; }} ' \
   | sed -e 's:\([0-9]\) \([0-9]\):\1-\2:' \
   | sort \
   >  ${tmp}-${i}.txt
done
join -1 1 -2 1 -a 1 -a 2 -e '???' -o 0,1.15,2.15 ${tmp}-0.txt ${tmp}-1.txt > ${tmp}-01.txt
nbugs=`grep -e '[?][?][?]' ${tmp}-01.txt | wc -l`
if [[ ${nbugs} -gt 0 ]]; then
  echo "** failed to pair ${nbugs} lines" 1>&2; exit 1
fi

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2400,800 font "courbd,24"
set output "${tmp}-full.png"
tstep=${tstep}
set title "${title}"
toff=${toff}
tim(dummy) = (column(0)*tstep+toff)
vratio(i,j) = ((column(i) == 0) || (column(j) == 0) ? 0/0 : column(i)/column(j))
vmin = ${vmin}
vmax = ${vmax}

ratit = "${label[0]}/${label[1]}"
set yrange [vmin:vmax]
set logscale y

if (vmax/vmin > 10) {
  set ytics ( \
    1.0,  1.2,  1.5,  2.0,  2.5,  3.0,  4.0,  5.0,  6.0,  7.0,  8.0,  9.0,  \
    10,   12,   15,   20,   25,   30,   40,   50,   60,   70,   80,   90,  \
    100,  120,  150,  200,  250,  300,  400,  500,  600,  700,  800,  900, \
    1000, 1200, 1500, 2000, 2500, 3000, 4000, 5000, 6000, 7000, 8000, 9000 \
  )
}

if ((vmax/vmin > 2.0) && (vmax/vmin <= 10)) {
  set ytics ( \
    0.10, 0.11, 0.12, 0.13, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95,  \
    1.0,  1.1,  1.2,  1.3,  1.5,  2.0,  2.5,  3.0,  3.5,  4.0,  4.5,  5.0,  5.5,  6.0,  6.5,  7.0,  7.5,  8.0,  8.5,  9.0,  9.5,  \
    10,   11,   12,   13,   15,   20,   25,   30,   35,   40,   45,   50,   55,   60,   65,   70,   75,   80,   85,   90,   95,  \
    100,  110,  120,  130,  150,  200,  250,  300,  350,  400,  450,  500,  550,  600,  650,  700,  750,  800,  850,  900,  950, \
    1000, 1100, 1200, 1300, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500 \
  )
}

if ((vmax/vmin > 1.01) && (vmax/vmin <= 2.0)) {
  set ytics ( \
    0.10, 0.105, 0.11, 0.115, 0.12, 0.125, 0.13, 0.14, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55, 0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95,  \
    1.0,  1.05,  1.1,  1.15,  1.2,  1.25,  1.3,  1.4,  1.5,  2.0,  2.5,  3.0,  3.5,  4.0,  4.5,  5.0,  5.5,  6.0,  6.5,  7.0,  7.5,  8.0,  8.5,  9.0,  9.5,  \
    10,   10.5,  11,   11.5,  12,   12.5,  13,   14,   15,   20,   25,   30,   35,   40,   45,   50,   55,   60,   65,   70,   75,   80,   85,   90,   95,  \
    100,  105,   110,  115,   120,  125,   130,  140,  150,  200,  250,  300,  350,  400,  450,  500,  550,  600,  650,  700,  750,  800,  850,  900,  950, \
    1000, 1050,  1100, 1150,  1200, 1250,  1300, 1400, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500, 9000, 9500 \
  )
}

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

set xtics 1.0
set mxtics ${mxtics}
set grid xtics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'
set grid mxtics
set key center top

plot \
  (1.0) notitle with lines lt 1 lw 3.0 lc rgb '#aabb88', \
  "${tmp}-01.txt" using (tim(0)):(vratio(2,3)) title ratit \
    with linespoints pt 7 ps 0.75 lt 1 lw 1.0 lc rgb '#0044dd'
    
quit
EOF

plotfile="${tmp}.png"
convert ${tmp}-full.png -resize '50%' ${plotfile}

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

cat ${plotfile}

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