#! /bin/bash
# Last edited on 2020-11-18 01:31:39 by jstolfi

# Plots some ref price and a polynomial fit to it
# 
# Arguments: {TITLE} {DMIN} {DMAX} {VMIN} {VMAX} {FNAME}
# 
# Where 
#   
#   {TITLE} is the plot's title;
#   {DMIN,DMAX} is the range of dates to plot (format "%Y-%m-%d");
#   {VMIN},{VMAX} is the range of prices for the vertical scale of the plot; 
#   {FNAME} is a data file with lines "{DATE} {REFPRICE} {FITPRICE}".
#   
asset="$1"; shift  # Asset name, "bitcoin" or "sucoasma"
title="$1"; shift  # Plot title.
dmin="$1"; shift   # Min date to plot.
dmax="$1"; shift   # Max date to plot.
vmin="$1"; shift   # Min price to plot.
vmax="$1"; shift   # Max price to plot.
width="$1"; shift  # Width of temp plot file in pixesls.

name="deg1-${asset}"
ffile="fit-${name}-fitted.txt"
pfile="fit-${name}-fitted.png"

show="YES";      # "YES" to display plot.

tmp="/tmp/$$"

tfile="${tmp}-full.png" # Temporary plot file (hi-res)

export GDFONTPATH=.:..

gnuplot <<EOF
set term png size ${width},2200 font "courbd,42"
set output "${tfile}"

tticdays = 50*7              # Number of days per time tic interval.
tticsecs = 60*60*24*tticdays # Number of seconds per time tic interval.
tmtics =   5                 # Number of minor time tic intervals per major time tic interval.

set xdata time
xmin = strptime("%Y-%m-%d","${dmin}")
xmax = strptime("%Y-%m-%d","${dmax}")

xminplt = xmin-24*3600
xmaxplt = xmax+24*3600
set xrange [(xminplt):(xmaxplt)]

# X-axis tics
unset xtics
unset mxtics 
set xtics ( 0 )
xtimefmt = "%Y-%m-%d"
mticsecs = tticsecs/tmtics
xtime = xminplt
k = 0
while (xtime <= xmaxplt) {
    minor = ((k % tmtics) != 0)
    set xtics rotate by -60 add ( strftime(xtimefmt, xtime) xtime minor )
    k = k + 1
    xtime = xtime + mticsecs
}
set xtics mirror

set title "${title}"

# Input time:
set timefmt "%Y-%m-%d %H:%M:%S"
tim(kcol) = (timecolumn(kcol))

# Price field, adjusted by given rate:
pmd(kpmd) = (column(kpmd) == 0 ? 0/0 : column(kpmd))

vmin = ${vmin}
vmax = ${vmax}

set yrange [vmin:vmax]
set logscale y

if (vmax < 100000) {
set ytics ( \
                                0.050,    0.070,    \
  0.10,     0.20,     0.30,     0.50,     0.70,     \
  1,        2,        3,        5,        7,        \
  10,       20,       30,       50,       70,       \
  100,      200,      300,      500,      700,      \
  1000,     2000,     3000,     5000,     7000,     \
  10000,    20000,    30000,    50000,    70000,    \
  100000,   200000,   300000,   500000,   700000,   \
  1000000,  2000000,  3000000,  5000000,  7000000   \
  10000000, 20000000, 30000000, 50000000, 70000000   \
)
} else {
set ytics ( \
                                  0.050,         \
  0.10,           0.20,           0.50,          \
  1.0,            2.0,            5.0,           \
  10.0,           20.0,           50.0,          \
  100.0,          200.0,          500.0,         \
  1000.0,         2000.0,         5000.0,        \
  10000.0,        20000.0,        50000.0,       \
  100000.0,       200000.0,       500000.0,      \
  "1 M"   1000000.0,      "2 M"   2000000.0,      "5 M"   5000000.0,     \
  "10 M"  10000000.0,     "20 M"  20000000.0,     "50 M"  50000000.0,    \
  "100 M" 100000000.0,    "200 M" 200000000.0,    "500 M" 500000000.0,   \
  "1 B"   1000000000.0,   "2 B"   2000000000.0,   "5 B"   5000000000.0,  \
  "10 B"  10000000000.0,  "20 B"  20000000000.0,  "50 B"  50000000000.0,  \
  "100 B" 100000000000.0, "200 B" 200000000000.0, "500 B" 500000000000.0  \
)
}
set ytics format "%.2f"

set grid xtics  lt 1 lw 4 lc rgb '#ddddcc', lt 1 lw 2 lc rgb '#ddddcc'
set grid mxtics
set grid ytics  lt 1 lw 6 lc rgb '#ccccbb', lt 1 lw 2 lc rgb '#ccccbb'

set xzeroaxis lt 1 lw 6 lc rgb '#ccccbb'

set key left top

plot \
  "${ffile}" using (tim(1)):(pmd(3)) title "fitted" with lines lt 1 lw 6.0 lc rgb '#ff2200', \
  "${ffile}" using (tim(1)):(pmd(2)) title "actual" with lines lt 1 lw 4.0 lc rgb '#2244ff'
quit
EOF

if [[ -s ${tfile} ]]; then 
  convert ${tfile} -resize '50%' ${pfile}
  if [[ "/${show}" == "/YES" ]]; then display ${pfile}; fi
fi

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