Creating the Strategy via Self Programming

View as Markdown

If you have not done so already, press the “Unlock Code” button within the wizard to launch the NinjaScript Editor.

The OnBarUpdate() method is called for each incoming tick or on the close of a bar (user defined) when performing real-time calculations. Therefore, this is the main method called for strategy calculation and we will use this method to enter the script that checks for entry and exit conditions.

The Entry and Exit Condition

Enter the code contained within the OnBarUpdate() method in the image below into the OnBarUpdate() method in the NinjaScript Editor:

1protected override void OnBarUpdate()
2{
3 if (CrossAbove(SMA(Fast), SMA(Slow), 1))
4 EnterLong();
5
6 if (CrossBelow(SMA(Fast), SMA(Slow), 1))
7 EnterShort();
8}

Translated into English, the code contained within the OnBarUpdate() method above reads:

if the fast simple moving average crosses above the slow simple moving average within the last bar, go long

if the fast simple moving average crosses below the slow simple moving average within the last bar, go short

To accomplish this we used the following methods:

  • CrossAbove - Checks for a cross above condition and returns true or false
  • CrossBelow - Checks for a cross below condition and returns true or false
  • SMA - Returns the value of a simple moving average
  • EnterLong - Enters a market order long
  • EnterShort - Enters a market order short