Getting Started with NinjaScript
We’ve put together a series of posts to help you in your journey to learn how to program from scratch, or edit existing NinjaTrader indicators and strategies. All of our posts are geared toward the non-programmer, so even though we will present a lot of information, for all of you more technical people out there, we will not go into all the technicalities of C#, NinjaTrader, or NinjaScript. It’s also important to note that even though we show you how to do something in NinjaTrader, that is by no way the only way to do it. We will present what has worked best for us and what will be most easily understood by someone learning how to program. Finally, for the more advance users, we are going to leave things out and over simplify certain parts because we are focused on learning what we need to so we can get started on our NinjaScript journey.
NinjaScript is NinjaTrader’s programming language and is based on C#. What that means is most C# concepts can be applied directly to NinjaTrader, but there are also built-in Methods that are unique to NinjaTrader. This may sound overwhelming and you may start thinking you need to learn C#, but I can assure you that to get started programming NinjaScript, you do not need to learn C# to code a lot of great indicators. All you need to do, for now, is stick with learning through our posts and as concepts come up that you don’t fully understand, do research on your own (I am continually doing research, even years later)!
All of our development will occur inside of NinjaTrader (even though you could do this through other programs), so first start NinjaTrader 8 and on the Control Center go to New -> NinjaScript Editor. You should now have a new window appear that says “NinjaScript Editor” in the top left hand corner.
Now it is time to create an indicator, so in the NinjaScript Editor, right-click on the folder (on the right) called “Indicator” and click New Indicator. A new window should appear that looks like this:
Click the Next button and then fill out the indicator Name and Description, as follows:
Once you have filled out your Name and Description, click Next and fill out the settings like this (we will get into the details behind the below options in a later post):
Finally, click Generate and our new indicator will appear in the NinjaScript Editor as follows:
Basic Indicator Layout
It’s time to look at the basic layout of all (almost all) NinjaTrader indicators. In most of the examples, I will post code that you can easily copy and paste into your NinjaScript Editor. Also, a lot of my explanation will occur as “comments” in the code itself. Copy and paste the below code into your indicator and see what happens:
// We will ignore this section for now
#region Using declarations
// The namespace contains a set of related objects
// Do not change the name space
// It also tells us what we are working on is an "Indicator"
// If we were working in a strataegy, the namespace would be NinjaTrader.NinjaScript.Strategies
// Class "MyFirstIndicator : Indicator" starts with the indicator name
// and then the ": Indicator" tells the MyFirstIndicator to
// inheret variables and methods from "Indicator"
// In simple terms, by using ": Indicator" we can use methods that
// NinjaTrader has built into the base class "Indicator"
// We will focus on putting all of our code (there are exceptiosn to this)
// inside the brackets of the class "MyFirstIndicator : Indicator"
public class MyFirstIndicator : Indicator
// OnStateChnage is covered in a different post
protected override void OnStateChange()
if (State == State.SetDefaults)
Description = @"The description of the indicators purpose goes here. My first indicator will demonstrate several basic NinjaScript concepts. ";
Name = "MyFirstIndicator";
Calculate = Calculate.OnBarClose;
IsOverlay = true;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
else if (State == State.Configure)
// OnBarUpdate is called based on the option Calculate, which
// is set in State.Defaults and then set by the user in the
// normal indicator settings
// This is where our indicator logic will go
protected override void OnBarUpdate()
// When we use "//" we are creating a comment
// that won't be read / processed by NinjaTrader
/* You can also write comments like this */
Save and Compile
The last thing to do for Part 1 is to Save your work and Compile. There are several ways you can save, Ctrl + S, right-click and choose save, or in the toolbar there is a save icon. Once your indicator is saved, right-click on the indicator editor and choose compile, or choose compile from the toolbar (has a “>” in the icon). When you compile your indicator, NinjaTrader will check the code for errors (errors can still occur once we place the indicator on the chart), but after the code is checked for errors, NinjaTrader will go through its internal process(es) so you can choose the indicator from the indicator window (when you are adding indicators to a chart) and reference the indicator from another indicator. Checkout Part 2 – Declaring Variables.