Lean  $LEAN_TAG$
ThreeBlackCrows.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  /// Three Black Crows candlestick pattern
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - three consecutive and declining black candlesticks
26  /// - each candle must have no or very short lower shadow
27  /// - each candle after the first must open within the prior candle's real body
28  /// - the first candle's close should be under the prior white candle's high
29  /// The meaning of "very short" is specified with SetCandleSettings
30  /// The returned value is negative (-1): three black crows is always bearish;
31  /// The user should consider that 3 black crows is significant when it appears after a mature advance or at high levels,
32  /// while this function does not consider it
33  /// </remarks>
35  {
36  private readonly int _shadowVeryShortAveragePeriod;
37 
38  private decimal[] _shadowVeryShortPeriodTotal = new decimal[3];
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="ThreeBlackCrows"/> class using the specified name.
42  /// </summary>
43  /// <param name="name">The name of this indicator</param>
44  public ThreeBlackCrows(string name)
45  : base(name, CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod + 3 + 1)
46  {
47  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
48  }
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="ThreeBlackCrows"/> class.
52  /// </summary>
53  public ThreeBlackCrows()
54  : this("THREEBLACKCROWS")
55  {
56  }
57 
58  /// <summary>
59  /// Gets a flag indicating when this indicator is ready and fully initialized
60  /// </summary>
61  public override bool IsReady
62  {
63  get { return Samples >= Period; }
64  }
65 
66  /// <summary>
67  /// Computes the next value of this indicator from the given state
68  /// </summary>
69  /// <param name="window">The window of data held in this indicator</param>
70  /// <param name="input">The input given to the indicator</param>
71  /// <returns>A new value for this indicator</returns>
72  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
73  {
74  if (!IsReady)
75  {
76  if (Samples >= Period - _shadowVeryShortAveragePeriod)
77  {
78  _shadowVeryShortPeriodTotal[2] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[2]);
79  _shadowVeryShortPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
80  _shadowVeryShortPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
81  }
82 
83  return 0m;
84  }
85 
86  decimal value;
87  if (
88  // white
89  GetCandleColor(window[3]) == CandleColor.White &&
90  // 1st black
91  GetCandleColor(window[2]) == CandleColor.Black &&
92  // very short lower shadow
93  GetLowerShadow(window[2]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[2], window[2]) &&
94  // 2nd black
95  GetCandleColor(window[1]) == CandleColor.Black &&
96  // very short lower shadow
97  GetLowerShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
98  // 3rd black
99  GetCandleColor(input) == CandleColor.Black &&
100  // very short lower shadow
101  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
102  // 2nd black opens within 1st black's rb
103  window[1].Open < window[2].Open && window[1].Open > window[2].Close &&
104  // 3rd black opens within 2nd black's rb
105  input.Open < window[1].Open && input.Open > window[1].Close &&
106  // 1st black closes under prior candle's high
107  window[3].High > window[2].Close &&
108  // three declining
109  window[2].Close > window[1].Close &&
110  // three declining
111  window[1].Close > input.Close
112  )
113  value = -1m;
114  else
115  value = 0m;
116 
117  // add the current range and subtract the first range: this is done after the pattern recognition
118  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
119 
120  for (var i = 2; i >= 0; i--)
121  {
122  _shadowVeryShortPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[i]) -
123  GetCandleRange(CandleSettingType.ShadowVeryShort, window[i + _shadowVeryShortAveragePeriod]);
124  }
125 
126  return value;
127  }
128 
129  /// <summary>
130  /// Resets this indicator to its initial state
131  /// </summary>
132  public override void Reset()
133  {
134  _shadowVeryShortPeriodTotal = new decimal[3];
135  base.Reset();
136  }
137  }
138 }