Skip to content

AverageTrueRange

Summary

Average true range. An indicator providing the degree of price volatility.

Remarks

Average true range is a volatility indicator originally developed by J. Welles Wilder. The indicator provides the degree of price volatility. The average true range is an N-day (exponential) moving average of the true range values. Wilder recommended a 14-period smoothing.

Signature

1
public abstract interface AverageTrueRange

Namespace

cAlgo.API.Indicators

Properties

Name Description
Result { get; set; } The resulting data series of Average True Range Indicator instance

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 private AverageTrueRange averageTrueRange;
 [Parameter(DefaultValue = 14)]
 public int Periods { get; set; }
 [Parameter(DefaultValue = 0.002)]
 public double ATRValue { get; set; }
 [Parameter("MA Type", DefaultValue = MovingAverageType.Exponential)]
 public MovingAverageType MAType { get; set; }
 protected override void OnStart()
 {
     averageTrueRange = Indicators.AverageTrueRange(Periods, MAType);
 }
 protected override void OnTick()
 {
     // if the 14 day Average True Range is higher than 0.002
     if(averageTrueRange.Result.LastValue >= ATRValue)
     {
         // Do Something
     }
    }
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
 using cAlgo.API;
 using cAlgo.API.Indicators;
 using System;
 namespace cAlgo.Robots
 {
     // This sample cBot shows how to use an Average True Range indicator
     [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
     public class AverageTrueRangeSample : Robot
     {
         private double _volumeInUnits;
         private AverageTrueRange _averageTrueRange;
         [Parameter("Volume (Lots)", DefaultValue = 0.01)]
         public double VolumeInLots { get; set; }
         [Parameter("Label", DefaultValue = "Sample")]
         public string Label { get; set; }
         public Position[] BotPositions
         {
             get
             {
                 return Positions.FindAll(Label);
             }
         }
         protected override void OnStart()
         {
             _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
             _averageTrueRange = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
         }
         protected override void OnBar()
         {
             if (Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1) && Bars.ClosePrices.Last(2) < Bars.OpenPrices.Last(2))
             {
                 ClosePositions(TradeType.Sell);
                 ExecuteOrder(TradeType.Buy);
             }
             else if (Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1) && Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2))
             {
                 ClosePositions(TradeType.Buy);
                 ExecuteOrder(TradeType.Sell);
             }
         }
         private void ClosePositions(TradeType tradeType)
         {
             foreach (var position in BotPositions)
             {
                 if (position.TradeType != tradeType) continue;
                 ClosePosition(position);
             }
         }
         private void ExecuteOrder(TradeType tradeType)
         {
             var atrInPips = _averageTrueRange.Result.Last(1) * (Symbol.TickSize / Symbol.PipSize * Math.Pow(10, Symbol.Digits));
             var stopLossInPips = atrInPips * 2;
             var takeProfitInPips = stopLossInPips * 2;
             ExecuteMarketOrder(tradeType, SymbolName, _volumeInUnits, Label, stopLossInPips, takeProfitInPips);
         }
     }
 }

Last update: July 1, 2022

Comments