Signaling Average True Range

View as Markdown

Indicator Tutorial 8

Fancy-looking EMA is not enough for successful trading. We need a fancy-looking Average True Range indicator too.

First, we just copy the built-in ATR indicator with some renaming:

1const predef = require("./tools/predef");
2const meta = require("./tools/meta");
3const MMA = require("./tools/MMA");
4const trueRange = require("./tools/trueRange");
5
6class averageTrueRange {
7 init() {
8 this.movingAverage = MMA(this.props.period);
9 }
10
11 map(d, i, history) {
12 return this.movingAverage(trueRange(d, history.prior()));
13 }
14}
15
16module.exports = {
17 name: "exampleATR",
18 description: "Average True Range",
19 calculator: averageTrueRange,
20 params: {
21 period: predef.paramSpecs.period(14)
22 },
23 inputType: meta.InputType.BARS,
24 areaChoice: meta.AreaChoice.NEW,
25 tags: ["My Indicators"],
26 schemeStyles: predef.styles.solidLine("#ffe270")
27};

The source code is similar to our previous indicators with some additions in the module’s export: Indicator.inputType restricts user’s choice with OHLC bars here, and Indicator.areaChoice will highlight ‘New Area’ choice by default when you will place an indicator to the chart. Bars as an input are required to calculate True Range that needs High, Low and Close: all of them will be available in map function via d.high(), d.low() and d.close()

Our goal is to improve the indicator and highlight places where it is larger than some parameterized threshold. Moreover, the threshold parameter will be in tick sizes to make the indicator a product-neutral.

We will highlight the ATR line as well as corresponding candlesticks. For simplicity, highlighting will be with eye-catching tones of red/green colors.

If we need just change the style of dots and columns, we don’t need to implement a custom plotter: all we need to do is to compose the style field in the returned object from the map function. Candlestick style is done a similar way.

1const predef = require("./tools/predef");
2const meta = require("./tools/meta");
3const MMA = require("./tools/MMA");
4const trueRange = require("./tools/trueRange");
5
6class averageTrueRange {
7 init() {
8 this.movingAverage = MMA(this.props.period);
9 }
10
11 map(d, i, history) {
12 const atr = this.movingAverage(trueRange(d, history.prior()));
13 const tickSize = this.contractInfo.tickSize;
14 const atrInTicks = atr / tickSize;
15 let overrideStyle;
16 if (atrInTicks > this.props.threshold) {
17 overrideStyle = {
18 color: d.open() > d.close() ? "salmon" : "lightgreen"
19 };
20 }
21 return {
22 value: atr,
23 candlestick: overrideStyle,
24 style: {
25 value: overrideStyle
26 }
27 };
28 }
29}
30
31module.exports = {
32 name: "exampleATR",
33 description: "Average True Range",
34 calculator: averageTrueRange,
35 params: {
36 period: predef.paramSpecs.period(14),
37 threshold: predef.paramSpecs.number(10, 1, 0)
38 },
39 inputType: meta.InputType.BARS,
40 areaChoice: meta.AreaChoice.NEW,
41 tags: ["My Indicators"],
42 plotter: predef.plotters.columns("value"),
43 schemeStyles: predef.styles.solidLine("#ffe270")
44};

Signaling ATR

this.contractInfo above is an object with details about the contract of the chart. The app assigns it to the indicator during construction.