$dateArray[$dateCount]['trade_date'] = $data[0]; $dateCount++; #print "trade date in array: $data[0] <br />"; } $count = 0; $minReturn = 100; $maxReturn = -100; for ($x = 0; $x < $dateCount - 1; $x++) { #while ($data = mysql_fetch_row($result)) { # $trade_date = $data[0]; $trade_date = $dateArray[$x]['trade_date']; $next_trade_date = $dateArray[$x + 1]['trade_date']; if ($showOutput) { print "trade date: {$trade_date} <br />"; } crsi_sell($trade_date, $portfolioID, $tranHistory, $exitCRSI, $stopLoss, $next_trade_date); crsi_buy($trade_date, $breakOutSignal, $ADX_filter, $breakOutOrderBy, $portfolioID, $dailyBuyList, $enterCRSI, $enterRange, $enterLimit, $tranHistory, $orderBy, $next_trade_date); $value = get_historical_turtle_portfolio_value($trade_date, $portfolioID); $preturn = ($value - $original_investment) / $original_investment * 100; $dollar_return = $value - $original_investment; if ($preturn > $maxReturn) { $maxReturn = $preturn; } if ($preturn < $minReturn) { $minReturn = $preturn; } $portfolioReturn[$count]['trade_date'] = $trade_date; $portfolioReturn[$count]['return'] = $preturn; $portfolioReturn[$count]['value'] = $value; $portfolioReturn[$count]['maxReturn'] = $maxReturn; $portfolioReturn[$count]['minReturn'] = $minReturn;
function simulate_range_trade($portfolioID, $start_date, $end_date, $enterCRSI, $enterRange, $enterLimit, $exitCRSI, $commission, $max_risk, $risk_factor, $stop_loss_multiplier, $orderBy) { global $original_investment; global $breakOutSignal; global $ADX_filter; global $breakOutSignal; global $breakOutOrderBy; global $simplePriceHistory; global $showOutput; global $risk_factor; global $max_risk; global $stop_loss_multiplier; global $tranHistArray; global $portfolio_table; $dailyBuyList = "crsi_daily_buy_list" . $portfolioID; ### drop recreate daily buy list table $query = "drop table if exists " . $dailyBuyList; $result = queryMysql($query); $create_sql = "CREATE TABLE {$dailyBuyList} ( "; $create_sql .= "portfolio_id int(11) DEFAULT NULL, "; $create_sql .= "rank int(11) NOT NULL DEFAULT '0', "; $create_sql .= "trade_date date DEFAULT NULL, "; $create_sql .= "symbol varchar(6) COLLATE latin1_german2_ci DEFAULT NULL, "; $create_sql .= "buy_price double DEFAULT NULL, "; $create_sql .= "UNIQUE KEY uniq_idx (portfolio_id,symbol), "; $create_sql .= "KEY pid_idex (portfolio_id) "; $create_sql .= ") ENGINE=MEMORY "; $result = queryMysql($create_sql); $tranHistory = "turtle_portfolio_transaction" . $portfolioID; reset_portfolio($portfolioID); prepare_transactions_table($portfolioID, $tranHistory, $portfolio_table); $query = "select trade_date from quotes_memory where symbol = 'AAPL' and trade_date >= '" . $start_date . "' and trade_date <= '" . $end_date . "' order by trade_date"; $result = queryMysql($query); $dateArray = array(); $dateCount = 0; while ($data = mysql_fetch_row($result)) { $dateArray[$dateCount]['trade_date'] = $data[0]; $dateCount++; } $count = 0; $minReturn = 100; $maxReturn = -100; $retArray = array(); for ($x = 0; $x < $dateCount - 1; $x++) { #while ($data = mysql_fetch_row($result)) { # $trade_date = $data[0]; $trade_date = $dateArray[$x]['trade_date']; $next_trade_date = $dateArray[$x + 1]['trade_date']; if ($showOutput) { print "trade date: {$trade_date} <br />"; } crsi_sell($trade_date, $portfolioID, $tranHistory, $exitCRSI, $stopLoss, $next_trade_date); crsi_buy($trade_date, $breakOutSignal, $ADX_filter, $breakOutOrderBy, $portfolioID, $dailyBuyList, $enterCRSI, $enterRange, $enterLimit, $tranHistory, $orderBy, $next_trade_date); $value = get_historical_turtle_portfolio_value($trade_date, $portfolioID); $preturn = ($value - $original_investment) / $original_investment * 100; $dollar_return = $value - $original_investment; if ($preturn > $maxReturn) { $maxReturn = $preturn; } if ($preturn < $minReturn) { $minReturn = $preturn; } $portfolioReturn[$count]['trade_date'] = $trade_date; $portfolioReturn[$count]['return'] = $preturn; $portfolioReturn[$count]['value'] = $value; $portfolioReturn[$count]['maxReturn'] = $maxReturn; $portfolioReturn[$count]['minReturn'] = $minReturn; $portfolioReturn[$count]['dollar_return'] = $dollar_return; $newDateStr = strtotime($trade_date); $newDateStr = $newDateStr * 1000 - 14400000; array_push($retArray, array($newDateStr, $preturn)); $count++; } ## batch insert into crsi_portfolio_performance afterward $sql = array(); foreach ($portfolioReturn as $row) { $sql[] = '(' . $portfolioID . ", '" . mysql_real_escape_string($row['trade_date']) . "', " . $row['dollar_return'] . ", " . $row['return'] . ", " . $row['value'] . ")"; } try { mysql_query('INSERT INTO crsi_portfolio_performance (portfolio_id, trade_date, return_dollar, return_pct, portfolio_value) VALUES ' . implode(',', $sql)); } catch (Exception $e) { echo "Query: {$query} \n"; echo 'Caught exception: ', $e->getMessage(), "\n"; } foreach ($tranHistArray as $th) { try { queryMysql($th); } catch (Exception $e) { echo "Query: {$th} \n"; echo 'Caught exception: ', $e->getMessage(), "\n"; } } mysql_close(); return json_encode($retArray); }
while ($data = mysql_fetch_row($result)) { $dateArray[$dateCount]['trade_date'] = $data[0]; $dateCount++; #print "trade date in array: $data[0] <br />"; } $count = 0; $minReturn = 100; $maxReturn = -100; for ($x = 0; $x < $dateCount; $x++) { #while ($data = mysql_fetch_row($result)) { # $trade_date = $data[0]; $trade_date = $dateArray[$x]['trade_date']; if ($showOutput) { print "trade date: {$trade_date} <br />"; } crsi_sell($trade_date, $portfolioID); crsi_buy($trade_date, $breakOutSignal, $ADX_filter, $breakOutOrderBy, $portfolioID, $dailyBuyList); $value = get_historical_turtle_portfolio_value($trade_date, $portfolioID); $preturn = ($value - $original_investment) / $original_investment * 100; $dollar_return = $value - $original_investment; if ($preturn > $maxReturn) { $maxReturn = $preturn; } if ($preturn < $minReturn) { $minReturn = $preturn; } $portfolioReturn[$count]['trade_date'] = $trade_date; $portfolioReturn[$count]['return'] = $preturn; $portfolioReturn[$count]['value'] = $value; $portfolioReturn[$count]['maxReturn'] = $maxReturn; $portfolioReturn[$count]['minReturn'] = $minReturn;
$keyStatsArray[0]['neg_tran_probability'] = round($keyStatsArray[0]['total_neg_tran'] / $keyStatsArray[0]['tran_count'] * 100, 2); echo json_encode($keyStatsArray); } elseif ($_GET['action'] == 'getRealTimeQuote') { $symbol = $_GET['symbol']; $quote = get_yahoo_rt_quote($symbol); echo json_encode($quote); } elseif ($_GET['action'] == 'test_turtle_portfolio_sell') { $today_date = $_GET['today_date']; $date = $_GET['date']; $portfolioID = 1; turtle_portfolio_sell($date, $portfolioID); } elseif ($_GET['action'] == 'test_crsi_sell') { $today_date = $_GET['today_date']; $date = $_GET['date']; $portfolioID = 1; crsi_sell($date, $portfolioID); } elseif ($_GET['action'] == 'test_turtle_portfolio_update_stop_loss') { $today_date = $_GET['today_date']; $date = $_GET['date']; turtle_portfolio_update_stop_loss($date); } elseif ($_GET['action'] == 'test_turtle_portfolio_buy') { $today_date = $_GET['today_date']; $date = $_GET['date']; turtle_portfolio_buy($date); } elseif ($_GET['action'] == 'test_turtle_portfolio_pyramid_buy') { $today_date = $_GET['today_date']; $date = $_GET['date']; turtle_portfolio_pyramid_buy($date); } elseif ($_GET['action'] == 'get_current_portfolio_value') { $today_date = $_GET['today_date']; $date = $_GET['date'];
while ($data = mysql_fetch_row($result)) { $dateArray[$dateCount]['trade_date'] = $data[0]; $dateCount++; #print "trade date in array: $data[0] <br />"; } $count = 0; $minReturn = 100; $maxReturn = -100; for ($x = 0; $x < $dateCount; $x++) { #while ($data = mysql_fetch_row($result)) { # $trade_date = $data[0]; $trade_date = $dateArray[$x]['trade_date']; if ($showOutput) { print "trade date: {$trade_date} <br />"; } crsi_sell($trade_date, $portfolioID, $tranHistory); crsi_buy($trade_date, $breakOutSignal, $ADX_filter, $breakOutOrderBy, $portfolioID, $dailyBuyList, $enterCRSI, $enterRange, $enterLimit, $tranHistory); $value = get_historical_turtle_portfolio_value($trade_date, $portfolioID); $preturn = ($value - $original_investment) / $original_investment * 100; $dollar_return = $value - $original_investment; if ($preturn > $maxReturn) { $maxReturn = $preturn; } if ($preturn < $minReturn) { $minReturn = $preturn; } $portfolioReturn[$count]['trade_date'] = $trade_date; $portfolioReturn[$count]['return'] = $preturn; $portfolioReturn[$count]['value'] = $value; $portfolioReturn[$count]['maxReturn'] = $maxReturn; $portfolioReturn[$count]['minReturn'] = $minReturn;