5.1.6Technical analysis calculations in the candle store

A candle store lets you set a list of any technical analysis calculations from the FXB.ta library, and have these automatically updated when the candle data changes.

For example, you can initialise the candle store with a moving average and ATR. These calculations then automatically update whenever candles are loaded into the store (and before any event handlers are called):

var myCandleStore = new FXB.CandleStore({

// Automatically add a moving average and ATR

ta: [

new FXB.ta.EMA({period: 20, member: "h"}), // EMA of high (.h) price

new FXB.ta.ATR({period: 14}) // ATR

],

… further initialisation settings

You can set up technical analysis calculations in two ways. You can provide a ta[] array in the initialisation of the store, like the example above. It's also permissible to pass a single item rather than an array:

var myCandleStore = new FXB.CandleStore({

// Single TA object

ta: new FXB.ta.EMA({period: 20, member: "h"}), // EMA of high (.h) price

Or, at any time after creation of the store, you can use the AddTA() function:

myCandleStore.AddTA(new FXB.ta.RSI({period: 30}));

myCandleStore.AddTA(new FXB.ta.Stochastic({kPeriod: 5, dPeriod: 3, slowing: 3}));

Instead of providing an instance of the analysis class, such as new FXB.ta.ATR(), in the store's initialisation or in a call to AddTA(), you can instead pass a definition containing the parameters for the calculation plus an indicatorType value. The indicatorType should be the class within FXB.ta such as DEMA or ATR or Envelope. For example:

myCandleStore.AddTA({indicatorType: "Stochastic", kPeriod: 5, dPeriod: 3, slowing: 3});

// Has the same effect as…

myCandleStore.AddTA(new FXB.ta.Stochastic({kPeriod: 5, dPeriod: 3, slowing: 3}));

You can subsequently read the calculations using the candle store's ta[] array. The order of items within this is simply the order in which you added them. For example:

var currentRSI = myCandleStore.ta[0].GetCurrentValue();

var currentStochastic = myCandleStore.ta[1].GetCurrentValue();

You can add calculations, using AddTA(), after loading candle data as well as during the initial set-up of the store. And it's safe to modify the store's ta[] array directly to remove or even replace calculations.

As a service to make your code easier to read, there is a further way of addressing technical analysis calculations. When creating a calculation you can include an alias property, such "ema20":

myCandleStore.AddTA(new FXB.ta.EMA({period: 20, alias: "ema20"}));

You can then refer to calculations via their alias using a $ta() function in the candle store, making the code easier to read than referring to the calculation via a numeric index such as [1]. For example:

// Calculations can always be addressed via their numeric index, in order of creation…

var currentEma = myCandleStore.ta[1].GetCurrentValue();

// But the code becomes more readable if the access is given a name rather than a number…

var currentEma = myCandleStore.$ta("ema20").GetCurrentValue();