function turtle_portfolio_update_stop_loss_with_spl($date, $portfolioID) { global $max_num_holdings; global $max_risk; global $risk_factor; global $stop_loss_muptiplier; $splArray = array(); $query = "select a.symbol, close, low, daily_change, pct_change, ATR, 55_DAY_HIGH, 20_DAY_HIGH, 20_DAY_LOW, 10_DAY_LOW, 50_MA, 200_MA, stop_loss, stop_buy, shares "; $query .= "from turtle_portfolio a, quotes b "; $query .= "where a.symbol = b.symbol "; $query .= "and a.portfolio_id = " . $portfolioID . " "; $query .= "and a.symbol != 'CASH' "; $query .= "and b.trade_date = '" . $date . "'"; $result = queryMysql($query); $ph = array(); $i = 0; while ($data = mysql_fetch_row($result)) { $ph[$i]['symbol'] = str_replace("\"", "", $data[0]); $ph[$i]['close'] = str_replace("\"", "", $data[1]); $ph[$i]['low'] = str_replace("\"", "", $data[2]); $ph[$i]['daily_change'] = str_replace("\"", "", $data[3]); $ph[$i]['pct_change'] = str_replace("\"", "", $data[4]); $ph[$i]['ATR'] = str_replace("\"", "", $data[5]); $ph[$i]['55_DAY_HIGH'] = str_replace("\"", "", $data[6]); $ph[$i]['20_DAY_HIGH'] = str_replace("\"", "", $data[7]); $ph[$i]['20_DAY_LOW'] = str_replace("\"", "", $data[8]); $ph[$i]['10_DAY_LOW'] = str_replace("\"", "", $data[9]); $ph[$i]['50_MA'] = str_replace("\"", "", $data[10]); $ph[$i]['200_MA'] = str_replace("\"", "", $data[11]); $ph[$i]['stop_loss'] = str_replace("\"", "", $data[12]); $ph[$i]['stop_buy'] = str_replace("\"", "", $data[13]); $ph[$i]['shares'] = str_replace("\"", "", $data[14]); $i++; } for ($x = 0; $x < $i; $x++) { # if stop loss is less than (close price - 2N), make (close - 2N) the new stop loss //$new_stop_loss = $ph[$x]['close'] - ($stop_loss_muptiplier*$ph[$x]['ATR']); $splArray = get_last_spl($ph[$x]['symbol'], $date); $new_stop_loss = $splArray[1] - 0.25 * $ph[$x]['ATR']; if ($ph[$x]['stop_loss'] < $new_stop_loss) { //$new_stop_loss = $ph[$x]['close'] - ($stop_loss_muptiplier*$ph[$x]['ATR']); $update_sql = "update turtle_portfolio set stop_loss = " . $new_stop_loss . " where symbol = '" . $ph[$x]['symbol'] . "' and portfolio_id = " . $portfolioID; $result = queryMysql($update_sql); } } }
$pid = 1; } $swingArray = array(); $swingArray = generate_trends($symbol, $start_date, $end_date, $pid); $arrayLen = count($swingArray); echo json_encode($swingArray); } else { if ($_GET['action'] == 'test_get_last_spl') { $symbol = $_GET['symbol']; $current_date = $_GET['current_date']; $pid = $_GET['portfolio_id']; if (!$current_date) { $current_date = date("Y-m-d"); } $retArray = array(); $retArray = get_last_spl($symbol, $current_date); print "spl date: "; print $retArray['date']; print " spl: "; print $retArray['price']; } else { if ($_GET['action'] == 'test_get_last_sph') { $symbol = $_GET['symbol']; $current_date = $_GET['current_date']; $pid = $_GET['portfolio_id']; if (!$current_date) { $current_date = date("Y-m-d"); } $retArray = array(); $retArray = get_last_sph($symbol, $current_date); print "sph date: ";