Lean  $LEAN_TAG$
AugenPriceSpike.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 using System;
16 
18 {
19  /// <summary>
20  /// The Augen Price Spike indicator is an indicator that measures price
21  /// changes in terms of standard deviations. In the book, The
22  /// Volatility Edge in Options Trading, Jeff Augen describes a
23  /// method for tracking absolute price changes in terms of recent
24  /// volatility, using the standard deviation.
25  ///
26  /// length = x
27  /// closes = closeArray
28  /// closes1 = closeArray shifted right by 1
29  /// closes2 = closeArray shifted right by 2
30  /// closeLog = np.log(np.divide(closes1, closes2))
31  /// SDev = np.std(closeLog)
32  /// m = SDev * closes1[-1]
33  /// spike = (closes[-1]-closes1[-1])/m
34  /// return spike
35  ///
36  /// Augen Price Spike from TradingView
37  /// https://www.tradingview.com/script/fC7Pn2X2-Price-Spike-Jeff-Augen/
38  ///
39  /// </summary>
41  {
42  private readonly StandardDeviation _standardDeviation;
43  private readonly RollingWindow<decimal> _rollingData;
44 
45  /// <summary>
46  /// Initializes a new instance of the AugenPriceSpike class using the specified period
47  /// </summary>
48  /// <param name="period">The period over which to perform to computation</param>
49  public AugenPriceSpike(int period = 3)
50  : this($"APS({period})", period)
51  {
52  }
53  /// <summary>
54  /// Creates a new AugenPriceSpike indicator with the specified period
55  /// </summary>
56  /// <param name="name">The name of this indicator</param>
57  /// <param name="period">The period of this indicator</param>
58  public AugenPriceSpike(string name, int period)
59  : base(name)
60  {
61  if (period < 3)
62  {
63  throw new ArgumentException("AugenPriceSpike Indicator must have a period of at least 3", nameof(period));
64  }
65  _standardDeviation = new StandardDeviation(period);
66  _rollingData = new RollingWindow<decimal>(3);
67  WarmUpPeriod = period+2;
68  }
69 
70  /// <summary>
71  /// Gets a flag indicating when the indicator is ready and fully initialized
72  /// </summary>
73  public override bool IsReady => _rollingData.IsReady && _standardDeviation.IsReady;
74 
75  /// <summary>
76  /// Required period, in data points, for the indicator to be ready and fully initialized.
77  /// </summary>
78  public int WarmUpPeriod { get; }
79 
80  /// <summary>
81  /// Computes the next value for this indicator from the given state.
82  /// </summary>
83  /// <param name="input">The input value to this indicator on this time step</param>
84  /// <returns>A a value for this indicator</returns>
85  protected override decimal ComputeNextValue(IndicatorDataPoint input)
86  {
87  _rollingData.Add(input.Value);
88  if (_rollingData.Count < 3) { return 0m; }
89 
90  var previousPoint = _rollingData[1];
91  var previousPoint2 = _rollingData[2];
92 
93  var logPoint = 0.0;
94  if (previousPoint2 != 0)
95  {
96  logPoint = Math.Log((double)(previousPoint / previousPoint2));
97  }
98 
99  _standardDeviation.Update(input.Time, (decimal)logPoint);
100 
101  if (!_rollingData.IsReady) { return 0m; }
102  if (!_standardDeviation.IsReady) { return 0m; }
103 
104  var m = _standardDeviation.Current.Value * previousPoint;
105  if (m == 0) { return 0; }
106 
107  var spikeValue = (input.Value - previousPoint) / m;
108  return spikeValue;
109  }
110 
111  /// <summary>
112  /// Resets this indicator to its initial state
113  /// </summary>
114  public override void Reset()
115  {
116  _standardDeviation.Reset();
117  _rollingData.Reset();
118  base.Reset();
119  }
120  }
121 }