Im trying to calculate a moving average, however I cant get it to work.
$result = @mysql_query("SELECT close, FROM am where sdate BETWEEN '2004-03-01' AND '2004-03-30'ORDER BY sdate");
while($row = mysql_fetch_assoc($result))
{
$count =+1;
$total =+$row['close'];
if(count==5){
$res=$total/5;
$array[] = $res;
$count =0;
}
}
this will only calculate everage for every 5 days. Im strugling to make it a moving average.
a 5-day simple moving average is calculated by adding the prices for the last 5 days and dividing the total by 5.
10+11+12+13+14 = 60/5
if the next closing price in the average is 15, then this new period would be added and the oldest day, which is 10, would be dropped. The new 5-day simple moving average would be calculated as follows:
11+12+13+14 +15 = 65/5
Im not sure how to drop 10 from the equation and start while loop from 11.
Could anyone advise