Lean  $LEAN_TAG$
MatchingLow.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  /// Matching Low candlestick pattern indicator
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - first candle: black candle
26  /// - second candle: black candle with the close equal to the previous close
27  /// The meaning of "equal" is specified with SetCandleSettings
28  /// The returned value is always positive(+1): matching low is always bullish;
29  /// </remarks>
31  {
32  private readonly int _equalAveragePeriod;
33 
34  private decimal _equalPeriodTotal;
35 
36  /// <summary>
37  /// Initializes a new instance of the <see cref="MatchingLow"/> class using the specified name.
38  /// </summary>
39  /// <param name="name">The name of this indicator</param>
40  public MatchingLow(string name)
41  : base(name, CandleSettings.Get(CandleSettingType.Equal).AveragePeriod + 1 + 1)
42  {
43  _equalAveragePeriod = CandleSettings.Get(CandleSettingType.Equal).AveragePeriod;
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="MatchingLow"/> class.
48  /// </summary>
49  public MatchingLow()
50  : this("MATCHINGLOW")
51  {
52  }
53 
54  /// <summary>
55  /// Gets a flag indicating when this indicator is ready and fully initialized
56  /// </summary>
57  public override bool IsReady
58  {
59  get { return Samples >= Period; }
60  }
61 
62  /// <summary>
63  /// Computes the next value of this indicator from the given state
64  /// </summary>
65  /// <param name="window">The window of data held in this indicator</param>
66  /// <param name="input">The input given to the indicator</param>
67  /// <returns>A new value for this indicator</returns>
68  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
69  {
70  if (!IsReady)
71  {
72  if (Samples >= Period - _equalAveragePeriod)
73  {
74  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]);
75  }
76 
77  return 0m;
78  }
79 
80  decimal value;
81  if (
82  // first black
83  GetCandleColor(window[1]) == CandleColor.Black &&
84  // second black
85  GetCandleColor(input) == CandleColor.Black &&
86  // 1st and 2nd same close
87  input.Close <= window[1].Close + GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[1]) &&
88  input.Close >= window[1].Close - GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[1])
89  )
90  value = 1m;
91  else
92  value = 0m;
93 
94  // add the current range and subtract the first range: this is done after the pattern recognition
95  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
96 
97  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]) -
98  GetCandleRange(CandleSettingType.Equal, window[_equalAveragePeriod + 1]);
99 
100  return value;
101  }
102 
103  /// <summary>
104  /// Resets this indicator to its initial state
105  /// </summary>
106  public override void Reset()
107  {
108  _equalPeriodTotal = 0m;
109  base.Reset();
110  }
111  }
112 }