Exemplo n.º 1
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();
    	}
    * 
    */
}