#! /bin/bash
# Last edited on 2014-02-13 20:13:59 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 with label.
  
  # 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 # Label: UTC time when prediction was to be fulfilled, or "@" to omit the label.
  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 ))

  if [[ "/${ttarg}" != "/@" ]]; then 
    vtarg_arr=$(( ${vtarg_top} - 40 ))
    vtarg_lab=$(( ${vtarg_top} - 100 ))

    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}"
    tcolor="#0088ff"
  else
    tcolor="#aabbcc"
  fi
  
  printf "%s %s\n" "-color" "${tcolor}"
  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}" 
  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, or "@".
  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, or "@".
  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, or "@".
  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.
  
  ttarg4="$1"; shift # UTC time when prediction 4 was to be fulfilled, or "@".
  htarg4="$1"; shift # H-coordinate corresponding to ${ttarg}.
  ptarg4="$1"; shift # V-coordinate for predicted price at ${ttarg}.
  perr4="$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-13-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}` )
  boxcmds+=( `boxcom ${pmin} ${vbot} ${pmax} ${vtop} "${ttarg4}" ${htarg4} ${ptarg4} ${perr4}` )
  
  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` 

# 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 601  820 107 \
  "04:47" 2064 320 \
  "@"     1646  700 10 \
  "@"     1766  690 10 \
  "@"     2011  640 15 \
  "19:00" 2126  650 10 \
  756 \
    86   793 \
   206   789 \
   326   785 \
   446   792 \
   566   796 \
   686   807 \
   806   812 \
   926   800 \
  1046   803 \
  1166   799 \
  1286   781 \
  1406   724 \
  1526   711 \
  1646   701 \
  1766   666 \
  1886   676 \
  2006   654 \
  2126   633

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

annot HUBI \
  3500 618  5000 113 \
  "04:47" 2064 410 \
  "@"     1646  4510 20 \
  "@"     1766  4450 25 \
  "@"     2011  4075 30 \
  "19:00" 2126  3970 30 \
   756 \
    86 4856 \
   206 4829 \
   326 4809 \
   446 4851 \
   566 4873 \
   686 4939 \
   806 4973 \
   926 4898 \
  1046 4915 \
  1166 4891 \
  1286 4782 \
  1406 4637 \
  1526 4555 \
  1646 4492 \
  1766 4080 \
  1886 4139 \
  2006 4006 \
  2126 3876
