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