Example #1
0
 private function updateStochStateMachine($hId, $tId, $symbol, $last, $stoch_action, $action)
 {
     $new_stoch_action = $stoch_action;
     $func = "";
     if ($stoch_action != STOCH_STATE_SELL3) {
         echo "\nTESTING STOCH MACHINE " . $symbol;
         $tickerHistory = new ticker_history();
         $tickerHistory->set_variable('history_ticker_id', $tId);
         if ($tickerHistory->loadLatest()) {
             $slowk = $tickerHistory->get_variable('history_slowk');
             $slowd = $tickerHistory->get_variable('history_slowd');
             switch ($stoch_action) {
                 case STOCH_PENDING:
                     if ($action == BUY && $slowk <= 20) {
                         // TEST 1 - set when slow K is below 20
                         $new_stoch_action = STOCH_STATE_BUY_WAIT;
                         $func = stoch_state_buy_wait;
                     }
                     break;
                 case STOCH_STATE_BUY_WAIT:
                     if ($slowk >= 20) {
                         // BUY NOW
                         $new_stoch_action = STOCH_STATE_BUY;
                         $func = stoch_state_buy;
                     }
                     break;
                 case STOCH_STATE_BUY:
                     if ($slowk >= 80 && $action == SELL1) {
                         $new_stoch_action = STOCH_STATE_SELL1_WAIT;
                         $func = stoch_state_sell_wait;
                     }
                     break;
                 case STOCH_STATE_SELL1:
                     if ($slowk >= 80 && $action == SELL2) {
                         $new_stoch_action = STOCH_STATE_SELL2_WAIT;
                         $func = stoch_state_sell_wait;
                     }
                     break;
                 case STOCH_STATE_SELL2:
                     if ($slowk >= 80 && $action == SELL3) {
                         $new_stoch_action = STOCH_STATE_SELL3_WAIT;
                         $func = stoch_state_sell_wait;
                     }
                     break;
                 case STOCH_STATE_SELL1_WAIT:
                 case STOCH_STATE_SELL2_WAIT:
                 case STOCH_STATE_SELL3_WAIT:
                     if ($slowk >= 80) {
                         if ($stoch_action == STOCH_STATE_SELL1_WAIT && $action == SELL2) {
                             $new_stoch_action = STOCH_STATE_SELL2_WAIT;
                             $func = stoch_state_sell_wait;
                         }
                         if ($stoch_action == STOCH_STATE_SELL2_WAIT && $action == SELL3) {
                             $new_stoch_action = STOCH_STATE_SELL3_WAIT;
                             $func = stoch_state_sell_wait;
                         }
                     } else {
                         if ($slowk <= 80) {
                             if ($stoch_action == STOCH_STATE_SELL1_WAIT) {
                                 $new_stoch_action = STOCH_STATE_SELL1;
                                 $func = stoch_state_sell;
                             } else {
                                 if ($stoch_action == STOCH_STATE_SELL2_WAIT) {
                                     $new_stoch_action = STOCH_STATE_SELL2;
                                     $func = stoch_state_sell;
                                 } else {
                                     if ($stoch_action == STOCH_STATE_SELL3_WAIT) {
                                         $new_stoch_action = STOCH_STATE_SELL3;
                                         $func = stoch_state_sell;
                                     }
                                 }
                             }
                         }
                     }
                     break;
             }
         }
     }
     if ($new_stoch_action != $stoch_action) {
         echo "\n" . $symbol . " UPDATING STOCH STATE MACHINE FROM " . $stoch_action . " TO " . $new_stoch_action;
         $newHolding = new holdings();
         $newHolding->set_variable('holdings_id', $hId);
         $newHolding->set_variable('holdings_stoch_action', $new_stoch_action);
         $newHolding->update();
         $tweetprivate = new tweetprivate();
         $tweetprivate->{$func}($symbol, $last, $new_stoch_action);
     }
 }
 public static function addStochInfoAndCreate($ticker, $tid)
 {
     $parts = finance_google::retrieveSlowDAndParts($ticker);
     echo "\nUpdating history for " . $ticker . " TID:" . $tid;
     $tickerHistory = new ticker_history();
     $tickerHistory->set_variable('history_price', $parts['last']);
     $date = date("Y-m-d h:i:s", $parts['date']);
     $tickerHistory->set_variable('history_date', $date);
     $tickerHistory->set_variable('history_ticker_id', $tid);
     $tickerHistory->set_variable('history_lasthigh', $parts['high']);
     $tickerHistory->set_variable('history_lastlow', $parts['low']);
     $tickerHistory->set_variable('history_r1', $parts['r1']);
     $tickerHistory->set_variable('history_r2', $parts['r2']);
     $tickerHistory->set_variable('history_fastk', $parts['fastK']);
     $tickerHistory->set_variable('history_slowk', $parts['slowK']);
     $tickerHistory->set_variable('history_slowd', $parts['slowD']);
     $tickerHistory->createNew();
 }
Example #3
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();
}