5.2.3.1Loading candle data into a calculation

As described above, calculations can be either bar-based or value-based. For bar-based calculations, what you pass to LoadData() can be one of two things:

An array of candles: objects containing an o, h, l, c, and v (optional, defaults to zero).

An object containing a barData object which in turn contains separate arrays for the open, close etc: open[], close[], etc. This option means that you can pass in the data object from a user-defined indicator's UDI.onCalculate() function.

For example, to load an array of candle objects:

myATR.LoadData([{o:1.2342, h: 1.2347, l: 12341, c: 1.2343}, … ]);

Or, to load in a barData object with the candle values in multiple separate arrays:

myATR.LoadData({

valueCount: 500, // Compulsory: number of items in the arrays

barData: {

open: [1.2342, 1.2340, 1.2345, …],

high: [ …],

low: [ … ],

close: [ … ],

volume: [ … ] // Volume is optional, and defaults to zero if omitted

}

});

Similarly, in a bar-based calculation, what you pass to UpdateCurrent() must also be a candle object, or an object containing barData:

// When the current candle changes, use UpdateCurrent() with the single current candle object

myATR.UpdateCurrent({o:1.2342, h: 1.2347, l: 12340, c: 1.2340});

With a barData object:

// In UpdateCurrent(), only the first entry in each array is used

myATR.UpdateCurrent({

barData: {

open: [1.2342, …],

high: [ …],

low: [ … ],

close: [ … ],

volume: [ … ] // Volume is optional, and defaults to zero if omitted

}

});

However, there is a further refinement. LoadData() understands the currentBarUpdateOnly value supplied by a UDI, and automatically turns LoadData() into a faster call to UpdateCurrent() where applicable. For example:

UDI.onCalculate = function(data, output) {

// Create an ATR calculation if we don't already have one

if (!UDI.$myATR) UDI.$myATR = new FXB.ta.ATR({period: 14});

// Pass the data into the ATR calculation.

// If data.currentBarUpdateOnly is set then the calculation does a fast call to UpdateCurrent()

// rather than a full reload of all the data

UDI.$myATR.LoadData(data);

};