This series focuses on NinjaScript and is designed to cater to both experienced programmers and beginners. Our goal is to cover the fundamentals of NinjaScript, equipping you with the knowledge needed to develop NinjaTrader add-ons. Please note that this article does not delve deeply into C# programming.
While NinjaScript simplifies many aspects of trading development, a basic understanding of C# is beneficial for creating more complex add-ons. We recommend referring to Microsoft's official documentation: https://learn.microsoft.com/en-us/dotnet/csharp/
Fundamentals
The essence of programming
A program is basically a set of instructions that solves problems using math and logic.
Imagine I give you the heights of 3 people and ask you to figure out their average. You’d just add up their heights and divide the total by 3. Simple, right? Something you can easily do in your head.
Now, let’s say there are 20 people instead. Same process, but you’d probably grab a pen and paper to keep track. Still pretty easy!
But what if I asked you to find the average height of everyone in a whole city? We’re talking about thousands of people - grouped by men, women, kids - and things get a lot trickier. At that point, you’d need software to do the heavy lifting for you.
That’s where programming comes in. Computers are crazy powerful, but they don’t understand human language. You need to talk to them in their language. Basically, programming means breaking the problem into clear steps, writing those steps in a procedure that the computer understands, and letting it do the rest. This process is what we call programming, and the result is a program.
Variables
One of the building blocks of programming is something called a "variable". So, what’s that all about?
Imagine you’ve got 2 dogs. One’s a 6-month-old golden retriever with light golden fur mixed with white, and the other’s also a golden retriever, but 1 year old with dark red fur. Now, when you’re talking to friends or family about them, are you really going to say, “My 6-month-old golden retriever with light golden fur mixed with white is growing so fast, nearly as tall as 1-year-old golden retriever with dark red fur”? Nope, that’d be exhausting. Instead, you’d just give them names:
Max: 6 months old, light golden fur mixed with white
Cara: 1 year old, dark red fur
Now the conversation gets way simpler: “My Max is growing so fast, nearly as tall as Cara!” Easy to remember, right?
That’s the whole idea behind variables in programming. You give something a name so it’s easier to refer to it and work with it later. Max will grow up and maybe change how he looks, but he’ll still be Max, and everyone will know who you mean. It’s the same with variables - they can hold different values as things change, but their name stays the same (at least until you decide to get rid of it).
Syntax
Syntax in programming is simply the grammar in literature.
Syntax is a set of rules that define the structure of the programming language. It is a standard that helps computers understand what you tell them to do. If you don’t follow the syntax, you will probably get a “Syntax Error” and your program will not work.
This is the basic of C# syntax which is used in NinjaScript:
namespace NinjaTrader.NinjaScript.Indicators
{
public class HelloWorldIndicator : Indicator
{
protected override void OnStateChange()
{
// ...
}
protected override void OnBarUpdate()
{
// ...
}
}
}
Don’t worry if you don’t understand them for now. We will dive deeper in these later.
Data types
Data type is a classification specifying the type of value stored in a variable. It also indicates what type of operation can be applied to it. Data Types are generally classified into two types:
Primitive:
Int (integer number): used for storing whole numbers, such as bar numbers or order quantities
Double (decimal number): used for storing decimal numbers, such as prices or indicator values
Boolean (true/false): used for storing true/false values, often used for conditions
…
Non-primitive:
You don’t need to remember all of them. You will get familiar quickly when getting into the programming activity in the next article.
Flow control structure
Sequential: Your code runs one line at a time, from top to bottom - just like following a recipe step by step..
Logical expression: These check if something is true or false and give you a result. For example, "If the dog has light golden fur, it's Max; otherwise, it's Cara". Simple logic!
Iteration: This means doing something over and over until a condition is met. It’s great for repetitive tasks. For instance, if you need to find the average height of citizens in 10 cities, you’d write a loop that runs 10 times. Each time, it would do the steps to calculate the average for one city.
Function
Functions (or Methods) in Programming is a block of code that encapsulates a specific task or related group of tasks, including one or many logical expressions, iterations…
You can think about functions as small modules in your car engine:
Each part of a car’s engine, like the pistons, crankshaft, or spark plugs, serves a specific role. Similarly, a function is a block of code designed to perform a specific task.
All car parts must work together in harmony to produce motion. Functions interact and collaborate to accomplice a larger task.
Car parts are designed to be replaceable & reusable in similar models. Functions are the same. They can be reused in different parts of a program.
A fuel injector takes input (fuel) and produces an output (a fine mist of fuel for combustion). Functions are similar, it takes inputs (called “arguments”) and produce outputs (return value)
Hello World
Simple version
One of the easiest and most common examples for anyone taking their first step into programming is displaying the text “Hello World” on the screen. So, let’s dive in and do that with NinjaScript to sum up the basics of programming.
Open NinjaScript Editor, click the plus (+) button at the bottom of the window, and select “New Indicator”

In the left menu, click “General”. On the right side, at “Name”, give your indicator a name.

You can skip the other menus for now, we will come back to them later. Go to the “Finish” menu and click the “Finish” button

This process, called the “NinjaScript Wizard”, will create a new template file for your indicator. Now, when you look at the code, don’t freak out! It might seem overwhelming at first, but let’s take it one step at a time. Just focus on the function called OnBarUpdate()
In C# & NinjaScript, a function has a name followed by parentheses - like OnBarUpdate()
- and it’s always followed by curly braces {}
to define the function's logic (called the “function body”).

OnBarUpdate()
is a default function in NinjaScript indicators, and it runs every time a new bar appears on the chart. Don’t delete it or change its name - otherwise, your program won’t work!
Now we will create our very first variable. Find the line that says // Add your custom indicator logic here
and replace it with this:
string text = "Hello World";
Make sure you add a semicolon at the end - it tells the computer that the line is complete. Here’s the breakdown:
string
is the type of variable
text
is the name of the variable
“Hello World” is the value assigned to it.
That’s the basic syntax of a variable.

Next, let’s display that variable. You will need to use another default function of NinjaScript Print()
. Add this line right below the one you just wrote:
Print(text);
Keep in mind, C# is case-sensitive. That means Print()
with a capital “P” works, but print()
with a lowercase “p” won’t. Your code should now look like this:

You can press the F5 on the keyboard (or right-click to open the context menu and select “Compile”). This step is mandatory. It tells NinjaScript that you are done coding and want to register the indicator in the NinjaTrader system for testing.
Now go to the Control Center => New => Chart to open a new chart. Right-click the chart to open the context menu => select Indicators… (or press Ctrl + I) to open the Indicators dialog. Find your indicator name on the left list & add it to the chart.

Finally, go to Control Center => New => NinjaScript Output to open the output dialog. Now go back to your chart & hit F5 to refresh it. You will see a lot of “Hello World” printed line by line in the output dialog.
Congratulations - you just created your very first program!

Level up Hello World
You might be wondering why you only wrote one line with the Print()
function, but you’re seeing tons of “Hello World” lines printed. Don’t worry, that’s just how NinjaScript works!
The OnBarUpdate()
function gets triggered every time a bar (or candle) on the chart updates. So if your chart has 1000 candles, it’ll call the function 1000 times.
If you want the text to print just once, we can fix that using a logical expression - a condition that checks if it’s the first bar on the chart. If it is, we print the text. If not, we skip it.
There’s a default variable in NinjaScript called CurrentBar
. It’s a number that represents the index of the current bar. For example, if the chart has 100 bars, the very first bar (on the far left) is number 0. Each bar to the right is numbered incrementally - 1, 2, 3, and so on.
Here’s what you’ll add to the program:
if (CurrentBar == 0)
{
string text = "Hello World";
Print(text);
}
Here’s the breakdown:
CurrentBar == 0
checks if the current bar’s number is zero, meaning the first bar.
The double equals sign (==
) is used to compare values in C# programming. If the condition is true (i.e., it’s the first bar), the code inside the curly braces runs, printing “Hello World”. If it’s false, nothing happens.
Now press F5 to compile your code. Then, go back to the NinjaScript Output window, right-click, and select Clear to remove all the previous printed text. Refresh the chart with F5 again, and you’ll see just one “Hello World” line in the output.

Next
Nice work! You're all set to start building your own NinjaScript add-ons. In the next article, I’ll walk you through creating your first moving average SMA, customized with your own algorithm.
If you run into any trouble, feel free to leave a comment. We’re here to help and can’t wait to see you dive into building awesome NinjaTrader tools!
Appendix
NinjaScript for Beginner
- Article #1: Introduction
- Article #2: Core Programming Concepts (this article)