NinjaScript > Educational Resources > Basic Programming Concepts >

Basic Syntax

Print this Topic Previous pageReturn to chapter overviewNext page

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)
{
    Print("NinjaTrader");
    Print("NinjaScript");
}

 

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. The 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
double myDouble = 2.5;

 

// Declaring an integer variable
int myInteger = 1;

 

// Declaring a bool variable
bool learningIsFun = true;

 

// Declaring an object type variable using a Simple Moving Average indicator
SMA mySMA = new SMA(Close, 20);

 

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. The 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
double myDouble = 1000.25;

 
if (myDouble < 1000 || myDouble > 1001)
{

    // Note that since myDouble does not satisfy the if-statement, no printout will be seen
    Print("Variable myDouble is between 1000 and 1001");
}

 

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(wordOne.ToString() + wordTwo.ToString());

 

The above example would print out NinjaTrader to the NinjaTrader output window.