Getting Started with NinjaScript
Brief Introduction
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)!
NinjaScript Editor
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.
Create Indicator
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:
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.
Where in the indicator do I paste the code? I tried 2 different places but both result in errors and refuse to compile until the errors are corrected. Thanks.
After you click generate and the NinjaScript Editor appears, copy and paste the code in this post over all the code that is in the NinjaScript Editor and compile (I just tried this on 8.0.15.0 and had no issues).
Thank you! I hadn’t considered the possibility that the instructions were to REPLACE existing code with the code they wanted pasted in.