#! /bin/bash
# Last edited on 2014-02-13 01:54:14 by stolfilocal

# 05 pixels = 01:00

function pscale(){
  # Prints to {stdout} the V-coordinate corresponing to a given price.
  # Plot scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  parg="$1"; shift # Given price.
  
  echo "enter pscale ${pmin} ${vbot} ${pmax} ${vtop} ${parg}" 1>&2 

  pdel=$(( ${pmax} - ${pmin} ));
  prnd=$(( ${pdel} / 2 ))
  
  varg=$(( ${vbot} - ( ( ( ${parg} - ${pmin} ) * ( ${vbot} - ${vtop} ) + ${prnd} ) / ${pdel} ) ))
  
  printf "%d\n" "${varg}"
  echo "exit pscale ${varg}" 1>&2 
}

function boxcom(){
  # Prints to {stdout} the draw commands for a prediction box.
  
  # Plot scaling parameters:
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  ttarg="$1"; shift # UTC time when predicion was to be fulfilled.
  htarg="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg="$1"; shift # Predicted price at ${ttarg}.
  perr="$1"; shift  # V-radius of confidence interval.

  echo "enter boxcom ${pmin} ${vbot} ${pmax} ${vtop} ${ttarg} ${htarg} ${ptarg} ${perr}" 1>&2 
  
  vtarg_top=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${ptarg} + ${perr} ))`
  vtarg_bot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} $(( ${ptarg} - ${perr} ))`
  htarg_lft=$(( ${htarg} - 3 ))
  htarg_rht=$(( ${htarg} + 3 ))
  
  vtarg_arr=$(( ${vtarg_top} - 40 ))
  vtarg_lab=$(( ${vtarg_top} - 100 ))

  printf "%s %s\n" "-color" "#0088ff"
  printf "%s %s\n" "-strokewidth" "2"
  printf "%s %s %s  %s %s  %s %s  %s %s\n" "-Q" \
    "${htarg_rht}" "${vtarg_top}" \
    "${htarg_rht}" "${vtarg_bot}" \
    "${htarg_lft}" "${vtarg_bot}" \
    "${htarg_lft}" "${vtarg_top}" 
  printf "%s %s\n" "-color" "#33ddff"
  printf "%s %s\n" "-strokewidth" "2"
  printf "%s %s %s  %s %s %s\n" "-L" "${htarg}" "${vtarg_arr}"  "${htarg}" "${vtarg_lab}"  "${ttarg}"
  echo "exit boxcom" 1>&2 
}     

function annot(){
  # Annotate one image.
  
  ex="$1"; shift    # Exchange code
  
  pmin="$1"; shift # Low reference price.
  vbot="$1"; shift # V-coordinate of the low reference price.
  pmax="$1"; shift # High reference price.
  vtop="$1"; shift # V-coordinate of the high reference price.
  
  tpred="$1"; shift # UTC time when last prediction was issued.
  hpred="$1"; shift # H-coordinate corresponding to ${tpred}.
  vpred="$1"; shift # V-coordinate just above plot at ${tprev}.
  
  ttarg1="$1"; shift # UTC time when prediction 1 was to be fulfilled.
  htarg1="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg1="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr1="$1"; shift  # V-radius of confidence interval.
  
  ttarg2="$1"; shift # UTC time when prediction 2 was to be fulfilled.
  htarg2="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg2="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr2="$1"; shift  # V-radius of confidence interval.
  
  ttarg3="$1"; shift # UTC time when prediction 3 was to be fulfilled.
  htarg3="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg3="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr3="$1"; shift  # V-radius of confidence interval.
  
  vvolb="$1"; shift  # V-coordinate of volume plot baseline.

  dots=( "$@" ) # H- an V-cordinates of "slumber points".

  img="2014-02-12-prediction-check-${ex}"
  
  vpred_lab=$(( ${vpred} - 160 ))
  
  boxcmds=( ) # Commands to draw prediction boxes.
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg1}" ${htarg1} ${ptarg1} ${perr1}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg2}" ${htarg2} ${ptarg2} ${perr2}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg3}" ${htarg3} ${ptarg3} ${perr3}` )
  
  echo "boxcmds=( " "${boxcmds[@]}" " )" 1>&2
  
  dotcmds=( "-color" "#ffaa0099" "-strokewidth" "20" ) # Commands to draw dots at the slumber points.
  
  echo "dotcmds=( " "${boxcmds[@]}" " )" 1>&2

  i=0
  while [[ ${i} -lt ${#dots[@]} ]]; do 
    hdot="${dots[${i}]}";
    i=$(( ${i} + 1 ))
    pdot="${dots[${i}]}";
    vdot=`pscale ${pmin} ${vbot} ${pmax} ${vtop} ${pdot}`
    i=$(( ${i} + 1 ))
    dotcmds+=( -S ${hdot} ${vdot}  -S ${hdot} ${vvolb} )
  done
  
  set echo
  
  annotate-pic.sh \
      ${img}.png 16 Black Yellow \
      -color '#ff44aa' -strokewidth 2 \
      -L ${hpred} ${vpred}  ${hpred} ${vpred_lab}  "${tpred}" \
      \
      "${boxcmds[@]}" \
      \
      "${dotcmds[@]}" \
      \
    > ${img}-annotated.png
  gimp ${img}-annotated.png
}

# echo `pscale 3700 573  4800 111  4400` 
# exit 0

# Bitstamp slumber dots are computed from Huobi's by the ratios
#   1 USD = 6.40 CNY for 2014-02-07 -- 2014-02-09
#   1 USD = 6.12 CNY for other days.
annot BSTP \
  540 575  780 115 \
  "02:40" 666 285 \
  "19:00" 388  700 10 \
  "19:00" 508  690 10 \
  "20:00" 753  640 15 \
  716 \
   28 781 \
  148 724 \
  268 712 \
  388 702 \
  508 667 \
  628 667 \
  748 655

# Bitstamp actual prices at the slumber times:
#    28 779 \
#   148 730 \
#   268 706 \
#   388 702 \
#   508 664 \
#   628 675--646 \
#   748 663
# 

annot HUBI \
  3700 573  4800 111 \
  "02:40" 666 340 \
  "19:00" 388  4510 20 \
  "19:00" 508  4450 25 \
  "20:00" 753  4075 30 \
  716 \
   28 4782 \
  148 4636 \
  268 4554 \
  388 4492 \
  508 4079 \
  628 4079 \
  748 4006

# Price range on 
# 4218--4060
