NinjaScript > Educational Resources > Tutorials > Indicators > Beginner - Using price variables >

Entering Calculation Logic

Print this Topic Previous pageReturn to chapter overviewNext page

The OnBarUpdate() method is called for each incoming tick or on the close of a bar (user defined) when performing real-time calculations and is called on each bar of a data series 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 check if the open price is equal to the close price.


The Comparison

Replace the wizard generated code with the following code into the OnBarUpdate() method in the NinjaScript Editor:


// Set the value
Plot0.Set(Open[0] == Close[0] ? 1 : 0);


Here we are doing a comparison of the current open price Open[0] and the current close price Close[0] using a conditional operator "?" and setting a value to the "Plot0" property via the Set() method. It might look a little intimidating but it is actually quite simple.


The syntax for a conditional operator is the following:


Expression ? value 1 : value2


Translated to English it is saying:


if the expression is true ? use value 1 : otherwise use value 2


translated one last time to our indicator logic:


if the current open price is equal to the current close price ? use a value of 1 : other wise use a value of 0


Our indicator then plots a value of 1 when we have a bar with the open price equal to the close price or a value of 0 otherwise. The OnBarUpdate() method in your editor should look identical to the image below.