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. It is also the only place to ask for other resolutions.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 by the server when the calculation of the indicator stops. The desktop chart does not call it: do not rely on it to release anything.
VAn.BarIndex is the index of the bar being calculated. Read VAn.BarVars only up to it: on the server, during the history, the following bars can already be there (see Market data).
On the server every call has a time limit, see Desktop or server.
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.