I am using PHP and recently started using the technical analysis extension TA-LIB to calculate indicators on stock price data. I am getting what I consider a strange result on one of the simplest indicators there is, the simple moving average, and I have not figured out the reason. I have posted a similar question on the TA-LIB forum, but the activity there is very low and I suspect I might not get an answer in a long time, if at all.
The SMA calculates the average based on a given number of previous price datapoints. The problem I am having is best illustrated by an example. Let us say I have a set of 10 price datapoints and I would like to calculate the SMA. For simplicity let us use an average of 4. When calculating this we would expect a new set of 7 averagevalues. However, TA-LIB returns 4 as seen below:
$sma=trader_sma($close,4);
echo"<pre>";
print_r($sma);
Array
(
[0] => Array
(
[0] =>
[1] => 0
[2] =>
)
[1] => Array
(
[0] =>
[1] => 1
[2] =>
)
[2] => Array
(
[0] =>
[1] => 2
[2] =>
)
[3] => Array
(
[0] =>
[1] => 3
[2] => 573.267
)
[4] => Array
(
[0] =>
[1] => 4
[2] => 565.307
)
[5] => Array
(
[0] =>
[1] => 5
[2] => 560.552
)
[6] => Array
(
[0] =>
[1] => 6
[2] => 557.382
)
)
As you can see we get nulls at the beginning of the set, as expected. However, we are missing datapoints at the end and the number of missing datapoints always corresponds to the number of datapoints I am averaging over. This causes an unatural gap at the end when I plot the moving average against my price data.
Any ideas on what is going on?