5.2.4Reading calculation output

All calculations have a primary output value, but some calculations produce other secondary values as well. For example, the Stochastic Oscillator generates not only a primary value but also a "signal" value (typically represented on a chart as a dotted or dashed line).

All classes in the FXB.ta library have some standard functions for reading their primary values:

Function

Description

GetValueArray()

Returns an array of all the primary output values

GetValue(idx)

Returns a single output value, using a zero-based index. By default, calculations are indexed newest-first, and #0 is the current, latest value of the calculation. Returns null if the index is not valid (< 0 or >= length).

GetCurrentValue()

Shortcut for GetValue(0)

GetLength()

Get the number of output values. Should always match the number of items in the array passed to LoadData(), unless LoadData() failed and returned false

length

Length property; alternative and equivalent to GetLength() function

HasData()

Shortcut for checking whether a calculation has been initialised. Equivalent to GetLength() > 0

hasData

Has-data property; alternative and equivalent to HasData() function

For example, testing whether two moving averages have crossed over since the previous bar:

// Create two moving averages for different periods and load the same data into them

var ma1 = new FXB.ta.EMA({period: 20});

var ma2 = new FXB.ta.EMA({period: 50});

ma1.Load(myDataArray);

ma2.Load(myDataArray);

// Compare the moving averages in the current bar versus the previous bar

if (ma1.GetValue(0) > ma2.GetValue(0) && ma1.GetValue(1) < ma2.GetValue(1)) {

// MA 1 has crossed above MA 2 during current bar (is now > and was <)

} else if (ma1.GetValue(0) < ma2.GetValue(0) && ma1.GetValue(1) > ma2.GetValue(1)) {

// MA 1 has crossed below MA 2 during current bar (is now < and was >)

} else {

// (No cross)

}

As described above, the number of output values will always be the same as the number of input values in the array passed to LoadData(). But some of the output values may be - usually, will be - null. For example, if you are calculating a 20-period simple moving average, no average can be calculated for the first 19 values. If you supply an array of 1000 input values, then GetLength() on the output will be 1000 but the oldest 19 values - from GetValue(981) to GetValue(999) - will be null.

For calculations which produce more than one output, the details are described below in relation to each calculation. But there is some standard functionality where a secondary value is returned via GetSignalArray(), GetSignalValue(), and GetCurrentSignalValue().

Function

Description

GetSignalArray()

Returns an array of all the secondary, "signal" output values

GetSignalValue(idx)

Returns a single signal value, using a zero-based index

GetCurrentSignalValue()

Equivalent to GetSignalValue(0)

Some calculations then have further functions for retrieving their output. For example, the Keltner and Bollinger band calculations have GetUpper(idx) and GetLower(idx) functions for returning their upper and lower band values.