Declaring Variables 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.
Variables are integral to programming indicators. Think of a variable as a name such as Period that holds a value (or something we want to keep track of and reference later), such as 8 (the value does not have to be a number). The value could also be a string such as Variable, it could contain a decimal 8.5354, it could be True, or it could even be Null (there are more options, but let’s stick with the basics). Variables help us easily reference a number, or calculation and they come in all different varieties.
Common Data Types
When we create (declare) a variable, we can declare that variable as one of many different data types. I will present a few of the most common and as we progress, I will introduce more through the use of examples (even though the following list looks intimidating, the most common Data Types we use are int, double, and string):
Table Source: https://msdn.microsoft.com/en-us/library/ms228360(v=vs.90).aspx
Short Name | .NET Class | Type | Width | Range (bits) |
---|---|---|---|---|
byte | Byte | Unsigned integer | 8 | 0 to 255 |
sbyte | SByte | Signed integer | 8 | -128 to 127 |
int | Int32 | Signed integer | 32 | -2,147,483,648 to 2,147,483,647 |
uint | UInt32 | Unsigned integer | 32 | 0 to 4294967295 |
short | Int16 | Signed integer | 16 | -32,768 to 32,767 |
ushort | UInt16 | Unsigned integer | 16 | 0 to 65535 |
long | Int64 | Signed integer | 64 | -9223372036854775808 to 9223372036854775807 |
ulong | UInt64 | Unsigned integer | 64 | 0 to 18446744073709551615 |
float | Single | Single-precision floating point type | 32 | -3.402823e38 to 3.402823e38 |
double | Double | Double-precision floating point type | 64 | -1.79769313486232e308 to 1.79769313486232e308 |
char | Char | A single Unicode character | 16 | Unicode symbols used in text |
bool | Boolean | Logical Boolean type | 8 | True or false |
object | Object | Base type of all other types | ||
string | String | A sequence of characters | ||
decimal | Decimal | Precise fractional or integral type that can represent decimal numbers with 29 significant digits | 128 | ±1.0 × 10e−28 to ±7.9 × 10e28 |
Variable Examples
I get it, my last table didn’t make that much sense. I only included it as reference for later, but the best way to demonstrate variables is through an example, but before we do that, we need to open a NinjaScript Output window. Go to your Control Center and navigate to New -> NinjaScript Output. A new window should appear that looks like this:
When we edit the indicator from Part 1, we will Print text that will appear in the NinjaScript Output window. So, let’s look at some examples of how to declare variables:
Before you add the above to a chart and see what happens in the NinjaScript Output window, you need to save and compile (see Part 1). After your indicator compiles (sound will play and no errors appear) and you load the indicator on a chart, you should see this in your NinjaScript Output window:
It’s As Simple As That
Declaring variables is as simple as that. Now it’s time to checkout Part 3 – If Statements.
Leave a Reply