How to code a Simple Moving Average? - NinjaTrader Programming | futures io social day trading
futures io futures trading


How to code a Simple Moving Average?
Updated: Views / Replies:6,623 / 5
Created: by Fat Tails Attachments:2

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 
Thread Tools Search this Thread
 

How to code a Simple Moving Average?

  #1 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,652 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary

How to code a Simple Moving Average?

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:

 
Code
 
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(Input[0]);
else
{
if (CurrentBar >= Period)
{ 
double sum = 0;
for (int i = 0; i<Period; i++)
sum = sum + Input[i];
Value.Set(sum/Period);
}
else
Value.Set((Value[1]* Math.Min(CurrentBar, Period) + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}

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:

 
Code
 
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
Value.Set(Input[0]);
else
{
double last = Value[1] * Math.Min(CurrentBar, Period);
if (CurrentBar >= Period)
Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
else
Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
}
}
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?

Attached Thumbnails
How to code a Simple Moving Average?-lookback-5-days.jpg   How to code a Simple Moving Average?-lookback-65-days.jpg  
Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Asia
 
Futures Experience: Beginner
Platform: NinjaTrader, TOS
 
Posts: 798 since Jun 2009
Thanks: 109 given, 786 received


Check out TA-lib TA-Lib : Technical Analysis Library - Home

And read this thread: C# Sample for Moving Average

Particularly the last paragraph...

Reply With Quote
The following user says Thank You to MXASJ for this post:
 
  #4 (permalink)
Elite Member
Berlin, Europe
 
Futures Experience: Advanced
Platform: NinjaTrader, MultiCharts
Broker/Data: Interactive Brokers
Favorite Futures: Keyboard
 
Fat Tails's Avatar
 
Posts: 9,652 since Mar 2010
Thanks: 4,226 given, 25,601 received
Forum Reputation: Legendary

Hello this is MaximumBarsLookBack greeting!

Hi MAXSJ

Thanks for the information.


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

 
Code
 
MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
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.

Reply With Quote
 
  #5 (permalink)
Site Administrator
Manta, Ecuador
 
Futures Experience: Advanced
Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500
 
Big Mike's Avatar
 
Posts: 46,240 since Jun 2009
Thanks: 29,352 given, 83,223 received

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.

Mike

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.

Reply With Quote
The following user says Thank You to Big Mike for this post:
 
  #6 (permalink)
Elite Member
toronto, ontario
 
Futures Experience: Intermediate
Platform: ninja trader
Broker/Data: sterling, iqfeed
Favorite Futures: es, nq, 6e
 
bizman70's Avatar
 
Posts: 363 since May 2010
Thanks: 41 given, 140 received

converting indicators

how difficult or what would it take to convert ninja based indicators to the Ta-lib library

Reply With Quote

Reply



futures io > > > > > How to code a Simple Moving Average?

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Jigsaw Trading: TBA

Elite only

FuturesTrader71: TBA

Elite only

NinjaTrader: TBA

Jan 18

RandBots: TBA

Jan 23

GFF Brokers & CME Group: Futures & Bitcoin

Elite only

Adam Grimes: TBA

Elite only

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Moving Average sharmas Elite Automated Trading 62 April 9th, 2012 07:44 PM
Moving average Help yiman NinjaTrader Programming 4 July 29th, 2011 09:03 AM
New Moving Average arnaudlx The Elite Circle 10 January 28th, 2011 06:15 AM
Image:simple moving average 100.png Big Mike Trading Wiki Images 0 June 28th, 2010 08:56 AM
Simple moving average of volume Saroj NinjaTrader 0 July 14th, 2009 04:05 PM


All times are GMT -4. The time now is 06:40 PM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2017-12-13 in 0.10 seconds with 20 queries on phoenix via your IP 54.90.92.204