Esempio n. 1
0
 public function updateHistoryAndBorders($useGoogle)
 {
     echo "\n\nUpdating History and highs and lows\n\n";
     if ($useGoogle) {
         $results = finance_google::retrieveHighsLowsPrice($this->tickerSymbols);
     }
     $results2 = finance::retrieveCurrentPrice($this->tickerSymbols, "gh");
     $date = date('Y-m-d H:i:s');
     $ticker = new ticker();
     foreach ($results2 as $key => $value) {
         $ticker->reset_query();
         $ticker->set_variable('ticker_symbol', $key);
         if ($ticker->load()) {
             $tickerClose = $ticker->get_variable('last_close');
             $tickerId = $ticker->get_variable('ticker_id');
             $last = $ticker->get_variable('last');
             $yahooArr = explode(",", $value);
             if ($yahooArr[0] == 0) {
                 $yahooArr[0] = $last;
             }
             if ($yahooArr[1] == 0) {
                 $yahooArr[1] = $last;
             }
             $lasthigh = floatval($ticker->get_variable('today_high'));
             // Yahoo is broken
             //$currenthigh = max($lasthigh, floatval ($yahooArr[1]));
             // TEST CODE
             echo "\nYAHOO HIGH: " . ($yahoohigh = max($lasthigh, floatval($yahooArr[1])));
             $currenthigh = $yahoohigh;
             // END TEST CODE
             $googleHigh = $last;
             if ($useGoogle) {
                 $googleHigh = floatval($results[$key]['high']);
                 if ($googleHigh > 0) {
                     echo "\n Updating Last High " . $key . " " . $googleHigh;
                     $ticker->set_variable('last_high', $googleHigh);
                     $ticker->update();
                     $currenthigh = max($currenthigh, $googleHigh);
                 }
             }
             $lastlow = $ticker->get_variable('today_low');
             // Yahoo is broken
             //$currentlow = min($lastlow, floatval ($yahooArr[0]));
             // TEST CODE
             $yahoolow = min($lastlow, floatval($yahooArr[0]));
             $yahoolow = $yahoolow == 0 ? $lastlow : $yahoolow;
             $currentlow = $yahoolow;
             // END TEST CODE
             $googleLow = $last;
             if ($useGoogle) {
                 $googleLow = floatval($results[$key]['low']);
                 if ($googleLow < 10000) {
                     if ($currentlow <= 0) {
                         $currentlow = $googleLow;
                         $ticker->set_variable('today_low', $currentlow);
                     }
                     $currentlow = min($googleLow, $currentlow);
                     $ticker->set_variable('last_low', $googleLow);
                     $ticker->update();
                     echo "\n Updating Last Low " . $key . " " . $googleLow . " And today low " . $currentlow;
                 }
             }
             $lastupdate = date('Y-m-d', strtotime($ticker->get_variable('last_highlow_update')));
             $today = date('Y-m-d');
             echo "\n\nTESTING HIGH and LOW of " . $key . " as " . $currenthigh . " : " . $currentlow . "\n";
             $doUpdate = false;
             if ($currenthigh != 0 && ($today != $lastupdate || $currenthigh > $lasthigh)) {
                 echo "\nset high " . $currenthigh;
                 $ticker->set_variable('yahoo_high', round($yahoohigh, 2));
                 $ticker->set_variable('today_high', round($currenthigh, 2));
                 $doUpdate = true;
             }
             if ($currentlow != 0 && ($today != $lastupdate || $currentlow < $lastlow)) {
                 echo "\nset low " . $currentlow;
                 $ticker->set_variable('yahoo_low', round($yahoolow, 2));
                 $ticker->set_variable('today_low', round($currentlow, 2));
                 $doUpdate = true;
             }
             if ($doUpdate) {
                 if ($today != $lastupdate) {
                     echo "\nset last ";
                     $ticker->set_variable('last_highlow_update', $today);
                 }
                 $ticker->update();
             }
             // add ticker history
             /*
             
             $tickerHistory = new ticker_history();
             $tickerHistory->set_variable('history_price', $last);
             $tickerHistory->set_variable('history_date', $date);
             $tickerHistory->set_variable('history_ticker_id', $tickerId);
             if ($googleHigh > 0)
             $tickerHistory->set_variable('history_lasthigh', $googleHigh);
             else
             $tickerHistory->set_variable('history_lasthigh', $last);
             
             if ($googleLow < 10000)
             $tickerHistory->set_variable('history_lastlow', $googleLow);
             else
             $tickerHistory->set_variable('history_lastlow', $last);
             $tickerHistory->addStochInfoAndCreate($last);
             *
             */
             //ticker_history::addStochInfoAndCreate($key, $tickerId);
             // remove oldest history
             $tickerHistory = new ticker_history();
             $tickerHistory->set_variable('history_ticker_id', $tickerId);
             $numHistory = $tickerHistory->countAll('', '', 'history_id');
             $tickerHistory->reset_query();
             $limit = NUM_HISTORY_TO_KEEP;
             if ($numHistory > $limit) {
                 $limit = $numHistory - $limit;
                 $tickerHistory->delete(" WHERE history_ticker_id='" . $tickerId . "' ORDER BY history_date ASC LIMIT " . $limit);
             }
         }
     }
 }