5.2.6Combining arrays

The FXB.ta library contains a helper function for combining the results from different arrays: FXB.ta.ArrayCombine(). For example, you can take an array of moving averages and an array of ATR values and add them together.

A simple example of ArrayCombine() is as follows:

// Subtract arr2 from arr1

var arr1 = [2, 4, 7, 8];

var arr2 = [5, 3, 7, 2];

var res = FXB.ta.ArrayCombine("subtract", arr1, arr2); // Returns [-3, 1, 0, 6]

ArrayCombine() takes three or more parameters: the operation to perform on the arrays, such as addition or subtraction, and two or more arrays to combine. The output of ArrayCombine() is an array of the combined results (or null if your inputs are not valid). If your input arrays have different lengths, then the length of the output is controlled by the first array you provide.

Valid operations to perform on the arrays are "add", "subtract", "multiply", "divide", "average", "minimum", "maximum", and "percent". (The "percent" mode divides the first array by the second array and multiplies by 100.)

Each "array" parameter can in fact be one of three things:

A simple array of values, as in the example above

A list of candles plus the value to use from each candle

A single fixed value

You define a candle input as follows - an object containing a candles[] array of the candle objects, plus a member defining which value to use from the candles.

{

member: "median", // Value to use from each candle, including derived properties

candles: [{o:1.2342, h: 1.2347, l: 12341, c: 1.2343}, …] // Array of candle data

}

For example, you can calculate the range - the difference between high and low - of an array of candles as follows:

// Array of candles to calculate from

var arrCandles = [{o:1.2342, h: 1.2347, l: 12341, c: 1.2343}, …];

// Subtract the low of each candle from the high of each candle - uses the same arrCandles

// for both parameters, but different members

var ranges = FXB.ta.ArrayCombine(

"subtract",

{member: "h", candles: arrCandles},

{member: "l", candles: arrCandles}

);

An input parameter can also be a {fixedValue: X}. For example, you can double all the values in an array as follows:

var arr1 = [2, 4, 7, 8];

var res = FXB.ta.ArrayCombine("multiply", arr1, {fixedValue: 2}); // Returns [4, 8, 14, 16]

Putting these examples together, you can do something such as calculating a moving average, calculating ATR, doubling the value of the ATR, and adding that to the moving average - which creates the upper band of a Keltner channel. For example:

// Calculate a moving average and ATR on an array of candles

var candles = [{o:1.2342, h: 1.2347, l: 12341, c: 1.2343}, …];

var ma = new FXB.ta.EMA({period: 20, data: candles});

var atr = new FXB.ta.ATR({period: 10, data: candles});

// Calculate double the ATR, using ArrayCombine()

var arrDoubleATR = FXB.ta.ArrayCombine("multiply", atr.GetValueArray(), {fixedValue: 2});

// Add double the ATR to the moving average, using ArrayCombine()

var arrKeltnerUpper = FXB.ta.ArrayCombine("add", ma.GetValueArray(), arrDoubleATR);