Lean  $LEAN_TAG$
Piercing.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  /// Piercing candlestick pattern
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - first candle: long black candle
26  /// - second candle: long white candle with open below previous day low and close at least at 50% of previous day
27  /// real body
28  /// The meaning of "long" is specified with SetCandleSettings
29  /// The returned value is positive(+1): piercing pattern is always bullish
30  /// The user should consider that a piercing pattern is significant when it appears in a downtrend, while
31  /// this function does not consider it
32  /// </remarks>
34  {
35  private readonly int _bodyLongAveragePeriod;
36 
37  private decimal[] _bodyLongPeriodTotal = new decimal[2];
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="Piercing"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public Piercing(string name)
44  : base(name, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod + 1 + 1)
45  {
46  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="Piercing"/> class.
51  /// </summary>
52  public Piercing()
53  : this("PIERCING")
54  {
55  }
56 
57  /// <summary>
58  /// Gets a flag indicating when this indicator is ready and fully initialized
59  /// </summary>
60  public override bool IsReady
61  {
62  get { return Samples >= Period; }
63  }
64 
65  /// <summary>
66  /// Computes the next value of this indicator from the given state
67  /// </summary>
68  /// <param name="window">The window of data held in this indicator</param>
69  /// <param name="input">The input given to the indicator</param>
70  /// <returns>A new value for this indicator</returns>
71  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
72  {
73  if (!IsReady)
74  {
75  if (Samples >= Period - _bodyLongAveragePeriod)
76  {
77  _bodyLongPeriodTotal[1] += GetCandleRange(CandleSettingType.BodyLong, window[1]);
78  _bodyLongPeriodTotal[0] += GetCandleRange(CandleSettingType.BodyLong, input);
79  }
80 
81  return 0m;
82  }
83 
84  decimal value;
85  if (
86  // 1st: black
87  GetCandleColor(window[1]) == CandleColor.Black &&
88  // long
89  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[1], window[1]) &&
90  // 2nd: white
91  GetCandleColor(input) == CandleColor.White &&
92  // long
93  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[0], input) &&
94  // open below prior low
95  input.Open < window[1].Low &&
96  // close within prior body
97  input.Close < window[1].Open &&
98  // above midpoint
99  input.Close > window[1].Close + GetRealBody(window[1]) * 0.5m
100  )
101  value = 1m;
102  else
103  value = 0m;
104 
105  // add the current range and subtract the first range: this is done after the pattern recognition
106  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
107 
108  for (var i = 1; i >= 0; i--)
109  {
110  _bodyLongPeriodTotal[i] += GetCandleRange(CandleSettingType.BodyLong, window[i]) -
111  GetCandleRange(CandleSettingType.BodyLong, window[i + _bodyLongAveragePeriod]);
112  }
113 
114  return value;
115  }
116 
117  /// <summary>
118  /// Resets this indicator to its initial state
119  /// </summary>
120  public override void Reset()
121  {
122  _bodyLongPeriodTotal = new decimal[2];
123  base.Reset();
124  }
125  }
126 }