5.2.7Moving averages

The FXB.ta library provides several classes for creating different types of moving average, such as FXB.ta.EMA and FXB.ta.DEMA, listed below (and also widely used in the examples above).

However, the library also provides a way of creating moving-average calculations where the type of moving average is supplied as a variable, using the FXB.ta.CreateMovingAverage() function.

CreateMovingAverage() takes two parameters: the type of moving average, and the usual initialisation parameters which are then passed into the moving-average calculation in the normal way. For example:

var maType = "ema";

var parameters = {period: 30, member: "typical"};

// Create a type of moving average via the maType variable.

// Ends up being equivalent to: someMA = new FXB.ta.EMA(parameters);

var someMA = FXB.ta.CreateMovingAverage("ema", parameters);

Some calculations in the FXB.ta library use moving averages internally, and let you change the type of average by setting an initialisation parameter. For example, the Bollinger band calculation defaults to using a simple moving average but can be changed to using any other type of moving average. The FXB.ta.Bands calculation takes the parameter you provide, or its default, and does an internal call to CreateMovingAverage():

// Change the bands calculation to use a Hull MA instead of the default SMA

var bands = new FXB.ta.Bands({period: 20, deviations: 2, maType: "hull"});

The types of moving average are as follows. Each has both a numeric and a textual ID, and you can use either:

Numeric

Textual

FXB.ta.

Description

0

sma

SMA

Simple moving average

1

ema

EMA

Exponential moving average

4

vma

VMA

Variable moving average

5

vidya

VIDYA

Volatility Index Dynamic Average

7

wma

WMA

Weighted moving average

8

smma

SMMA

Smoothed moving average

9

hull

HullMA

Hull moving average

10

lsma

LSMA

Least-squares moving average

11

alma

ALMA

Arnaud Legoux moving average

12

dema

DEMA

Double exponential moving average

13

tema

TEMA

Triple exponential moving average

14

smaofsma

SMAofSMA

Simple moving average of a simple moving average

15

emaofema

EMAofEMA

Exponential moving average of an exponential moving average

16

mcginley

McGinleyMA

McGinley dynamic moving average

17

ama

AMA

Adaptive moving average

Giving another version of the example above, you can create a least-squares moving average in two ways:

var ma = new FXB.ta.LSMA({period: 20});

var ma = FXB.ta.CreateMovingAverage(10, {period: 20}); // 10 = "lsma" = FXB.ta.LSMA()

All moving averages need a period parameter in their initialisation - as widely used in various examples above.

Some of the types of moving average also require other parameters, though these have standard defaults and you may not want to change them or specify them explicitly:

Moving average

Extra parameters

ALMA

Uses offset and sigma parameters. The standard values are 0.85 and 6.

AMA

Uses fast and slow parameters for smoothing, defaulting to 2 and 10 respectively.

SMAofSMA

Uses a subPeriod parameter. The calculation does a moving average using the main period parameter, and then a moving average of that moving average using the subPeriod. If you do not provide a value for subPeriod, then it defaults to the same as period.

EMAofEMA

Same as SMAofSMA

VIDYA

Uses a cmoPeriod parameter, defaulting to the same as the main period

These extra parameters can be added to the ID of a moving average when creating an instance of the calculation via a variable. For example, the ID "alma:0.9:5" (or "11:0.9:5") means "ALMA, with an offset of 0.9 and sigma of 5". Similarly, "ama:5:15" (or "17:5:15") means "AMA, with a fast period of 5 and slow period of 15".