Order of the calls
1
OnSet
Called before every load of the chart, and again when the user changes a parameter. Here the indicator says which calls it wants, enables or disables its series and sets its description. The parameters already have their values.
2
OnPreInit, OnInit
Called once per load, before the series exist. The axes (
AxisX, AxisY) are available from here: set their options in OnInit.3
OnLoad
Called when the series have been created and before the first bar. Reset here everything a previous calculation left: counters, lists, annotations you keep a reference to.
4
History
For every bar already on the chart, in order:
OnOpen, then OnClose when the bar is complete. The last bar is still open, so it gets OnOpen only. When all the bars are done, OnEnd(false) is called once.5
Realtime
For every trade,
OnTick. When a bar ends, OnClose for it and OnOpen for the new one. After each batch of updates, OnEnd(true): the place to update what depends on the bar still open.6
OnDispose
Called when the indicator is removed from the chart.
VAn.BarIndex is the index of the bar being calculated, and VAn.BarVars holds the bars up to it.
Choosing the calls
A call is made only when the indicator asks for it inOnSet, with one property per method:
Ask only for what you use: an indicator which asks for
OnTick on the history makes every chart load slower.
Which method for what
A common pattern, used by several examples, needs only two calls: in
OnOpen add the point of the new bar and finish the previous one, in OnEnd update the last one.
Recalculation
When the user changes a parameter, the chart callsOnSet and calculates the indicator again from OnLoad. Nothing you kept in fields is cleared for you: OnLoad must bring the indicator back to its initial state.