5.2.5Indexing order and oldestFirst

As described in the previous section, the default for FXB.ta calculations is that output is indexed with newest values first: the current, latest result is #0. This is simply because a test such as GetValue(0) > GetValue(1) is a less cumbersome and more readable way of checking the current versus previous value than a test such as GetValue(length - 1) > GetValue(length - 2).

This also means that you should load data newest-first. For example:

// The 1.2349 value in array #0 should be the most recent value, not the oldest value

myEma.LoadData([1.2349, 1.2368, 1.2392, 1.2347, … ]);

However, if you prefer, all FXB.ta calculations can be changed so that they are indexed the other way round, with the oldest value in #0 and the newest value in length-1.

You do this by setting oldestFirst:true, either in the initialisation of the calculation or by changing the oldestFirst property at any later point. For example:

// Create a calculation with oldestFirst turned on

var myEma = new FXB.ta.EMA({period: 20, oldestFirst: true});

// Data should now be loaded oldest-first, not newest-first

myEma.LoadData([ …, …, …, 1.2347, 1.2392, 1.2368, 1.2349]);

// Because of oldestFirst, GetCurrentValue() is now equivalent to GetValue(length-1),

// not to GetValue(0)

var currentValue = myEma.GetCurrentValue();

At the risk of creating considerable potential confusion and bugs for yourself, it's possible to load data newest-first (because that is how the framework provides it by default) and then to change the indexing to oldest-first before reading the results:

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

// oldestFirst is not yet turned on, and data should be loaded newest-first

myEma.LoadData([1.2349, 1.2368, 1.2392, 1.2347, … ]);

// Turn on oldestFirst, after loading

myEma.oldestFirst = true;

// Because of oldestFirst, the current latest item is in #length-1

var currentValue = myEma.GetValue(myEma.length - 1);

// And GetCurrentValue() is now equivalent to GetValue(length-1), not to GetValue(0)

var alsoCurrentValue = myEma.GetCurrentValue();