Calculation cadence
Calculation cadence
Choose how often OnBar fires (once per closed bar, on every price change, or on
every tick) and tell a close from an intra-bar update.
Calculation cadence
The Calculate property sets how often OnBar runs. Set it in
OnInitialize. It uses the same CalculationMode enum and the same default as an
indicator's Calculate. The default, OnBarClose, fires once per completed bar and
suits most strategies: it is cheaper, deterministic, and free of noise from a bar that is still forming.
| Mode | When OnBar fires |
|---|---|
CalculationMode.OnBarClose (default) |
Once per closed bar. |
CalculationMode.OnPriceChange |
At each close, and intra-bar whenever the developing bar's close moves. |
CalculationMode.OnEachTick |
At each close, and on every intra-bar tick. |
C#using SabrTrader.Pipeline.Indicators; // CalculationMode
protected override void OnInitialize()
{
Calculate = CalculationMode.OnEachTick;
}
The closing OnBar for a bar fires exactly once in every mode. An intra-bar mode adds
callbacks and never removes one, so logic written for OnBarClose still gets its per-close call.
Telling a close from an intra-bar update
Under OnPriceChange and OnEachTick the same OnBar hook fires inside
the forming bar and at its close, and during an intra-bar call CurrentBar is the developing
bar. Two flags tell the calls apart:
-
IsBarClosed: true on a closing callback (always true underOnBarClose), false during an intra-bar update. Gate once-per-bar work on it. -
IsFirstTickOfBar: true on the first intra-bar tick of a newly opened bar, false on a closing callback. Run "new bar opened" logic once with it.
C#protected override void OnBar()
{
if (IsFirstTickOfBar) { /* a new bar just opened */ }
// intra-bar: react to the developing close
if (!IsBarClosed) { TightenStopIfNeeded(CurrentBar.Close); return; }
// closed bar: the once-per-bar entry logic
EvaluateSignal();
}
TryGetOrderFlowBar(0, …) returns false during an intra-bar call: only closed bars carry a final
order-flow snapshot.
OnBar only, so the strategy runs at close cadence.
Calculate at OnBarClose unless an
entry or exit truly cannot wait for the bar to close; it keeps backtests fast and results reproducible. An
attached ATM advances break-even and trailing once per closed bar, whatever the cadence.