Lean  $LEAN_TAG$
AverageRange.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
17 
19 {
20  /// <summary>
21  /// Represents the Average Range (AR) indicator, which calculates the average price range
22  /// </summary>
24  {
25  /// <summary>
26  /// The Simple Moving Average (SMA) used to calculate the average of the price ranges.
27  /// </summary>
28  private readonly SimpleMovingAverage _sma;
29 
30  /// <summary>
31  /// Initializes a new instance of the AverageRange class with the specified name and period.
32  /// </summary>
33  /// <param name="name">The name of the AR indicator.</param>
34  /// <param name="period">The number of periods over which to compute the average range.</param>
35  public AverageRange(string name, int period) : base(name)
36  {
37  _sma = new SimpleMovingAverage(name + "_SMA", period);
38  }
39 
40  /// <summary>
41  /// Initializes the AR indicator with the default name format and period.
42  /// </summary>
43  public AverageRange(int period)
44  : this($"AR({period})", period)
45  {
46  }
47 
48  /// <summary>
49  /// Indicates whether the indicator has enough data to start producing valid results.
50  /// </summary>
51  public override bool IsReady => _sma.IsReady;
52 
53  /// <summary>
54  /// The number of periods needed to fully initialize the AR indicator.
55  /// </summary>
56  public int WarmUpPeriod => _sma.WarmUpPeriod;
57 
58  /// <summary>
59  /// Resets the indicator and clears the internal state, including the SMA.
60  /// </summary>
61  public override void Reset()
62  {
63  _sma.Reset();
64  base.Reset();
65  }
66 
67  /// <summary>
68  /// Computes the next value of the Average Range (AR) by calculating the price range (high - low)
69  /// and passing it to the SMA to get the smoothed value.
70  /// </summary>
71  /// <param name="input">The input data for the current bar, including open, high, low, close values.</param>
72  /// <returns>The computed AR value, which is the smoothed average of price ranges.</returns>
73  protected override decimal ComputeNextValue(IBaseDataBar input)
74  {
75  var priceRange = input.High - input.Low;
76  // Update the SMA with the price range
77  _sma.Update(new IndicatorDataPoint(input.EndTime, priceRange));
78  return _sma.Current.Value;
79  }
80  }
81 }