Creating the Strategy via Self Programming
<< Click to Display Table of Contents >> Creating the Strategy via Self Programming |
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 check for entry and exit conditions.
Enter the code contained within the OnBarUpdate() method in the image below into the OnBarUpdate() method in the NinjaScript Editor:
protected override void OnBarUpdate() |
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