NinjaScript is an extension to the powerful Microsoft C# language. The following syntax guide is a subset of the capabilities of the C# language. For tutorials and an online reference to the C# language visit the overview page.
Statements
A statement is analogous to a complete sentence in the English language.
Example:
I like trading.
This is clear and easy to understand. If you only wrote:
I like.
Anyone reading this would not know what you like. This is not a complete sentence. The same logic applies to a C# statement, it forms a complete instruction that can be interpreted by the compiler.
|
sum = 10 + 10; |
The above statement is complete since we are saying that the variable sum is equal to 10 plus 10. Notice that the statement ends with a semicolon ";" and not a period. In English sentences end in periods, in C#, statements end with a semicolon.
Building Blocks
Building blocks are analogous to paragraphs in the English language in that they group statements together. In the programming world we call this a "Block of code". These blocks are delimited with curly braces "{}" like the following example:
|
if (x == 5)
|
The above code block encloses two statements in curly braces.
User Defined Comments
You can add your own comments to your code.
Use the "//" characters for single line comments.
Example:
|
// The following code encloses two statements with curly braces if (x == 5) { Print("NinjaTrader"); Print("NinjaScript"); } |
You can enclose several comment lines using the "/*" characters to start the comment block and then using the "*/" characters to end the comment block.
Example:
|
/* These are comments to illustrate a multi line comment block within NinjaScript */ |
Case Sensitivity
C# is a case-sensitive language which means that "NinjaTrader" with a capital "N" is not the same as "ninjaTrader" with a lower-case "n".
Variables and Value Types
A variable is a place holder that stores information into computer memory. A variable is unique analogous to your mailing address. Use variables to store and access data. There are many variable types that you can use in the C# language. Following are a few of the basics:
string
Stores textual data
double
Stores floating point values
integer
Stores whole number values
bool
Stores either true or false
object
Stores objects such as NinjaTrader indicators (in these cases, you would declare the variable type as the object type itself)
Declaring Variables
To declare a variable in C# you must first declare its data type and then provide a unique name and optionally assign a value.
|
// Declaring a string variable string myString = "NinjaTrader"; // Declaring a double variable
// Declaring an integer variable
// Declaring a bool variable
// Declaring an object type variable using a Simple Moving Average indicator
|
In each of the above examples you will notice that the equals character "=" is used to assign a value to the declared variable.
Operators
C# provides a large set of operators, which are symbols that specify which operations to perform in a statement. Following is a subset of common operators.
Arithmetic
|
+ |
addition |
|
- |
subtraction |
|
* |
multiplication |
|
/ |
division |
|
// Example of using arithmetic operators int myInteger = 0; myInteger = 5 + (3 * 4); Print(myInteger.ToString()); |
The above example would print a value of 17 to the NinjaTrader output window. The System.Math class provides additional math functions.
For example:
|
// Example of using the ABS method of the System.Math class double myDouble = Math.Abs(5 - 6); Print(myDouble.ToString()); |
Would print a value of 1 to the NinjaTrader output window. See a complete list of the Sytem.Math methods.
Logical
|
&& |
and also |
|
|| |
or else |
|
// Example of using logical operators int myInteger = 3; string myString = "NinjaTrader"; if (myInteger == 3 && myString == "NinjaTrader") { Print("true"); } |
The above example will print true to the NinjaTrader output window if the variable myString is equal to 3 and also (&& operator used) the variable myString is equal to NinjaTrader.
Relational
|
== |
is equal to |
|
!= |
does not equal |
|
< |
less than |
|
> |
greater than |
|
<= |
less than or equal to |
|
>= |
greater than or equal to |
|
// Example of using relational operator string myString = "NinjaTrader"; if (myString != "trading") { Print("Variable myString does not equal trading"); } // Second example of using a relational operator in conjunction with logical operator
|
Assignment
|
= |
equals |
|
+= |
x += y is equivalent to x = x + y |
|
-= |
x -= y is equivalent to x = x - y |
|
*= |
x *= y is equivalent to x = x * y |
|
/= |
x /= y is equivalent to x = x / y |
Conditional
?:
|
// Example of a conditional operator int myInteger = (10 > 12 ? 3 : 4); Print(MyInteger.ToString()); |
The above conditional statement says assign the value 3 to the variable myInt if 10 is greater than 12 else assign the value 4 to the variable myInt. The example will then print the value of 4 to the NinjaTrader output window since 10 is not greater than 12.
String Concatenation
To append one string to another string use the "+" character.
|
// Example of string concatenation string wordOne = "Ninja"; string wordTwo = "Trader"; Print(MyInteger.ToString()); |
The above example would print out NinjaTrader to the NinjaTrader output window.