function getRecursive($symbol, $ntd) { $log = new Loggy(); $log->lfile('slope.log'); $tc = ""; $tdc = ""; $tdtd = ""; $ntd_1 = $ntd + 1; $sumntd = $ntd * $ntd_1; $sumntd = $sumntd / 2; require_once '/home/stocksloper/db.inc'; #$getstockhistory = mysql_query("select @rownum:=@rownum+1 trading_day, sh.close # from stockhistory sh, # (select @rownum:=0) r # where symbol = '$symbol' # order by ydate desc limit $ntd") or trigger_error(mysql_error()); $getstockhistory = mysql_query("select t.trading_day, close from\n(select @rownum:=@rownum+1 trading_day, sh.close, sh.ydate\n from stockhistory sh,\n (select @rownum:=0) r\n where symbol = '{$symbol}'\n order by ydate desc limit {$ntd}) t order by ydate asc") or trigger_error(mysql_error()); while ($row = mysql_fetch_array($getstockhistory)) { foreach ($row as $key => $value) { $row[$key] = stripslashes($value); } #dbg # echo $row['trading_day'] . " - " . $row['close']."\n"; $tradingday = $row['trading_day']; $closeprice = $row['close']; $tdb = $tradingday * $closeprice; $tdc = $tdb + $tdc; $tdt = $tradingday * $tradingday; $tdtd = $tdtd + $tdt; $tc = $tc + $closeprice; #dbg #echo "TDC = $tdc, TDT = $tdt, TDTD = $tdtd\n"; } //while getstockhistory mysql_free_result($getstockhistory); #dbg #echo "Totals: sumX = $sumntd, sumY = $tc, sumXY = $tdc, sumX2 = $tdtd\n"; $preslope1a = bcmul($ntd, $tdc, 8); $preslope1b = bcmul($sumntd, $tc, 8); $preslope1 = bcsub($preslope1a, $preslope1b, 8); $preslope2a = bcmul($ntd, $tdtd, 8); $preslope2b = bcmul($sumntd, $sumntd, 8); $preslope2 = bcsub($preslope2a, $preslope2b, 8); $slope = bcdiv($preslope1, $preslope2, 8); $slope = bcmul($slope, '-1', 8); if ($slope >= -0.001 && $slope <= 0.001) { # need to reverse slope as per requirments $slope = bcmul($slope, '-1', 8); #$log->lwrite("Slope $slope in range for $symbol with $ntd days"); # put slope data into database $putslope = mysql_query("insert into slopedata (symbol,slope,tradingdays) values ('{$symbol}','{$slope}','{$ntd}')") or trigger_error(mysql_error()); #echo "slope $slope in range for $symbol\n"; } elseif ($ntd <= 503) { $ntd++; # $log->lwrite("Slope NOT in range for $symbol with $ntd days"); getRecursive($symbol, $ntd); } //elseif }
<?php Autoloader::directories(array(Bundle::path('loggy') . 'classes', Bundle::path('loggy') . 'config')); /* AutoLoader::map(array( 'Remote_Logger' => Bundle::path('loggy').'classes/remote_logger.php', )); */ Loggy::init();
StockSloper Jason Fowler June 2013 === Grabs stock symbol from database using getSymbol function Gets history of stock from getHistory function Does slope analysis using getSlope function Starts with 120 days and keeps going until 2500 days ***/ # Put stuff in a log $log = new Loggy(); $log->lfile('logstock.log'); # Delete previous slope data deleteSlopeData(); # minimum trading days for slope analysis $ntd = 120; # look up stocks in stock_symbol_name table $stocks = getSymbol(); foreach ($stocks as $stock) { $symbol = $stock['symbol']; # look up # of trading days stock traded $days = getDays($symbol); if ($days === 0 || $days < 500) { continue; } else { getRecursive($symbol, $ntd);