5.1.3Events in the candle store

One of the key benefits of the candle store is that it fires events when candle data is loaded. This is particularly useful on streaming rather than static candle requests, because it helps you to differentiate between updates of the current candle versus the start of a new candle.

Note: loading in UDI bar data always fires either OnLoad() or OnNewCandle(). It does not fire OnNewCandle() because the UDI always provides either the single current bar or the whole bar data, not the addition of a new bar.

Event

Description / type of change

OnCurrentCandleChange()

Called when only the current, in-progress candle is changing

OnNewCandle()

Called when a new, later candle has been added to the store. What was previously the current candle may also have received a final update as part of the same change, without firing OnCurrentCandleChange()

OnLoad()

Any other type of change to the candle store. Mainly initial load, but can also be called if a block of additional old candles is later added to the store. In other words, OnLoad() is a notification that the candle data has fundamentally changed, and anything based on it needs to be recalculated.

OnLoadError()

Special case: called if you assign the candle store as the output for RequestCandles(), and there is a problem with your candle request

OnUpdate()

Called on any type of change to the candle store - (re-)load, new candle, update of current candle. Designed for callers which only need a notification that the store has changed, regardless of the type of change. The more specific handler such as OnNewCandle() or OnLoad() is called immediately before OnUpdate().

The candle store object is passed as a parameter to all event handlers. For example:

myCandleStore.OnLoad = function(candleStore) {

// Store can be accessed either via the myCandleStore variable

// or via the candleStore parameter to the event handler

};

You can assign event handlers after the candle store has been created. For example:

// Create candle store

var myCandleStore = new FXB.CandleStore();

// Assign a candle request directly into the store

Framework.RequestCandles({…}, myCandleStore);

// Assign event handlers

// Note: candle requests always return asynchronously, and therefore it's safe to assign

// the event handlers immediately after RequestCandles(), rather than doing so before.

myCandleStore.OnLoad = function(candleStore) { … };

myCandleStore.OnNewCandle = function(candleStore) { … };

You can also, instead or as well, provide the event handlers in the initialisation of the candle store. For example:

var myCandleStore = new FXB.CandleStore({

OnLoad: function() { … },

OnCurrentCandleChange: function() { … },

etc

});