Home arrow PHP arrow Page 3 - Building an E-mini Trading System Using PHP and Advanced MySQL Queries

Implementation, steps 6 through 13 - PHP

This article shows illustrative examples of how PHP and some advanced MySQL queries can be used to build an online trading system. For simplicity, we will be featuring one of the most common stock indexes: the S&P 500 index.

TABLE OF CONTENTS:
  1. Building an E-mini Trading System Using PHP and Advanced MySQL Queries
  2. Implementation, steps 1 through 5
  3. Implementation, steps 6 through 13
  4. Implementation, steps 14 and 15
By: Codex-M
Rating: starstarstarstarstar / 4
November 10, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: