OnBarUpdate()

View as Markdown

Definition

An event driven method which is called whenever a bar is updated. The frequency in which OnBarUpdate is called will be determined by the “Calculate” property. OnBarUpdate() is the method where all of your script’s core bar based calculation logic should be contained.

Notes:

  • For multi-timeframe and instrument scripts, the OnBarUpdate method is called for each Bars object of a strategy. You MUST filter for the exact bar update events using the “BarsInProgress” property you want your system logic to execute against.
  • Hosted indicators will need to be accessed by the hosting script to ensure OnBarUpdate functionality. This can be done by:
    1. Calling Update on the hosted indicator within the host script,
    2. Including a plot in the hosted indicator and accessing the plot in the host script,
    3. Including a plot in the hosted indicator and adding the indicator to the chart with AddChartIndicator (strategies only)
MethodDescription
BarsPeriodThe primary Bars object time frame (period type and interval).
CalculateDetermines how often OnBarUpdate() is called for each bar.
CountThe total number of bars or data points.
CurrentBarA number representing the current bar in a Bars object that the OnBarUpdate() method in an indicator or strategy is currently processing.
IsDataSeriesRequiredDetermines if a Data Series is required for calculating this NinjaScript object.
IsFirstTickOfBarIndicates if the incoming tick is the first tick of a new bar.
IsResetOnNewTradingDaysDetermines if the specified bar series is using Break at EOD.
IsTickReplaysIndicates the specified bar series is using Tick Replay.
Update()Forces the OnBarUpdate() method to be called so that indicator values are updated to the current bar.

Method Return Value

This method does not return a value.

Syntax

You must override this method with the following syntax:

protected override void OnBarUpdate()

Tip: The NinjaScript code wizards automatically generates the method syntax for you.

Parameters

This method does not take any parameters.

Examples

1protected override void OnBarUpdate()
2{
3 if (CurrentBar < 1)
4 return;
5
6 // Compares the primary bar's low price to the 5-minute bar's low price
7 if (Low[0] > Lows[1])
8 Print("The current bar's low price is greater");
9}