function live_crsi_buy($date, $portfolioID, $enterCRSI, $enterRange, $enterLimit, $orderBy) { global $max_num_holdings; global $max_risk; global $risk_factor; global $stop_loss_multiplier; global $portfolio_tbl; global $showOutput; global $commission; global $tranHistArray; global $portfolio_table; global $live_daily_buy_list; global $quote_table; global $liveportfolio; echo "date: ", $date, " id: ", $portfolioID, " order by: ", $orderBy, PHP_EOL; $breakOutStockArray = live_getBuyList($date, $portfolioID, $orderBy); #var_dump ($breakOutStockArray); $len_array = count($breakOutStockArray); //$risk_factor = 1 / $max_num_holdings; $portfolio_value = 0; $current_pos = 0; $next_buy_point = 100000; $stop_loss = 0; $num_shares = 0; $current_N = 0; $risk_value = 0; $purchase_value = 0; $sim_start_day = 55; $pyramid_mode = 0; $stop_loss = 0; $current_trade_date_id = 0; $workingArray = $breakOutStockArray; $adxArray = ""; $adxCount = 1; $len_array = count($breakOutStockArray); $portfolio_value = live_get_portfolio_value($date, $portfolioID); $query = "select shares from {$liveportfolio} where symbol = 'CASH' and portfolio_id = " . $portfolioID; $result = queryMysql($query); while ($data = mysql_fetch_row($result)) { $cash = $data[0]; } $buyingpower = 0; $pendingcash = 0; $query = "select sum(a.shares * b.close) from {$order_queue} a, {$quote_table} b where portfolio_id = {$portfolioID} and a.symbol = b.symbol and a.order_type like '%SELL%'"; $result = queryMysql($query); while ($data = mysql_fetch_row($result)) { $pendingcash = $data[0]; } $buyingpower = $cash + $pendingcash; #$current_risk = get_portfolio_risk($date, $portfolioID); $current_risk = live_get_portfolio_risk($date, $portfolioID); $buy_array = array(); for ($x = 0; $x < $len_array; $x++) { ### get portfolio value #$portfolio_value = get_historical_turtle_portfolio_value($date, $portfolioID); ### get current available cash #$query = "select shares from turtle_portfolio where symbol = 'CASH' and portfolio_id = ".$portfolioID; #$result = queryMysql($query); #while ($data = mysql_fetch_row($result)) { # $cash = $data[0]; #} $risk_value = $portfolio_value * $risk_factor / 100; $current_N = $breakOutStockArray[$x]['ATR']; if ($current_N > 0) { $num_shares = floor($risk_value / ($stop_loss_multiplier * $current_N)); } $purchase_price = $breakOutStockArray[$x]['close']; $purchase_value = $num_shares * $purchase_price + $commission; $stop_loss = $purchase_price - $stop_loss_multiplier * $current_N; $stop_buy = $purchase_price + $current_N; echo "buying power: ", $buyingpower, "cash: ", $cash, " purchase value: ", $purchase_value, " current risk: ", $current_risk, " max risk: {$max_risk} ", " current N: ", $current_N, " num shares: ", $num_shares, PHP_EOL; #if (($cash > $purchase_value) && ($current_risk < $max_risk)) { if ($buyingpower > $purchase_value && $current_risk < $max_risk) { #$cash = $cash - $purchase_value; $buyingpower = $buyingpower - $purchase_value; $risk_dollar = $num_shares * ($stop_loss_multiplier * $current_N); $risk_pct = $risk_dollar / $portfolio_value * 100; $current_risk += $risk_pct; $order_type = "BUY_OPEN"; $symbol = $breakOutStockArray[$x]['symbol']; #echo "date: $today symbol: $symbol num shares: $num_shares ", PHP_EOL; add_order($portfolioID, $order_type, $symbol, $num_shares); } else { #echo "trade date: $date ran out of money ", PHP_EOL; break; } } }
If you have any queries please do not hesitate to email me at info@julian-young.com ***/ include 'config.php'; include 'sqlreporter.php'; include_once '../scripts/dbfunction.php'; include_once '../scripts/connors_strat_live.php'; $all_active_pid = get_all_active_portfolio_id(); var_dump($all_active_pid); $today = date("Y-m-d"); //Setup Email Subject $subject = $today . ': Executed Orders'; //Color is the color report table, it can be set to be grey, green or blue $color = 'blue'; foreach ($all_active_pid as $pid) { $current_value = live_get_portfolio_value($today, $pid); //Add a message to the header of the report $summary_header = '<p><b>Summary for Portfolio ID: ' . $pid . '</b></p><b>Account Setting</b>'; //Add a message to the footer of the report, you can use plain text or html. //$footer = '<p><strong>Note: </strong>This is an optional report footer, ideally used for a report legend and/or signature.</p>'; $footer = ''; $summary_query = 'select description AS "Description", risk_category AS "Risk Category", start_date AS "Portfolio Start Date", starting_capital AS "Starting Capital" from liveportfolio_account where portfolio_id = ' . $pid; $summary_report = generateReport($summary_query, $summary_header, $footer, $color); $performance_header = '<b>Current Performance</b>'; $performance_query = 'select liveportfolio_value(' . $pid . ') AS "Current Value", (liveportfolio_value(' . $pid . ') - starting_capital) AS "Current Dollar Return", round((liveportfolio_value(' . $pid . ') - starting_capital)*100/starting_capital, 2) AS "Current Pct Return" from liveportfolio_account where portfolio_id = ' . $pid; $poerformance_report = generateReport($performance_query, $performance_header, $footer, $color); $sell_header = '<b>Executed Sell Orders</b>'; $sell_query = 'select a.execute_time as "Executed Time", order_type AS "Order Type", symbol AS "Symbol", shares AS "Shares", a.execute_price as "Executed Price" , liveportfolio_get_purchase_price(' . $pid . ', symbol) AS "Purchase Price", round((a.execute_price-liveportfolio_get_purchase_price(' . $pid . ', symbol))*100/liveportfolio_get_purchase_price(' . $pid . ', symbol), 2) AS "PnL %" from order_history_queue a, liveportfolio_account b where a.portfolio_id = b.portfolio_id and b.status = "A" and a.portfolio_id = ' . $pid . ' and a.order_type like "%SELL%" and a.execute_date = "' . $today . '" order by a.execute_time'; $sell_report = generateReport($sell_query, $sell_header, $footer, $color); $buy_header = '<b>Executed Buy Orders</b>'; $buy_query = 'select a.execute_time as "Executed Time", order_type AS "Order Type", symbol AS "Symbol", shares AS "Shares", a.execute_price as "Executed Price" from order_history_queue a, liveportfolio_account b where a.portfolio_id = b.portfolio_id and b.status = "A" and a.portfolio_id = ' . $pid . ' and a.order_type like "%BUY%" and a.execute_date = "' . $today . '" order by a.execute_time';