NinjaScript > Educational Resources > Basic Programming Concepts > Basic Syntax |
|
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.
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:
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:
You can enclose several comment lines using the "/*" characters to start the comment block and then using the "*/" characters to end the comment block.
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.
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
The above example would print a value of 17 to the NinjaTrader output window. The System.Math class provides additional math functions. For example:
Would print a value of 1 to the NinjaTrader output window. See a complete list of the Sytem.Math methods.
Logical
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
Assignment
Conditional
?:
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.
The above example would print out NinjaTrader to the NinjaTrader output window. |