Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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.
Standard Formula
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:
Recursive Formula
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.
False 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.
Blue: CurrentSessionVWAP
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?
Can you help answer these questions from other members on NexusFi?
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.
Empirical Finding
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.