#!/usr/local/bin/tops -s /usr/local/tops/sys -u /opt/mytops/usr/

# File eod_elec, January 2008

# End of day collection of electronic market data
{
  Sun Sep 16 12:19:06 PDT 2012.  In July 2012 site barchart.com became
  unreliable, invalidating primary source bcDATA() used below.  At that
  time the script for electronic market data was switched to eod_ino.
  Lately barchart.com has become reliable, and this script is again 
  being used.

  Tue Jul  5 19:54:13 PDT 2011.  Upgrades to site tch make it no longer
  accessible.  Site bch was fixed to handle intraday data.

  Thu Sep  9 15:18:16 PDT 2010.  Trouble with bc doing LC.  Switched
  so tch is tried first and it worked.

  Note:
  When a word has trouble, as when the site changes its format, source 
  this file at the ready prompt to troubleshoot.
}
#-----------------------------------------------------------------------

   usrpath "mget.v" + source
   keys? not IF LOG-FILE set_sysout THEN

   ontheweb
   IF " eod_elec: skipping electronic end of day collection" . nl
      exit
   THEN

   usrpath "eod_params" + "host" param_find "HOST" book
  
   host HOST <> IF " eod_elec: only on " . HOST . nl exit THEN

   inline: eodECOLLECT ( --- hT) \ data from bch or ino or tch
    \ This word is based upon inoCOLLECT and bcEOD, files ino.v and 
    \ bch.v.

      [ 6 "KEEP" book ]

      tracklist "TRACK" book

      time push
      depth push
      "From script eod_elec"
      date neat

      "Electronic trading" 
      "Showing: Open High Low Settle Close Chg Vol OpenInt"

      " "
      TRACK rows 1st
      DO \ tries bch, then ino, then tch:

         no "bcPROCESS" "EOD" bank \ doing intraday, not end of day

         TRACK I quote bcDATA (hT) any?
         IF (hT) " bcDATA for " TRACK I quote + . nl

         ELSE \ Fri Nov 30 15:16:07 PST 2012.  Make another attempt
          \ with bcDATA:

            " eod_elec: idle and make a second bcDATA() attempt" . nl 
            2 idle 
            TRACK I quote bcDATA (hT) any?
            IF (hT) " bcDATA for " TRACK I quote + . nl (hT)

            ELSE
               no "inoDATA" "ALL_HOSTS" bank \ just do one ino host
               TRACK I quote inoEDATA any?
               IF " inoEDATA for " TRACK I quote + . nl (hT)

               ELSE \ Tue Jul  5 19:54:13 PDT 2011: this will fail
         \        TRACK I quote tcEDATA any?
         \        IF " tcEDATA for " TRACK I quote + . nl
         \        ELSE ""
         \        THEN
                  ""
               THEN
            THEN
         THEN (hT) any? 
         IF (hT) 1st over rows KEEP min items reach (hT)

            "-----" " 0 " strp

             this 8 ndx word drop
             numerate totals ontop @ 1 max int$ "V" book
             this 9 ndx word drop
             numerate totals ontop @ 1 max int$ "OI" book

            TRACK I quote spaced
            " VOL and OI totals: " + V + spaced OI + neat

            swap

         ELSE TRACK I quote spaced date + " no data" +
         THEN
         " "
      LOOP
      depth pull less pilen

      "ET: " time pull less 60 slash "%0.1f" format +
      " minutes" + pile

      "eodECOLLECT" naming
   end

   " eod_elec: begin " date + . nl

   eodECOLLECT any? not
   IF eodECOLLECT any? not
      IF " eod_elec: two attempts failed" ersys "" THEN
   THEN

   (hT) any?
   IF (hT) mpath
      time GMT>LA ctime 1st quote strchop
      sysdate drop intstr "E.dat" + + (hT qFile) 2dup save
      " eod_elec: E file saved " date + . nl

{     The following non-E file lets pit eod be run now (called pre-
      eod), before final pit data has been received (which will be 
      later, in about 1.25 hours).  

      After the final pit data is received, pit eod will be run again 
      after files are reverted to the way they were before pre-eod was
      run.  See word revert, file eod.v.
}
      (hT qFile) "E" "" strp save \ temp file for pit pre-eod
      " eod_elec: temp file for pit pre-eod saved " date + . nl
   ELSE
      " eod_elec: failed " date + . nl
   THEN

   exit
