I always thought that the SMA - the simple moving average - was one of the simplest indicators. But as it appears, it is not easy to code one that is working correctly.
The standard formula for the SMA is well-known. You add up the input values for the last n periods and divide it by n. If you code this in NinjaScript it will essentially look like this:
Now, NinjaTrader developpers are clever. They use a recursive formula which calculates much faster, as it takes the previous SMA, multiplies it with the period, then adds the new input value and subtracts the value n periods ago. The NinjaTrader SMA therefore looks like this:
Now, theoretically the two formulae should give the same results. Practically, however, there are rounding errors that seem to have an impact on the results.
Let me show a simple example. I have coded a CurrentSessionVWAP for the RTH session of ES, and to please the eye, I want to smooth it with a 3-period-SMA. I only apply the smoothing with the fourth bar of the session, so for the first 3 bars the original CurrentSessionVWAP is displayed.
Yellow: Smoothed with SMA Standard Formula
Red: Smoothed with Default NinjaTrader SMA
Below are two identical charts. The only difference is that the first one has a lookback period of 5 days, while the second one has a lookback period of 65 days
Do you still think that it is easy to code a SMA?
The NinjaTrader guys did not succeed in my opinion. Or am I simply wrong?
I have now tried different settings, even changed the recursive formula. Finally I found the real problem:
The recursive indicators only work, if you change the default settings to MaximumBarsLookBack.Infinite.
So all indicators with recursive formulae need to use this setting. This shows again the problem of NinjaTrader. Because NT 7 is still a memory hog compared to MultiCharts, developpers tried to reduce memory load. Correct results were sacrified to reduce the memory usage.
For every indicator that uses a recursive formula you would need to modify the indicator code and add the line
in the Initialize section of the indicator.
This also applies to SMA and EMA. Once the change is made, NinjaTrader will use more memory, but the results will be correct.
I believe @Richard from Move the Markets did some studies on a fast-starting EMA that does not require that kind of lookback, you wish to poke around his blog. I am not sure if he is still around or not.
Due to time constraints, please do not PM me if your question can be resolved or answered on the forum.
Need help? 1) Stop changing things. No new indicators, charts, or methods. Be consistent with what is in front of you first. 2) Start a journal and post to it daily with the trades you made to show your strengths and weaknesses. 3) Set goals for yourself to reach daily. Make them about how you trade, not how much money you make. 4) Accept responsibility for your actions. Stop looking elsewhere to explain away poor performance. 5) Where to start as a trader? Watch this webinar and read this thread for hundreds of questions and answers. 6) Help using the forum? Watch this video to learn general tips on using the site.
If you want to support our community, become an Elite Member.
The following user says Thank You to Big Mike for this post: