#! /bin/bash
# Last edited on 2014-01-31 01:46:38 by stolfilocal

# Plots the prices of one or more exchanges.
# 
# Arguments: {TITLE} {TSTEP} {TOFF} {MXTICS} {VMIN} {VMAX} \
#    {FNAM[0]} {LABEL[0]} {RATE[0]} \
#    {FNAM[1]} {LABEL[1]} {RATE[1]} \
#    ... \
#    {FNAM[N-1]} {LABEL[N-1]} {RATE[N-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; 
#   {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.
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

show="SHOW"

tmp="/tmp/$$"

gplfile="${tmp}.gpl"

# Create the plot command
printf "plot" > ${gplfile}
sep=""

color=( '#0022ff' '#ff0000' '#008800' '#8800dd' '#dd4400' '#0066ff' )
i=0;
icolor=0
while [[ $i -lt $nfiles ]]; do
  fnamei=${fname[$i]}
  ratei=${rate[$i]}
  titlei="${label[$i]} price/${ratei}"
  printf "%s "'\\'"\n" "${sep}" >> ${gplfile}
  printf "  \"${fnamei}\" using (tim(0)):(usd(16,${ratei})) title \"${titlei}\"" >> ${gplfile}
  printf " with linespoints pt 7 ps 0.75 lt 1 lw 1.0 lc rgb '${color[$icolor]}'" >> ${gplfile}
  sep=","
  i=$(( $i + 1 ))
  icolor=$(( $icolor + 1 ))
  if [[ $icolor -ge ${#color[@]} ]]; then icolor=0; fi
done
printf "\n" >> ${gplfile}

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size 2000,1000 font "courbd,24"
set output "${tmp}-full.png"
tstep=${tstep}
set title "${title}"
toff=${toff}
tim(dummy) = (column(0)*tstep+toff)
usd(k,rate) = (column(k) == 0 ? 0/0 : column(k)/rate)
vmin = ${vmin}
vmax = ${vmax}
set yrange [vmin:vmax]
set logscale y

if (vmax/vmin > 10) {
set ytics ( \
  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 \
)
} else {
set ytics ( \
  10,   11,   12,   15,   20,   25,   30,   35,   40,   45,   50,   55,   60,   65,   70,   75,   80,   85,   90,   95,  \
  100,  110,  120,  150,  200,  250,  300,  350,  400,  450,  500,  550,  600,  650,  700,  750,  800,  850,  900,  950, \
  1000, 1100, 1200, 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 xzeroaxis lt 1 lw 3 lc rgb '#ffddaa'
set key center top

load "${gplfile}"
quit
EOF

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

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

cat ${plotfile}

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