function processBreakouts()
{
    $holding = new holdings();
    $holding->set_variable("holdings_abandon_hide", 0);
    $holding->set_variable("holdings_tradetype", BREAKOUT_TRADE);
    $symbols = array();
    $hids = array();
    while ($holding->loadNext()) {
        $hId = $holding->get_variable("holdings_id");
        $tId = $holding->get_variable("holdings_ticker_id");
        $abandon = $holding->get_variable("holdings_stop_price");
        $ticker = new ticker();
        $ticker->set_variable("ticker_id", $tId);
        if ($ticker->load()) {
            $symbol = strtoupper($ticker->get_variable("ticker_symbol"));
            echo "Symbol: " . $symbol . "\n";
            $weeksAgo = strtotime("-32 days");
            $historicalData = finance::retrieveHistorical($symbol, date("Ymd", $weeksAgo), date("Ymd"), 'daily');
            $ATRCount = $ATRSum = 0;
            $count = sizeof($historicalData);
            // get the last 19 days of information for the average and add today on the end
            $i = min($count - 1, 19);
            for ($j = $i - 1; $j >= 0; $j--) {
                $today = $historicalData[$j]['date'];
                $yesterdaysClose = $historicalData[$i]['close'];
                $todayHigh = $historicalData[$j]['high'];
                $todayLow = $historicalData[$j]['low'];
                $todayClose = $historicalData[$j]['close'];
                echo "TEST - DATE=" . $today . " PrevDayClose=" . $yesterdaysClose . " DayHigh=" . $todayHigh . " DayLow=" . $todayLow;
                $finalATRVal = GetATR($yesterdaysClose, $todayHigh, $todayLow);
                $ATRSum += $finalATRVal;
                $ATRCount++;
                $i = $j;
                echo " FINAL=" . $finalATRVal . "\n";
            }
            // ADD Today
            $yesterdaysClose = $todayClose;
            $todayHigh = $ticker->get_variable("today_high");
            $todayLow = $ticker->get_variable("today_low");
            $todayClose = $ticker->get_variable("last_close");
            echo "TEST - DATE=TODAY PrevDayClose=" . $yesterdaysClose . " DayHigh=" . $todayHigh . " DayLow=" . $todayLow;
            $finalATRVal = GetATR($yesterdaysClose, $todayHigh, $todayLow);
            echo "   FINAL=" . $finalATRVal . "\n";
            $ATRSum += $finalATRVal;
            $ATRCount++;
            $ATRAvg = round($ATRSum / $ATRCount, 2);
            echo $symbol . " ATR=" . $ATRAvg . " todayClose=" . $todayClose . "\n";
            $newAbandon = round($todayClose - 2 * $ATRAvg, 2);
            echo "Comparing Abandons: Old=" . $abandon . "   New=" . $newAbandon . "\n";
            if ($newAbandon > $abandon) {
                echo "NEW ABANDON PRICE SET TO " . $newAbandon . "\n";
                $holding->set_variable("holdings_stop_price", $newAbandon);
                $holding->update();
                // highlight the abandon price
                highlights::holdingsHighlight($hId, H_ABANDON, 0, highlights::EVENT_START_DAY);
            }
            echo "\n\n\n\n";
        }
    }
}
예제 #2
0
 private function updateHoldings()
 {
     $holdings = new holdings();
     $now = date('Y-m-d H:i:s');
     while ($holdings->loadNextAll()) {
         $last_action = $holdings->get_variable('holdings_last_action');
         $action = $last_action;
         $last = floatval($holdings->get_variable('last'));
         $high = floatval($holdings->get_variable('last_high'));
         $low = floatval($holdings->get_variable('last_low'));
         // check to make sure we didn't just zone for testing the ticker information
         $origDate = $holdings->get_variable('holdings_orig_date');
         $diff = (strtotime($now) - (strtotime($origDate) + 60 * updater::HIGH_MINUTE_WINDOW)) / 60;
         if ($diff < 0) {
             $high = $last;
         }
         if ($high <= 0) {
             $high = $last;
         }
         if ($low <= 0) {
             $low = $last;
         }
         $symbol = $holdings->get_variable('ticker_symbol');
         $tId = $holdings->get_variable('ticker_id');
         //$t0 = floatval($holdings->get_variable('holdings_t0'));
         $t1 = floatval($holdings->get_variable('holdings_t1'));
         $t2 = floatval($holdings->get_variable('holdings_t2'));
         $t3 = floatval($holdings->get_variable('holdings_t3'));
         $t1Marked = $holdings->get_variable('holdings_t1_marked');
         $t2Marked = $holdings->get_variable('holdings_t2_marked');
         $t3Marked = $holdings->get_variable('holdings_t3_marked');
         $stopType = $holdings->get_variable('holdings_stop_type');
         $stopPrice = $holdings->get_variable('holdings_stop_price');
         $topPrice = $holdings->get_variable('holdings_top_price');
         $origPrice = $holdings->get_variable('holdings_orig_price');
         $hId = $holdings->get_variable('holdings_id');
         $tradeType = $holdings->get_variable('holdings_tradetype');
         $newHolding = new holdings();
         if ($tradeType == SHORT_TRADE) {
             if (!$t1Marked && ($last <= $t1 || $low <= $t1)) {
                 //"SELL #1";
                 echo "\n\n\nSHORT BUY 1 and send tweet!";
                 $action = SELL1;
                 $actionPrice = $t1;
                 $newHolding->set_variable('holdings_t1_marked', 1);
                 // update sell price of all holdings with this ticker_id
                 watchlist::removeTickerHitT1($tId);
                 //holdings::updateTickerSellPrices($tId, $origPrice);
                 highlights::holdingsHighlight($hId, H_T1, 0, highlights::EVENT_START_DAY);
             } else {
                 if (!$t2Marked && ($last <= $t2 || $low <= $t2)) {
                     //"SELL #2";
                     echo "\n\n\nSHORT BUY2 and send tweet!";
                     $action = SELL2;
                     $actionPrice = $t2;
                     $newHolding->set_variable('holdings_t2_marked', 1);
                     holdings::updateTickerSellPrices($tId, $t1);
                     highlights::holdingsHighlight($hId, H_T2, 0, highlights::EVENT_START_DAY);
                 } else {
                     if (!$t3Marked && ($last <= $t3 || $low <= $t3)) {
                         //"SELL #3";
                         echo "\n\n\nSELL 3 and send tweet!";
                         $actionPrice = $t3;
                         $action = SELL3;
                         $newHolding->set_variable('holdings_t3_marked', 1);
                         holdings::updateTickerSellPrices($tId, $t3);
                         highlights::holdingsHighlight($hId, H_T3, 0, highlights::EVENT_START_DAY);
                     } else {
                         if (!$t3Marked && $last >= $stopPrice) {
                             if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                 //"WARNING";
                                 echo "\n\n\nWARNING and send tweet!";
                                 $action = WARNING;
                                 $actionPrice = $stopPrice;
                             }
                         }
                     }
                 }
             }
             if ($action != $last_action) {
                 $newHolding->set_variable('holdings_id', $hId);
                 $newHolding->set_variable('holdings_last_action', $action);
                 $newHolding->update();
                 // Add a transaction to the transaction table
                 holdings::CreateNewTransaction($hId, $tradeType, $actionPrice, $action);
             }
         } else {
             if ($tradeType == LONG_TRADE) {
                 if (!$t1Marked && ($last >= $t1 || $high >= $t1)) {
                     //"SELL #1";
                     echo "\n\n\nSELL 1 and send tweet!";
                     $action = SELL1;
                     $actionPrice = $t1;
                     $newHolding->set_variable('holdings_t1_marked', 1);
                     $newHolding->set_variable('holdings_stop_price', $origPrice);
                     // update sell price of all holdings with this ticker_id
                     watchlist::removeTickerHitT1($tId);
                     //holdings::updateTickerSellPrices($tId, $origPrice);
                     highlights::holdingsHighlight($hId, H_T1, 0, highlights::EVENT_START_DAY);
                 } else {
                     if (!$t2Marked && ($last >= $t2 || $high >= $t2)) {
                         //"SELL #2";
                         echo "\n\n\nSELL 2 and send tweet!";
                         $action = SELL2;
                         $actionPrice = $t2;
                         $newHolding->set_variable('holdings_t2_marked', 1);
                         holdings::updateTickerSellPrices($tId, $t1);
                         highlights::holdingsHighlight($hId, H_T2, 0, highlights::EVENT_START_DAY);
                     } else {
                         if (!$t3Marked && ($last >= $t3 || $high >= $t3)) {
                             //"SELL #3";
                             echo "\n\n\nSELL 3 and send tweet!";
                             $action = SELL3;
                             $actionPrice = $t3;
                             $newHolding->set_variable('holdings_t3_marked', 1);
                             holdings::updateTickerSellPrices($tId, $t2);
                             highlights::holdingsHighlight($hId, H_T3, 0, highlights::EVENT_START_DAY);
                         } else {
                             if ($last <= $stopPrice) {
                                 if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                     //"WARNING";
                                     echo "\n\n\nWARNING and send tweet!";
                                     $action = WARNING;
                                     $actionPrice = $stopPrice;
                                 }
                             }
                         }
                     }
                 }
                 if ($action != $last_action) {
                     $newHolding->set_variable('holdings_id', $hId);
                     $newHolding->set_variable('holdings_last_action', $action);
                     $newHolding->update();
                     // Add a transaction to the transaction table
                     holdings::CreateNewTransaction($hId, $tradeType, $actionPrice, $action);
                 }
             } else {
                 if ($tradeType == REVERSAL_TRADE) {
                 } else {
                     $abandonPrice = $stopPrice;
                     $hardStopPrice = $topPrice;
                     switch ($tradeType) {
                         case BREAKOUT_TRADE:
                             if (!$t3Marked && ($last >= $t3 || $high >= $t3)) {
                                 $holdings->markTarget(3, $symbol, $last, true, $t3);
                             } else {
                                 if ($last <= $hardStopPrice) {
                                     holdings::abandonHardStop($hId, "");
                                 } else {
                                     if ($last <= $abandonPrice) {
                                         holdings::abandonPriceMet($hId, $abandonPrice);
                                         // if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                         // //"WARNING";
                                         // echo "\n\n\nWARNING and send tweet!";
                                         // $action = WARNING;
                                         // $actionPrice = $abandonPrice;
                                         // $newHolding -> set_variable('holdings_id', $hId);
                                         // $newHolding -> set_variable('holdings_last_action', $action);
                                         // $newHolding -> update();
                                         //
                                         // holdings::CreateNewTransaction($hId, $tradeType, $abandonPrice, $action);
                                         // }
                                     }
                                 }
                             }
                             break;
                         case PULLBACK_TRADE:
                             if (!$t1Marked && ($last >= $t1 || $high >= $t1)) {
                                 $holdings->markTarget(1, $symbol, $last, true, $t1);
                             } else {
                                 if (!$t1Marked && $last <= $abandonPrice) {
                                     holdings::abandonPriceMet($hId, $abandonPrice);
                                     // if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                     // //"WARNING";
                                     // echo "\n\n\nWARNING and send tweet!";
                                     // $action = WARNING;
                                     // $actionPrice = $abandonPrice;
                                     // $newHolding -> set_variable('holdings_id', $hId);
                                     // $newHolding -> set_variable('holdings_last_action', $action);
                                     // $newHolding -> update();
                                     //
                                     // holdings::CreateNewTransaction($hId, $tradeType, $abandonPrice, $action);
                                     // }
                                 }
                             }
                             break;
                         case BACKDRAFT_TRADE:
                             if (!$t1Marked && ($last <= $t1 || $low <= $t1)) {
                                 $holdings->markTarget(1, $symbol, $last, true, $t1);
                             } else {
                                 if (!$t1Marked && $last >= $abandonPrice) {
                                     holdings::abandonPriceMet($hId, $abandonPrice);
                                     // if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                     // //"WARNING";
                                     // echo "\n\n\nWARNING and send tweet!";
                                     // $action = WARNING;
                                     // $actionPrice = $abandonPrice;
                                     // $newHolding -> set_variable('holdings_id', $hId);
                                     // $newHolding -> set_variable('holdings_last_action', $action);
                                     // $newHolding -> update();
                                     //
                                     // holdings::CreateNewTransaction($hId, $tradeType, $abandonPrice, $action);
                                     // }
                                 }
                             }
                             break;
                         case BREAKDOWN_TRADE:
                             if (!$t3Marked && ($last <= $t3 || $high <= $t3)) {
                                 $holdings->markTarget(3, $symbol, $last, true, $t3);
                             } else {
                                 if ($last >= $hardStopPrice) {
                                     holdings::abandonHardStop($hId, "");
                                 } else {
                                     if ($last >= $abandonPrice) {
                                         holdings::abandonPriceMet($hId, $abandonPrice);
                                         // if ($last_action != WARNING && !IsAbandoned($last_action)) {
                                         // //"WARNING";
                                         // echo "\n\n\nWARNING and send tweet!";
                                         // $action = WARNING;
                                         // $actionPrice = $abandonPrice;
                                         // $newHolding -> set_variable('holdings_id', $hId);
                                         // $newHolding -> set_variable('holdings_last_action', $action);
                                         // $newHolding -> update();
                                         //
                                         // holdings::CreateNewTransaction($hId, $tradeType, $abandonPrice, $action);
                                         // }
                                     }
                                 }
                             }
                             break;
                     }
                 }
             }
         }
         // OBSOLETE WITH THE NEW METHODS
         if ($action != $last_action) {
             $tweet = new tweet();
             $updateEmail = new email(email::ADDRESSES_ALL_CHOSEN, $tradeType, $hId);
             $tweet->newTweet($tradeType, $action, $symbol, $actionPrice);
             $updateEmail->newEmail($tradeType, $action, $symbol, $actionPrice);
         }
     }
 }
예제 #3
0
     }
     $holdingItem->set_variable('holdings_tooltip', $_POST['tip']);
     $holdingItem->set_variable('holdings_orig_price', floatval($_POST['origPrice']));
     // Check if we have to update the abandon price and add highlight
     $origAbandon = $holdingItem->get_variable('holdings_stop_price');
     $newAbandon = floatval($_POST['abandonPrice']);
     if ($origAbandon != $newAbandon) {
         $holdingItem->set_variable('holdings_stop_price', $newAbandon);
         highlights::holdingsHighlight($holdingId, H_ABANDON, 0, highlights::EVENT_START_DAY);
     }
     // Check if we have to update the abandon price and add highlight
     $origHardStop = $holdingItem->get_variable('holdings_top_price');
     $newHardStop = floatval($_POST['hardStopPrice']);
     if ($origHardStop != $newHardStop) {
         $holdingItem->set_variable('holdings_top_price', $newHardStop);
         highlights::holdingsHighlight($holdingId, H_HARDSTOP, 0, highlights::EVENT_START_DAY);
     }
     $holdingItem->set_variable('holdings_orig_date', $_POST['origdate']);
     if ($abandonDate != null) {
         $holdingItem->set_variable('holdings_abandon_date', $_POST['abandondate']);
     }
     $holdingItem->update();
 }
 $origDate = strtotime($holdingItem->get_variable('holdings_orig_date'));
 $action = $holdingItem->get_variable('holdings_last_action');
 $t0 = $holdingItem->get_variable('holdings_t0');
 $t1 = $holdingItem->get_variable('holdings_t1');
 $t2 = $holdingItem->get_variable('holdings_t2');
 $t3 = $holdingItem->get_variable('holdings_t3');
 $hitT0 = $holdingItem->get_variable('holdings_t0_marked');
 $hitT1 = $holdingItem->get_variable('holdings_t1_marked');
예제 #4
0
 public static function abandonHardStop($holding_id, $message = "")
 {
     $holding = new holdings();
     $holding->set_variable('holdings_id', $holding_id);
     if ($holding->load()) {
         $symbol = $holding->get_variable('holdings_ticker_symbol');
         $tId = $holding->get_variable('holdings_ticker_id');
         $tradeType = $holding->get_variable("holdings_tradetype");
         $stopPrice = floatval($holding->get_variable('holdings_top_price'));
         $action = $holding->get_variable("holdings_last_action");
         if (IsAbandoned($action)) {
             return;
         }
         $tweet = new tweet();
         $abandonEmail = new email(email::ADDRESSES_ALL_CHOSEN, $tradeType, $hId);
         if (strlen($symbol) <= 0) {
             $ticker = new ticker();
             $ticker->set_variable("ticker_id", $tId);
             if ($ticker->load()) {
                 $symbol = $ticker->get_variable("ticker_symbol");
                 $last = $ticker->get_variable('last');
             }
         }
         //"ABANDON";
         $action = ABANDON_HARD_STOP;
         $tweet->newTweet($tradeType, $action, $symbol, $stopPrice, $message);
         $abandonEmail->newEmail($tradeType, $action, $symbol, $stopPrice, $message);
         $holding->set_variable('holdings_last_action', $action);
         $holding->set_variable('holdings_stop_price', $stopPrice);
         $holding->update();
         highlights::holdingsHighlight($hId, H_LAST_ACTION, 0, highlights::EVENT_END_DAY);
         // Add a transaction to the transaction table
         $transactions = new transactions();
         $transactions->set_variable('transaction_holdings_id', $holding_id);
         $transactions->set_variable('transaction_price', $stopPrice);
         $transactions->set_variable('transaction_date', date('Y-m-d H:i:s'));
         $transactions->set_variable('transaction_action', ABANDON_HARD_STOP);
         $transactions->set_variable('transaction_tradetype', $tradeType);
         $transactions->createNew();
     }
 }