Entering Calculation Logic
Entering Calculation Logic
The OnBarUpdate() method is called for each incoming tick, or on the close of a bar (if enabled) when performing real-time calculations, and is called on each bar of a Bars object when re-calculating the indicator (For example, an indicator would be re-calculated when adding it to an existing chart that has existing price data displayed). Therefore, this is the main method called for indicator calculation and we will use this method to enter the script that will calculate a simple moving average.
Are there enough bars?
Enter the following code into the OnBarUpdate() method in the NinjaScript Editor:
To calculate a 20 period moving average you will need a minimum of 20 bars of data. The first statement in our OnBarUpdate() method checks to see if there are enough bars of data to perform the moving average calculation. CurrentBar returns the index number of the current bar and this is checked against the user-defined parameter Period. If the current bar number is less than the user-defined period we “return” which skips calculating the moving average.
Getting a sum of closing prices
Enter the following code into the OnBarUpdate() method and below the code snippet you entered above:
First we must declare a variable that will store our sum total.
The variable sum whose value is of type double will serve as temporary storage.
Next we must calculate the sum. We use a standard “for” loop to skip through prices and add them to the sum variable. Although the command that represents the loop may look intimidating, it’s really quite simple. Let’s look at it in English…
What the loop is saying is:
- the number of bars ago is now zero
- as long as the number of bars ago is less than the moving average period, then go to line 3 otherwise this loop is finished
- get the price Input[number of bars ago] and add it to the running sum total
- add one to the number of bars ago (if number of bars ago was zero it will now be one)
- go to line 2
You can find more information on how loops work here. Once the loop has finished, it will have calculated the total sum of closing prices for the period of our moving average.
- We use the value of Input[barsAgo] to get a price to use for our calculation. We could have substituted Close[barsAgo] to use closing prices or High[barsAgo] to use high prices. The reason we use Input[barsAgo] is since this allows flexibility for what the indicator is calculated based off of. Remember users have the option to select a price type (High, Open, Close etc…) from the Indicator Dialog window.
The final calculation
Enter the following code into the OnBarUpdate() method and below the code snippet you entered above:
We can now calculate the final moving average value and assign its value to the property that represents the plot data. We have just finished coding our simple moving average. The class code in your editor should look identical to the image below. You are now ready to compile the indicator.
Alternate Implementation
In this tutorial we are using a “for” loop to iterate through a collection of prices and accumulate a sum value. We chose this approach to demonstrate the use of a loop. A simple moving average can actually be expressed in a more efficient manner using the built-in SUM indicator as shown below.

