#! /bin/bash
# Last edited on 2014-03-09 02:54:33 by stolfilocal

# Plots transactions at one or more exchanges.
# 
# Arguments: {TITLE} {TOFF} {TTICS} {MINPRICE} {MAXPRICE} {REFVOL} \
#    {FNAM[0]} {LABEL[0]} {RATE[0]} \
#    {FNAM[1]} {LABEL[1]} {RATE[1]} \
#    ... \
#    {FNAM[N-1]} {LABEL[N-1]} {RATE[N-1]} {PTSIZE[N-1]}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {TOFF} is a timezone offset (in seconds) to add to the time in the file to get UTC time;
#   {TTICS} is the interval (in integer minutes) between labelled tics on time axis;
#   {MINPRICE},{MAXPRICE} is the range of prices for the vertical scale of the plot; 
#   {REFVOL} is the volume that should be plotted with the maximum dot size; 
#   {FNAM[i]} is a data file with transaction data; 
#   {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.
#  
# A transaction data file should have lines with the format
# "{DATE} {TIME} | {PRICE} | {BTCAMOUNT}"
#   
# The horizontal axis will show {the time converted to seconds UTC (approximately)

title="$1"; shift    # Plot title.
toff="$1"; shift     # Timezone offset to add to get UTC.
ttics="$1"; shift    # Time tics interval.
minprice="$1"; shift # Min price for plot range.
maxprice="$1"; shift # Max price for plot range.
refvol="$1"; shift   # Volume for point size scaling.
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(1)):(usd(4,${ratei})):(vpsz(6)) title \"${titlei}\"" >> ${gplfile}
  printf " with linespoints pt 7 ps variable 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 4800,1200 font "courbd,24"
set output "${tmp}-full.png"
set title "${title}"

toff = ${toff}
ttics = ${ttics}

tim(k) = (timecolumn(k) + toff)
usd(k,rate) = (column(k) == 0 ? 0/0 : column(k)/rate)
max(x,y) = (x >= y ? x : y)

# Converting volume to point size:
minpsz = 1.0       # Min point size.
refpsz = 5.0       # Max point size.
refvol = ${refvol} # Volume for ref point size.
vpsz(k) = max(minpsz, refpsz*sqrt(column(k)/refvol))
# minvol = 0.001   # Minimum volume
# vpsz(k) = minpsz + (refpsz-minpsz)*log10(max(minvol,column(k))/minvol)/log10(refvol/minvol)

set yrange [${minprice}:${maxprice}]
# set logscale y
# 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 mytics
set grid ytics lt 1 lw 3 lc rgb '#ffddaa', lt 1 lw 1.5 lc rgb '#ffddaa'

# Input time format:
set timefmt "%Y-%m-%d %H:%M:%S"

set xdata time
# Warning - placing parens around "ttics*60.0" confuses the parser!
set xtics ttics*60.0
set format x "%H:%M"
set mxtics (ttics == 1 ? 6 : ttics)
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}{-*,}.*
    
        
