describe ai_trades; * +-----------------+--------------------------------------------------+------+-----+---------+----------------+ * | Field | Type | Null | Key | Default | Extra | * +-----------------+--------------------------------------------------+------+-----+---------+----------------+ * | id | int(11) | NO | PRI | NULL | auto_increment | * | trade_id | varchar(100) | NO | UNI | NULL | | * | symbol | varchar(20) | NO | | NULL | | * | creation | datetime | YES | | NULL | | * | entry_date | datetime | YES | | NULL | | * | exit_date | datetime | YES | | NULL | | * | shares | int(11) | NO | | 1 | | * | entry_price | double | NO | | NULL | | * | exit_price | double | YES | | NULL | | * | stop_price | double | YES | | NULL | | * | target_price | double | YES | | NULL | | * | time_stop | datetime | YES | | NULL | | * | exit_reason | enum('open','time_stop','stop_hit','target_hit') | NO | | open | | * | profit | double | NO | | 0 | | * | max_profit | double | NO | | 0 | | * | min_profit | double | NO | | 0 | | * | max_profit_time | datetime | YES | | NULL | | * | min_profit_time | datetime | YES | | NULL | | * | is_long | tinyint(4) | NO | | 1 | | * | winner | tinyint(4) | NO | | 0 | | * | nonexit_profit | double | YES | | NULL | | * | spy_change | double | YES | | NULL | | * | strategy_id | int(11) | YES | | NULL | | * +-----------------+--------------------------------------------------+------+-----+---------+----------------+ * 21 rows in set (0.00 sec) * */ $options = getopt("d::t::"); $date_where = "DATE(entry_date) = DATE(NOW())"; $custom_date = ""; if (empty($_SERVER['REMOTE_ADDR']) && array_key_exists("d", $options) && preg_match("/^\d{4}-\d{2}-\d{2}$/", $options['d'])) { $custom_date = $options['d']; $date_where = "DATE(entry_date) = DATE('$custom_date')"; } else if ($_REQUEST['date'] && preg_match("/^\d{4}-\d{2}-\d{2}$/", $_REQUEST['date'])) { $custom_date = $_REQUEST['date']; $date_where = "DATE(entry_date) = DATE('$custom_date')"; } $custom_time = ""; if (empty($_SERVER['REMOTE_ADDR']) && array_key_exists("t", $options) && preg_match("/^\d{2}:\d{2}$/", $options['t'])) { $custom_time = $options['t']; } else if ($_REQUEST['time'] && preg_match("/^\d{2}:\d{2}$/", $_REQUEST['time'])) { $custom_time = $_REQUEST['time']; } $segment_codes = array( "main" => "ai1", "test" => "ai2", "aws1" => "ai3", ); foreach (array("main", "test", "aws1") as $segment) { $sql = "SELECT COUNT(*) AS trade_count, SUM(profit) AS normal_profit, SUM(nonexit_profit) AS nonexit_profit, MAX(spy_change) AS spy_change, MAX(DATE(entry_date)) AS last_date FROM ai_trades WHERE $date_where AND category = '$segment'"; //echo "SQL is: $sql\n"; $result = mysql_query($sql); if ($result) { $row = mysql_fetch_assoc($result); $trade_count = $row['trade_count']; $normal_profit = $row['normal_profit']; # risk off profit $nonexit_profit = $row['nonexit_profit']; # risk on profit $best_profit = "$" . number_format($normal_profit, 2); $spy_change = number_format($row['spy_change'], 2); $last_date = $row['last_date']; $raw_profit = $normal_profit; $history_column = "profit_history"; // risk off profit history if ($nonexit_profit > $normal_profit) { $history_column = "profit_history_nonexit"; // risk on profit history $best_profit = "$" . number_format($nonexit_profit, 2); $raw_profit = $nonexit_profit; } $trades_sql = "SELECT $history_column AS the_profit_history, symbol, UNIX_TIMESTAMP(entry_date) AS entry_timet FROM ai_trades WHERE $date_where AND category = '$segment' ORDER BY entry_date"; $trades_result = mysql_query($trades_sql); $all_trades = array(); $first_trade_timet = ""; if ($trades_result) { while ($trade_row = mysql_fetch_assoc($trades_result)) { //print_r($trade_row); $profit_history = $trade_row['the_profit_history']; $entry_timet = $trade_row['entry_timet']; if ($first_trade_timet == "") { $first_trade_timet = $entry_timet; //echo "Setting first_trade to $first_trade_timet\n"; } array_push($all_trades, $trade_row); } } $all_profits = array(); $min_profit = 0; $max_profit = 0; $count_trades = count($all_trades); if (count($all_trades) > 0) { $trading_minute = $first_trade_timet; $now = time(); $now_date = $now; if ($custom_date) { $now_time_part = date("H:i:s", $now); if ($custom_time) { $now_time_part = $custom_time; } //echo "Getting now date using: $custom_date $now_time_part\n"; $now_date = strtotime("$custom_date $now_time_part"); } $market_close = get_market_close($now_date); $minutes_ago = (int)($now_date - $trading_minute) / 60; while ($trading_minute >= $first_trade_timet && $trading_minute < $market_close && $trading_minute < $now_date && $minutes_ago > 1) { $date_string = date("Y-m-d h:i:s", $trading_minute); //echo "Minute: $date_string\n"; $profit = get_total_profit($trading_minute, $all_trades); if ($profit > $max_profit) { $max_profit = $profit; } if ($profit < $min_profit) { $min_profit = $profit; } array_push($all_profits, $profit); $trading_minute = $trading_minute + 60; $minutes_ago = (int)(($now - $trading_minute) / 60); } } //echo implode(", ", $all_profits); $image_width = 100; $image_height = 100; $top_buffer = 1; $bottom_buffer = 1; $created_image = false; $polygon_point_list = array(); if ($max_profit - $min_profit > 0) { $created_image = true; $green = "rgb(0, 100, 0)"; $red = "rgb(255, 0, 0)"; $yellow = "rgb(255, 255, 0)"; $gray = "rgb(220, 220, 220)"; $top_bottom_pixel_width = 0; $y_max = $max_profit; $y_min = $min_profit; $y_height = $y_max - $y_min; $time_period_pixel_width = $image_width / count($all_profits); $current_x = 0; $start_y = 0; $zero_y_percent = ($start_y - $y_min) / $y_height; $zero_y = $image_height - ($zero_y_percent * $image_height); $current_y = $zero_y; foreach ($all_profits as $profit) { $profit_y_percent = ($profit - $y_min) / $y_height; $profit_y = $image_height - ($profit_y_percent * $image_height); //echo "$profit -> $profit_y ($profit_y_percent) height=$y_height (max=$y_max $max_profit, min=$y_min $min_profit)\n"; $line_color = $green; if ($profit < 0) { $line_color = $red; } //ImageLine($image, $current_x, $current_y, $current_x + $time_period_pixel_width, $profit_y, $yellow); $polygon_values = array( $current_x, $current_y + $top_bottom_pixel_width, $current_x + $time_period_pixel_width, $profit_y + $top_bottom_pixel_width, $current_x + $time_period_pixel_width, $zero_y, $current_x, $zero_y ); $point_a = $current_x . "," . ($current_y + $top_bottom_pixel_width); $point_b = ($current_x + $time_period_pixel_width) . "," . ($profit_y + $top_bottom_pixel_width); $point_c = ($current_x + $time_period_pixel_width) . "," . $zero_y; $point_d = $current_x . "," . $zero_y; if ($profit < 0) { $polygon_values = array( $current_x, $current_y - $top_bottom_pixel_width, $current_x + $time_period_pixel_width, $profit_y - $top_bottom_pixel_width, $current_x + $time_period_pixel_width, $zero_y, $current_x, $zero_y ); $point_a = $current_x . "," . ($current_y - $top_bottom_pixel_width); $point_b = ($current_x + $time_period_pixel_width) . "," . ($profit_y - $top_bottom_pixel_width); $point_c = ($current_x + $time_period_pixel_width) . "," . $zero_y; $point_d = $current_x . "," . $zero_y; } $polygon = ""; //echo "$polygon\n"; array_push($polygon_point_list, $polygon); $current_x += $time_period_pixel_width; $current_y = $profit_y; } } $profit_html = "$best_profit"; if ($raw_profit < 0) { $profit_html = "$best_profit"; } $segment_code = $segment_codes[$segment]; $image_urls[$segment_code]['polygons'] = implode("\n", $polygon_point_list); $image_urls[$segment_code]['trades'] = $trade_count; $image_urls[$segment_code]['profit'] = $profit_html; } } //$image_urls['request_uri'] = $request_uri; $image_url_string = json_encode($image_urls); echo $image_url_string; $cache_contents = ob_get_contents(); write_cache($cache_file, $cache_contents); function get_total_profit($trading_minute, $all_trades) { $total_profit = 0; foreach ($all_trades as $trade) { $profit = get_trade_profit_at_minute($trading_minute, $trade); $total_profit = $total_profit + $profit; } return $total_profit; } function get_trade_profit_at_minute($trading_minute, $trade) { $entry_timet = $trade['entry_timet']; $minute_into_trade = (int)(($trading_minute - $entry_timet) / 60); $profit_history = explode(",", $trade['the_profit_history']); $symbol = $trade['symbol']; $history_count = count($profit_history); //echo "Symbol=$symbol, index=$minute_into_trade, history=$history_count\n"; if ($minute_into_trade > 0 && $history_count >= $minute_into_trade + 1) { //echo "Attempting to get profit for $symbol: $profit\n"; $profit = $profit_history[$minute_into_trade]; //echo "Getting profit for $symbol: $profit\n"; } else if ($minute_into_trade >= $history_count) { $profit = $profit_history[$history_count - 1]; } return $profit; } function get_market_open($time_t) { $ymd = date("Y-m-d", $time_t); return strtotime("$ymd 6:30am"); } function get_market_close($time_t) { $ymd = date("Y-m-d", $time_t); return strtotime("$ymd 1:00pm"); } ?>