Lean  $LEAN_TAG$
WilliamsPercentR.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  /// Williams %R, or just %R, is the current closing price in relation to the high and low of
22  /// the past N days (for a given N). The value of this indicator fluctuates between -100 and 0.
23  /// The symbol is said to be oversold when the oscillator is below -80%,
24  /// and overbought when the oscillator is above -20%.
25  /// </summary>
27  {
28  /// <summary>
29  /// Gets the Maximum indicator
30  /// </summary>
31  public Maximum Maximum { get; }
32 
33  /// <summary>
34  /// Gets the Minimum indicator
35  /// </summary>
36  public Minimum Minimum { get; }
37 
38  /// <summary>
39  /// Gets a flag indicating when this indicator is ready and fully initialized
40  /// </summary>
41  public override bool IsReady => Maximum.IsReady && Minimum.IsReady;
42 
43  /// <summary>
44  /// Required period, in data points, for the indicator to be ready and fully initialized.
45  /// </summary>
46  public int WarmUpPeriod { get; }
47 
48  /// <summary>
49  /// Creates a new Williams %R.
50  /// </summary>
51  /// <param name="period">The look-back period to determine the Williams %R</param>
52  public WilliamsPercentR(int period)
53  : this($"WILR({period})", period)
54  {
55  }
56 
57  /// <summary>
58  /// Creates a new Williams %R.
59  /// </summary>
60  /// <param name="name">The name of this indicator</param>
61  /// <param name="period">The look-back period to determine the Williams %R</param>
62  public WilliamsPercentR(string name, int period)
63  : base(name)
64  {
65  Maximum = new Maximum(name + "_Max", period);
66  Minimum = new Minimum(name + "_Min", period);
67  WarmUpPeriod = period;
68  }
69 
70  /// <summary>
71  /// Resets this indicator and both sub-indicators (Max and Min)
72  /// </summary>
73  public override void Reset()
74  {
75  Maximum.Reset();
76  Minimum.Reset();
77  base.Reset();
78  }
79 
80  /// <summary>
81  /// Computes the next value of this indicator from the given state
82  /// </summary>
83  /// <param name="input">The input given to the indicator</param>
84  /// <returns>A new value for this indicator</returns>
85  protected override decimal ComputeNextValue(IBaseDataBar input)
86  {
87  Minimum.Update(input.Time, input.Low);
88  Maximum.Update(input.Time, input.High);
89 
90  if (!IsReady) return 0;
91 
92  var range = Maximum.Current.Value - Minimum.Current.Value;
93 
94  return range == 0 ? 0 : -100m * (Maximum.Current.Value - input.Close) / range;
95  }
96  }
97 }