How To Use NinjaTrader To Test Simple Trading Strategies

by Mark Williams on February 9, 2009

My current trading platform that I am using is Ninjatrader. The enjoy the ease of use of the program, and the fact that this powerful little sumb**** of a program is free to use for charting and backtesting (but not for live trading).

Free. 100%. AND it’s powerful. You can’t beat that.

AND, it’s a lot better than some of other packages that I have played around with.

Anyway, the purpose of this article is give you a primer on how to create and back test strategies using this powerful program. So let’s get started.

First, our strategy:

The 3 Exponential Moving System 

  • This system uses 3 Exponential Moving Averages.

Entries are as follows:

If the Faster and Medium EMAs are above the Slowest EMA, then the only trades that are to be made will be long.

If the Faster and Medium EMAs are below the Slowest EMA, then the only trade that are to be made will be short.

A LONG trade will be made if the Faster EMA crosses Above the Medium Moving Average.

A SHORT trade will be made if the Faster EMA crosses Below the Medium Moving Average.

A LONG trade is exited when the Faster EMA crosses back below the Medium EMA.

A SHORT trade is exited when the Faster EMA crosses back above the Medium EMA.

My Variables are as follows:

  • Faster Period for EMAs
  • Medium Period For EMAs
  • SlowerPeriod For EMAs

3 Exponential Moving Average Example

Here is how we are going to make this happen in NinjaTrader:

Open Ninjatrader, and go to Tools ->New NinjaScript -> Strategy

Create a NinjaTrader Strategy

Hit Next:

Name and Description of NinjaTrader Strategy

From here, we are going to name the Strategy, and give a nice description of it.
Now, there is an option for calculate on bar close. What this means is that if your strategy is activated prior to the creation of the period bar that you are using (for example, the daily bar), then you will jump in immediately.

For example. Our FAST EMA line is about to cross the Medium EMA line. The current price of our security at this moment is 20 pts. If the security were to go to 30 pts, then the Fast EMA would cross the Medium EMA.

If we do not calculate our strategy on bar close, once the price crosses the 30pts line, we get in NOW, instead of waiting on the bar to finish up.

So, why would we ever set this for use on calculating the full bar?

Well, let’s say the current price does shoot above 30 pts, creating our cross of the Fast vs. Medium. If we have the calculate on bar close on, we are not going to do anything until the time period of this bar is over (in our example case, the market day).

So price shoots above 30pts, gives us our signal, but the bar is not done being calculated yet. Then, it crashes, and before the day is done, the bar’s current price drops to 15pts, and at the end of the day, we do not end up with a cross. Then the next day, the bar’s price is at 13pts, and it is continuing on a downward slope.

By calculating on bar close, we prevented ourselves from getting in to a losing trade. By not calculating on bar close, we got into the trade, but it didn’t work out as planned.

But, what if the price ended up at 50pts on the day?

If we calculated on bar close, we’d be getting in the trade at 50 pts. If we’d not calculated on bar close, we would have already been in the trade for a 30pts (unrealized) profit because we took the signal early.

Lets continue.

Now, we enter in the User Variables:
Trading Strategy User Variables

Once these are entered, we click on next and enter in our parameters.

New Strategy Basic Inputing of Logic

There are two spaces here. The top area is where we are going to put our logic statements. The bottom is where we put the actions that we want to accomplish.

So, the top area is where we let the program know, “hey, if everything in this box is true, do what ever is in the bottom box!”

When we click add, it opens up a menu where we select our parameters. So, we want to compare the 3 EMAs that we are using so, lets select them. We will start by adding our comparison of the Fast EMA vs. that of the Medium EMA:

Selecting the parameters of our strategy

We start by selecting EMA from the left pane.  Now, where it says PERIOD, instead of putting a static period, we are going to select our FAST EMA variable by selecting the 3 dots:

Selecting our Strategy parameters

And then selecting our parameter from the list.

Once we have done that, the logical operator is selected from the middle drop down list. You can select from the following:

  • Greater than >
  • Less than <
  • Greater than or equal to >=
  • Less than or equal to <=
  • Equals ==
  • Crosses Above
  • Crosses Below

Finishing up the First Logical Parameter

So, in our example, I want it so that if the Fast EMA “Crosses Above” the Medium EMA, we are going to do something.

Now that we are done with the first logic parameter, we are going to move on to the second one, or more comparisons if you want to add more.

Now it is time to add our actions…

Selecting the Actions that are to be done

Selecting the Action of going long!

Now we have completed our first set of rules for our trading system we are testing: “If the Medium Exponential Moving Average is above the Slower Exponential Moving Average, and the Faster Average Crosses Above the Medium Average, let’s buy!”

Done with Part one

Done. Now let’s hit the Set 2 tab to do this for our short trades:

Set 2

(Ignore the Variable1 =1. That was a mistake)

Now, let’s exit our positions via our rules. Select Set 3 and do the following:


So, instead of an indicator, we are now saying that if our market position is LONG or SHORT, then do X

Now we finish our LONG exit:

Our LONG exit criteria

And our Short Exit…

Exit our SHORT position

And we are done.

Next, we will go over actually running our strategy.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

{ 11 comments… read them below or add one }

1 JURAJ DANKONo Gravatar 07.23.09 at 8:30 pm

I am just looking for a similar system. I am very curious on the results. thanks

2 Gator-TraderNo Gravatar 08.26.09 at 12:34 am

I would really like to learn more.
Thank you

3 Juan RamosNo Gravatar 08.26.09 at 2:51 pm

How do you actually run this strategy on Ninja Trader? Please can you let me know. Thanks.

4 Erik HaugenNo Gravatar 11.07.09 at 4:50 pm

At the end of the strategy, I get a window opening showing the ninjascript and a list of errors. How can I resolve the errors? It doesn’t seem as if I can alter any of the script in the window. Some of the errors I am getting: CS1010, CS0116, CS1518, CS1026, CS1003, CS0101.

5 Erik HaugenNo Gravatar 11.07.09 at 5:00 pm

Okay got it to work.

6 markNo Gravatar 04.04.10 at 4:05 pm

Thanks for taking the time to do this – much appreciated!

7 NgchuanlingNo Gravatar 09.18.10 at 8:34 am

Dear Sir,
Thank you so much for the lesson!
BTW, how to program DONCHIAN CHANNEL?
Please show us….please.

8 JimNo Gravatar 12.03.10 at 12:59 am

This was very helpful. Thank you!

9 DaveNo Gravatar 03.23.11 at 6:58 pm

Very good how to. Wonder if this setup would work on ES futures with two different guides often used by pros…. buy and sell.
These have to do with MACD and Slow Stochastic indicators … when lines cross in a certain direction when over or under 50 line and it would be good to indicate when volume is up range for buy.

10 Tom LeesonNo Gravatar 03.31.11 at 9:02 pm

This is a very good article. What is missing in NinjaTrader training is really tuition for trades wanting to know more about optimising and backtesting without having to create a strategy. For that purpose all our NinjaTrader strategies are free for sim, test and some even free for live trading like a double ma crossover.

11 edNo Gravatar 05.14.11 at 2:04 pm

Are you still using NT?

I’m looking for somewhere to download some NT strategies.

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site is using OpenAvatar based on

Disclaimer | Privacy Policy © 2007-2008 YouSuckAtTrading.Com All Rights Reserved.