예제 #1
0
function startOfDayUpdate()
{
    // Update the highlights with start of day trigger
    highlights::eventTrigger(highlights::EVENT_START_DAY);
    // Make sure no expired users get emails.
    user::resetAllExpiredEmailUpdates();
    // Delete previously deleted watchlists
    watchlist::pruneDeletedWatchlistIds();
    watchlist::deleteZonedTickers(BREAKOUT_TRADE);
    watchlist::deleteZonedTickers(BREAKDOWN_TRADE);
    watchlist::deleteZonedTickers(LONG_TRADE);
    watchlist::deleteZonedTickers(SHORT_TRADE);
    watchlist::deleteZonedTickers(PULLBACK_TRADE);
    watchlist::deleteZonedTickers(BACKDRAFT_TRADE);
    $deleteIds = array();
    $deleteHoldingsIds = array();
    $holdings = new holdings();
    while ($holdings->loadNext()) {
        $hid = $holdings->get_variable("holdings_id");
        $tid = $holdings->get_variable("holdings_ticker_id");
        $action = $holdings->get_variable("holdings_last_action");
        $hidden = $holdings->get_variable("holdings_abandon_hide");
        $tradeType = $holdings->get_variable("holdings_tradetype");
        if (IsAbandoned($action) && $hidden == 0) {
            $deleteHoldingsIds[] = $hid;
            echo "\nDELETING Holdings ID " . $hid . " because it was abandoned";
            $watchlist = new watchlist();
            $watchlist->set_variable("watchlist_ticker_id", $tid);
            $watchlist->set_variable("watchlist_tradetype", $tradeType);
            while ($watchlist->loadNext()) {
                $deleteIds[] = $watchlist->get_variable("watchlist_id");
            }
        } else {
            if (($tradeType == REVERSAL_TRADE || $tradeType == SHORT_TRADE) && $hidden == 0) {
                $hitT3 = $holdings->get_variable("holdings_t3_marked");
                if ($hitT3 == 1) {
                    $deleteHoldingsIds[] = $hid;
                    echo "\nDELETING Holdings ID " . $hid . " because topped reversal/shorts";
                    $watchlist = new watchlist();
                    $watchlist->set_variable("watchlist_ticker_id", $tid);
                    $watchlist->set_variable("watchlist_tradetype", $tradeType);
                    while ($watchlist->loadNext()) {
                        $deleteIds[] = $watchlist->get_variable("watchlist_id");
                    }
                }
            } else {
                if (($tradeType == BACKDRAFT_TRADE || $tradeType == PULLBACK_TRADE) && $hidden == 0) {
                    $hitT1 = $holdings->get_variable("holdings_t1_marked");
                    if ($hitT1 == 1) {
                        $deleteHoldingsIds[] = $hid;
                        echo "\nDELETING Holdings ID " . $hid . " because topped pullback/backdraft";
                        $watchlist = new watchlist();
                        $watchlist->set_variable("watchlist_ticker_id", $tid);
                        $watchlist->set_variable("watchlist_tradetype", $tradeType);
                        while ($watchlist->loadNext()) {
                            $deleteIds[] = $watchlist->get_variable("watchlist_id");
                        }
                    }
                }
            }
        }
    }
    foreach ($deleteIds as $wid) {
        echo "\n\n\nDELETING Watchlist ID " . $wid . " because it is abandoned or topped out reversal/shorts";
        $watchlist = new watchlist();
        $watchlist->set_variable('watchlist_id', $wid);
        $watchlist->delete();
    }
    foreach ($deleteHoldingsIds as $hid) {
        echo "\n\n\nDELETING Holdings ID " . $hid . " because it is abandoned or topped out reversal/shorts";
        $holdings = new holdings();
        $holdings->set_variable('holdings_id', $hid);
        // instead of deleting, mark it as hidden
        if ($holdings->load()) {
            $tickerInfo = new ticker();
            $tickerInfo->set_variable('ticker_id', $holdings->get_variable('holdings_ticker_id'));
            if ($tickerInfo->load()) {
                $holdings->set_variable('holdings_ticker_symbol', $tickerInfo->get_variable('ticker_symbol'));
            }
            $holdings->set_variable('holdings_abandon_hide', 1);
            $holdings->set_variable('holdings_abandon_date', date('Y-m-d H:i:s'));
            $holdings->update();
        }
        // replace this
        //$holdings->delete();
    }
    // delete the tickers that are no longer used.
    $ticker = new ticker();
    $deleteIds = array();
    while ($ticker->loadNext()) {
        $tid = $ticker->get_variable("ticker_id");
        // reset the high and low
        $last = $ticker->get_variable("last");
        $ticker->set_variable("today_high", $last);
        $ticker->set_variable("today_low", $last);
        $ticker->set_variable("last_high", $last);
        $ticker->set_variable("last_low", $last);
        $ticker->update();
        $watchlist = new watchlist();
        $watchlist->set_variable("watchlist_ticker_id", $tid);
        if (!$watchlist->load()) {
            $holding = new holdings();
            $holding->set_variable("holdings_ticker_id", $tid);
            $holding->set_variable("holdings_abandon_hide", 0);
            if (!$holding->load()) {
                $deleteIds[] = $tid;
            }
        }
    }
    foreach ($deleteIds as $tid) {
        echo "\n\n\nDELETING TICKER ID " . $tid . " because it is no longer used";
        $ticker = new ticker();
        $ticker->set_variable('ticker_id', $tid);
        $ticker->delete();
    }
    $tickerHist = new ticker_history();
    while ($tickerHist->loadNextDistinctTicker()) {
        $ticker = new ticker();
        $tid = $tickerHist->get_variable('history_ticker_id');
        $ticker->set_variable('ticker_id', $tid);
        if (!$ticker->load()) {
            echo "\nTicker History Cleanup:: Removing " . $tid . "\n";
            ticker_history::removeTickerHistory($tid);
        }
    }
    // try to reconcile any payments that are not currently known.
    // now doing this whenever a payment is received.
    //payment_info::reconcileAllPaymentUids();
}
예제 #2
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);
             }
         }
     }
 }
예제 #3
0
function endOfDayUpdate()
{
    // Update the highlights with end of day trigger
    highlights::eventTrigger(highlights::EVENT_END_DAY);
    // Abandon the stocks that were marked abandon during the day
    abandonNowStocks();
    // update the close price of the ticker
    $ticker = new ticker();
    while ($ticker->loadNext()) {
        $last = $ticker->get_variable('last');
        $ticker->set_variable('last_close', $last);
        $ticker->update();
    }
    /// update the holdings
    $holdings = new holdings();
    while ($holdings->loadNextAll()) {
        $last_action = $holdings->get_variable('holdings_last_action');
        if (IsAbandoned($last_action)) {
            continue;
        }
        $last = floatval($holdings->get_variable('last'));
        $low = floatval($holdings->get_variable('today_low'));
        $t3 = floatval($holdings->get_variable('holdings_t3'));
        $tId = $holdings->get_variable('ticker_id');
        $symbol = $holdings->get_variable('ticker_symbol');
        $stopType = $holdings->get_variable('holdings_stop_type');
        $stopPrice = floatval($holdings->get_variable('holdings_stop_price'));
        $hId = $holdings->get_variable('holdings_id');
        $initSellPriceSet = $holdings->get_variable('holdings_init_sell_price_set');
        $isT1Marked = $holdings->get_variable('holdings_t1_marked');
        $tradeType = $holdings->get_variable("holdings_tradetype");
        echo "\n\n\nChecking " . $hId . " ticker " . $tId . " against \$" . $last;
        $action = NONE;
        switch ($tradeType) {
            case BREAKDOWN_TRADE:
            case SHORT_TRADE:
                $doAbandon = $last > $stopPrice;
                break;
            case BACKDRAFT_TRADE:
                $doAbandon = $last > $stopPrice && !$isT1Marked;
                break;
            case LONG_TRADE:
            case REVERSAL_TRADE:
            case BREAKOUT_TRADE:
                $doAbandon = $last < $stopPrice;
                break;
            case PULLBACK_TRADE:
                $doAbandon = $last < $stopPrice && !$isT1Marked;
                break;
        }
        if ($doAbandon) {
            $tweet = new tweet();
            $abandonEmail = new email(email::ADDRESSES_SMS_ONLY, $tradeType, $hId);
            $action = ABANDON;
            $tweet->newTweet($tradeType, $action, $symbol, $stopPrice);
            $abandonEmail->newEmail($tradeType, $action, $symbol, $stopPrice);
            $newHolding = new holdings();
            $newHolding->set_variable('holdings_id', $hId);
            $newHolding->set_variable('holdings_last_action', $action);
            $newHolding->set_variable('holdings_abandon_marked', 1);
            $newHolding->update();
            // Add a transaction to the transaction table
            holdings::CreateNewTransaction($hId, $tradeType, $stopPrice, $action);
        }
        if ($action != ABANDON && $last_action == WARNING) {
            // reset WARNING list
            $t1Marked = $holdings->get_variable('holdings_t1_marked');
            $t2Marked = $holdings->get_variable('holdings_t2_marked');
            $t3Marked = $holdings->get_variable('holdings_t3_marked');
            if ($t3Marked) {
                $next_action = SELL3;
            } else {
                if ($t2Marked) {
                    $next_action = SELL2;
                } else {
                    if ($t1Marked) {
                        $next_action = SELL1;
                    } else {
                        $next_action = BUY;
                    }
                }
            }
            $newHoldings = new holdings();
            $newHoldings->set_variable('holdings_id', $hId);
            $newHoldings->set_variable('holdings_last_action', $next_action);
            echo "\nREPLACE WARNING WITH LAST ACTION\n";
            echo $newHoldings->debug();
            $newHoldings->update();
        }
    }
    // end of day, check to see if we are out of zone
    /*
    	$watchlist = new watchlist();
    	$watchlist->set_variable('watchlist_is_zoned', 1);
    	$deleteIds = array();
    	while ($watchlist->loadNext()){
    		$tradeType = $watchlist->get_variable("watchlist_tradetype");
    		$ticker = new ticker();
    		$ticker->set_variable('ticker_id', $watchlist->get_variable('watchlist_ticker_id'));
    		if ($ticker->load()){
    			if ($tradeType==SHORT_TRADE){
    				$high = floatval($ticker->get_variable('today_high'));
    				$bottom = floatval($watchlist->get_variable('watchlist_bottom'));
    				if ($high < $bottom){
    					$watchlistId = floatval($watchlist->get_variable('watchlist_id'));
    					$deleteIds[] = $watchlistId;
    				}
    			}else if ($tradeType==LONG_TRADE){
    				$low = floatval($ticker->get_variable('today_low'));
    				$top = floatval($watchlist->get_variable('watchlist_top'));
    				if ($low > $top){
    					$watchlistId = floatval($watchlist->get_variable('watchlist_id'));
    					$deleteIds[] = $watchlistId;
    				}
    			}else if ($tradeType==REVERSAL_TRADE){
    				$low = floatval($ticker->get_variable('today_low'));
    				$top = floatval($watchlist->get_variable('watchlist_top'));
    				if ($low > $top){
    					$watchlistId = floatval($watchlist->get_variable('watchlist_id'));
    					$deleteIds[] = $watchlistId;
    				}
    			}
    		}
    	}
    	foreach ($deleteIds as $wId){
    		echo "\n\n\nDELETING WATCHLIST ID ". $wId. " because it was higher than top all day";
    		$watchlist = new watchlist();
    		$watchlist->set_variable('watchlist_id', $wId);
    		$watchlist->delete();
    	}
    * 
    */
}