NinjaScript > Educational Resources > Tutorials > Indicators > Intermediate - Historical Custom Data Series > Entering Calculation Logic |
|
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 do our calculations.
Creating DataSeries Object 1. Declare a variable ("myDataSeries" used in this example) of type DataSeries that will hold a DataSeries object within the region "Variables" 2. Create a new DataSeries object and assigning it to the DataSeries variable within the Initialize() method
Storing calculations in the DataSeries object Enter the following code into the OnBarUpdate() method:
The value of a DataSeries object will be aligned with the current bar. This means that all DataSeries objects will be synced with the CurrentBar index. It allows you to store a double value that corresponds with every bar.
Using DataSeries values
To plot our final calculation we will store the calculation in our Plot0. 1. Remove the wizard generated code in the OnBarUpdate() of “Plot0.Set(Close[0]);” 2. In the OnBarUpdate() method add the following code snippet
|