Step 6: Do a MySQL query to extract the latest 50 days of entries. $result2 = mysql_query("SELECT * FROM `sp500` WHERE `entry`>='$lastentry' AND `entry`<='$numberofentries' ORDER BY `entry` DESC") or die(mysql_error()); The tricky part is to sort the resulted queries by descending order. This will ensure that output results are sorted from the newest to oldest entries. Step 7: Define the limits for the 200-day moving average and 50-day moving average: /Define limits for 200 day moving average $lowerlimit= $numberofentries - 200 + 1; $upperlimit= $numberofentries; //Define limits for 50 day moving average $lowerlimit50= $numberofentries - 50 + 1; $upperlimit= $numberofentries; Step 8: Define the MySQL query to calculate the 200-day moving average. $result3 = mysql_query("SELECT avg(close) from `sp500` WHERE `entry`>='$lowerlimit' AND `entry`<='$upperlimit'") or die(mysql_error()); // store the record of the "example" table into $row $row3 = mysql_fetch_array($result3) or die("Invalid query: " . mysql_error()); // Print out the contents of the entry $ma200 = $row3['avg(close)']; Step 9: Define the MySQL query to calculate the 50-day moving average. $result4 = mysql_query("SELECT avg(close) from `sp500` WHERE `entry`>='$lowerlimit50' AND `entry`<='$upperlimit'") or die(mysql_error()); // store the record of the "example" table into $row $row4 = mysql_fetch_array($result4) or die("Invalid query: " . mysql_error()); // Print out the contents of the entry $ma50 = $row4['avg(close)']; Step 10: Assign to variables and round numbers for easier HTML display. $w=round($ma200,2); $x=round($ma50,2); Step 11: Compute DELTA and round results. $y=round(($ma50-$ma200),2); Step 12: Compute %STRENGTH and round results. $z=round(((($ma50-$ma200)/$ma200)*100),3); Step 13: Create the PHP script to make “recommendations.” if ($z >= 8.51) { $recommendation = 'above SATURATED BULL TREND( VERY HIGH RISK BUYING)'; } elseif ( $z >=5.1 && $z <=8.5) { $recommendation = 'CONFIRMED BULL TREND (HIGH RISK BUYING)'; } elseif ( $z >=2 && $z <=5) { $recommendation = 'CONFIRMED BULL TREND (LOW RISK BUYING)'; } elseif ( $z >=0 && $z <=1.99) { $recommendation = 'UNCONFIRMED BULL TREND'; } elseif ( $z <=0 && $z >=-4.99) { $recommendation = 'UNCONFIRMED BEAR TREND'; } elseif ( $z <=-5 && $z >=-8) { $recommendation = 'CONFIRMED BEAR TREND (LOW RISK SHORT)'; } elseif ( $z <=-8.1 ) { $recommendation = 'SATURATED BEAR TREND( VERY HIGH RISK SHORT)'; } Note: threshold values were taken from historical records of S&P. These computations are out of the scope of this tutorial.
blog comments powered by Disqus |
|
|
|
|
|
|
|