ATR

Modified on 2014/09/29 14:07 by Eugene — Categorized as: Standard Indicators

Syntax

public ATR(WealthLab.Bars bars, int period, string description)
public static ATR Series(WealthLab.Bars bars, int period)
public static double Value(int bar, WealthLab.Bars bars, int period)

Parameter Description

bars The Bars object
period Indicator calculation period

Description

The Average True Range is the average of the true ranges over the specified Period. WealthScript uses the moving average as formulated by Welles Wilder, the indicator's inventor (see WilderMA and TrueRange). The ATR is a measure of volatility and it takes into account any gaps in the price movement. Typically the ATR calculation is based on 14 periods, this can be intraday, daily, weekly or monthly. To measure recent volatility use a shorter average, 2 to 10 periods. For longer term volatility use 20 to 50 periods.

Interpretation


Calculation

Average True Range is calculated by applying Wilder's Moving Average to True Range over the period specified, see WilderMA indicator for more information:

ATR = ( Previous ATR * ( n - 1 ) + TR ) / n

where,
ATR = Average True Range n = number of periods or bars TR = True Range, (see TrueRange indicator)

Example

This simple system illustrates the application of the "ATR Ratchet" trailing exit created by Chuck LeBeau:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class RatchetStrategy : WealthScript { private StrategyParameter paramUnit; private StrategyParameter paramATR; private StrategyParameter paramLow; public RatchetStrategy() { paramUnit = CreateParameter("Unit", 0.05, 0.01, 0.1, 0.01); paramATR = CreateParameter("ATR period", 20, 2, 30, 2); paramLow = CreateParameter("Lowest low", 55, 5, 200, 5); } protected override void Execute() { int low = paramLow.ValueInt; int atr = paramATR.ValueInt; double unit = paramUnit.Value; DataSeries AtrRatchet = new DataSeries(Bars,"Ratchet"); PlotSeries(PricePane,AtrRatchet,Color.Red,LineStyle.Dots,4); for(int bar = GetTradingLoopStartBar(Math.Max(low,atr)); bar < Bars.Count; bar++) { AtrRatchet[bar] = Close[bar]; SetSeriesBarColor( bar, AtrRatchet, Color.Transparent ); if (IsLastPositionActive) { int daysintrade = bar - LastPosition.EntryBar; double ratchet = ( ATR.Series(Bars,atr)[bar] * unit ) * daysintrade; double sellprice = Lowest.Series(Low,low)[bar] + ratchet; AtrRatchet[bar] = sellprice; SetSeriesBarColor( bar, AtrRatchet, Color.Red );

if( Low[bar] < sellprice ) SellAtMarket(bar+1,LastPosition); } else { if(CrossOver(bar,Close,SMA.Series(Close,20))) BuyAtMarket(bar+1); } } } } }

This example plots ATRs in decreasing length if increasing blue intensity:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using WealthLab;
using WealthLab.Indicators;

namespace WealthLab.Strategies { public class MyStrategy : WealthScript { // Thank you fundtimer public Color WS4ColorToNET( double WS4Color ) { return Color.FromArgb( (int)Math.Floor( ( WS4Color % 1000 ) / 100 * 28.4 ), (int)Math.Floor( ( WS4Color % 100 ) / 10 * 28.4 ), (int)Math.Floor( WS4Color % 10 * 28.4 ) ); } protected override void Execute() { // Plot ATRs in decreasing length if increasing blue intensity ChartPane ATRPane = CreatePane( 50, true, true ); DrawLabel( ATRPane, "ATR from 2 to 18", Color.Black ); for( int i = 1; i < 10; i++ ) PlotSeries( ATRPane, ATR.Series( Bars, i * 2 ), WS4ColorToNET(10-i), LineStyle.Solid, 1 ); } } }