*/ $newDateStr = strtotime($stockRetArray[$x]['trade_date']); $newDateStr = $newDateStr * 1000 - 14400000; $pclose = $stockRetArray[$x]['close'] * 1 / 1; //print "return: $preturn"; //array_push($retArray, array($newDateStr, $preturn)); array_push($retArray, array($newDateStr, $pclose)); //array_push($retArray, {$newDateStr, $preturn}); } echo json_encode($retArray); } elseif ($_GET['action'] == 'get_stock_price_history_ohlc') { $symbol = $_GET['symbol']; $start_date = $_GET['start_date']; $retArray = array(); $stockRetArray = array(); $stockRetArray = historical_stock_price($symbol, $start_date); $arrayLen = count($stockRetArray); for ($x = 0; $x < $arrayLen; $x++) { /*print "<BR>date: $stockRetArray[$x] close: $stockRetArray[$x]['trade_date'] " ; print $stockRetArray[$x]['trade_date']; print $stockRetArray[$x]['close']; print $stockRetArray[$x]['return']; print "<BR>"; */ $newDateStr = strtotime($stockRetArray[$x]['trade_date']); $newDateStr = $newDateStr * 1000 - 14400000; $pclose = $stockRetArray[$x]['close'] * 1 / 1; $popen = $stockRetArray[$x]['open'] * 1 / 1; $phigh = $stockRetArray[$x]['high'] * 1 / 1; $plow = $stockRetArray[$x]['low'] * 1 / 1; //print "return: $preturn";
$swingArray = chart_trends($symbol, $start_date, $end_date, $pid); $arrayLen = count($swingArray); echo json_encode($swingArray); } elseif ($_GET['action'] == 'test_historical_stock_price') { $symbol = $_GET['symbol']; $start_date = $_GET['start_date']; $e_date = $_GET['end_date']; $pid = $_GET['portfolio_id']; if (!$e_date) { $e_date = date("Y-m-d"); } if (!$pid) { $pid = 1; } $swingArray = array(); $swingArray = historical_stock_price($symbol, $start_date, $e_date, $pid); echo json_encode($swingArray); } } function historical_stock_return($symbol, $start_date, $end_date) { $perfArray = array(); $count = 0; // if end date is not supplied, default to today if (!$end_date) { $end_date = date("Y-m-d"); } // get close price for symbol on starting date $query = "select close from quotes where symbol = '" . $symbol . "' and trade_date = "; $query .= "(select min(trade_date) from quotes where symbol = '" . $symbol . "' and trade_date >= '" . $start_date . "' and trade_date <= '" . $end_date . "')"; $result = queryMysql($query);
function generate_trends($symbol, $start_date, $end_date, $time_frame) { $stockRetArray = array(); $retArray = array(); $eachRow = array(); $spArray = array(); $sphArray = array(); $splArray = array(); $trend_strength = 0; $spArray = generate_swing_points($symbol, $start_date, $end_date, $time_frame); $spArrayLen = count($spArray); for ($x = 0; $x < $spArrayLen; $x++) { if ($spArray[$x]['type'] == "SPH") { array_push($sphArray, $spArray[$x]); } else { if ($spArray[$x]['type'] == "SPL") { array_push($splArray, $spArray[$x]); } } } $sphLen = count($sphArray); $splLen = count($splArray); $sphPos = 0; $splPos = 0; $stockRetArray = historical_stock_price($symbol, $start_date, $end_date); $arrayLen = count($stockRetArray); // use previous sph/spl data to determine trend transistion $previous_sph = 0; $previous_sph_volume = 1; $previous_spl = 10000; $previous_spl_volume = 1; // there are 7 possible current // CB = confirmed bullish // SB = suspect bullish // CR = confirmed bearish // SR = suspect bearish // CS = confirmed sideway // SS = suspect sideway $current_trend = "SS"; $trend_array = array(); $each_trend_row = array(); for ($x = 0; $x < $arrayLen; $x++) { $newDateStr = strtotime($stockRetArray[$x]['trade_date']); $newDateStr = $newDateStr * 1000 - 14400000; $pclose = $stockRetArray[$x]['close'] * 1 / 1; $popen = $stockRetArray[$x]['open'] * 1 / 1; $phigh = $stockRetArray[$x]['high'] * 1 / 1; $plow = $stockRetArray[$x]['low'] * 1 / 1; $pvolume = $stockRetArray[$x]['volume'] * 1 / 1; $pavg_volume = $stockRetArray[$x]['avg_volume'] * 1 / 1; $prelative_avg_vol = $stockRetArray[$x]['relative_avg_vol'] * 1 / 1; $pATR = $stockRetArray[$x]['ATR'] * 1 / 1; // update SPH and SPL position if ($sphArray[$sphPos + 1]['date'] >= $newDateStr && $newDateStr > $sphArray[$sphPos]['date']) { $sphPos++; } if ($splArray[$splPos + 1]['date'] >= $newDateStr && $newDateStr > $splArray[$splPos]['date']) { $splPos++; } // find new trend if ($phigh > $sphArray[$sphPos - 1]['price']) { $new_trend = get_new_bullish_trend($current_trend, $phigh, $pvolume, $newDateStr, $previous_sph, $sphArray[$sphPos - 1]['volume'], $sphArray[$sphPos - 1]['date']); // each SPH point will only be used to change trend once if ($sphPos < $sphLen) { $sphPos++; } //determine trend strength /* if (($previous_trend == "CR" || $previous_trend == "SR") && ($current_trend == "SS")) { $trend_strength = -1; } else if ($current_trend == "CB") { $trend_strength = 3; } else if ($current_trend == "SB") { $trend_strength = 2; } else if ($current_trend == "SS") { $trend_strength = 0; } else if ($current_trend == "CS") { $trend_strength = 0; } else if ($current_trend == "SR") { $trend_strength = -2; } else if ($current_trend == "CR") { $trend_strength = -3; } */ if (($current_trend == "CR" || $current_trend == "SR") && $new_trend == "SS") { $trend_strength = -1; } else { if ($new_trend == "CB") { $trend_strength = 3; } else { if ($new_trend == "SB") { $trend_strength = 2; } else { if ($new_trend == "SS") { $trend_strength = 0; } else { if ($new_trend == "CS") { $trend_strength = 0; } else { if ($new_trend == "SR") { $trend_strength = -2; } else { if ($new_trend == "CR") { $trend_strength = -3; } } } } } } } //print "HIGH: current trend: $current_trend new trend: $enw_trend strength: $trend_strength \n"; /* else if HIGH > SPH if prior trend = CR/SR AND current trend = SS strength = -1 else if current trend = CB strength = 3 else if current trend = SB strength = 2 else if current trend = CS strength = 0 else if current trend = SS strength = 0 else if current trend = SR strength = -2 else if current trend = CR strength = --3 */ //print "sph: "; //print $sphArray[$sphPos]['price']; //print " $previous_sph sph date: $previous_sph_date phigh: $phigh $current_trend new trend: $new_trend \n"; if ($new_trend != $current_trend) { $previous_trend = $current_trend; $each_trend_row['date'] = $newDateStr; $each_trend_row['date_string'] = $stockRetArray[$x]['trade_date']; $each_trend_row['price'] = $phigh; $each_trend_row['trend'] = $new_trend; $each_trend_row['strength'] = $trend_strength; array_push($trend_array, $each_trend_row); /* print "HIGH: date: $newDateStr price: $phigh sph price: "; print $sphArray[$sphPos-1]['price']; print " position: $sphPos trend: $new_trend \n"; */ $current_trend = $new_trend; } } // find new trend if ($plow < $splArray[$splPos - 1]['price']) { $new_trend = get_new_bearish_trend($current_trend, $plow, $pvolume, $newDateStr, $previous_spl, $splArray[$splPos - 1]['volume'], $splArray[$splPos - 1]['date']); if ($splPos < $splLen) { $splPos++; } //determine trend strength /* if (($previous_trend == "CB" || $previous_trend == "SB") && ($current_trend == "SS")) { $trend_strength = 1; } else if ($current_trend == "CB") { $trend_strength = 3; } else if ($current_trend == "SB") { $trend_strength = 2; } else if ($current_trend == "SS") { $trend_strength = 0; } else if ($current_trend == "CS") { $trend_strength = 0; } else if ($current_trend == "SR") { $trend_strength = -2; } else if ($current_trend == "CR") { $trend_strength = -3; } */ if (($current_trend == "CB" || $current_trend == "SB") && $new_trend == "SS") { $trend_strength = 1; } else { if ($new_trend == "CB") { $trend_strength = 3; } else { if ($new_trend == "SB") { $trend_strength = 2; } else { if ($new_trend == "SS") { $trend_strength = 0; } else { if ($new_trend == "CS") { $trend_strength = 0; } else { if ($new_trend == "SR") { $trend_strength = -2; } else { if ($new_trend == "CR") { $trend_strength = -3; } } } } } } } // print "current trend: $current_trend new trend: $enw_trend strength: $trend_strength \n"; /* if prior trend = CB/SB AND current trend is SS strength = 1 else if current trend = CB strength = 3 else if current trend = SB strength = 2 else if current trend = SS strength = 0 else if current trend = CS strength = 0 else if current trend = SR strength = -2 else if current trend = CR strength = -3 */ if ($new_trend != $current_trend) { $previous_trend = $current_trend; $each_trend_row['date'] = $newDateStr; $each_trend_row['date_string'] = $stockRetArray[$x]['trade_date']; $each_trend_row['price'] = $plow; $each_trend_row['trend'] = $new_trend; $each_trend_row['strength'] = $trend_strength; array_push($trend_array, $each_trend_row); //print "LOW: date: $newDateStr price: $plow tred: $new_trend \n"; $current_trend = $new_trend; } } } return $trend_array; //return $trend_array; }