Lean  $LEAN_TAG$
TwoCrows.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  /// Two Crows candlestick pattern indicator
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - first candle: long white candle
26  /// - second candle: black real body
27  /// - gap between the first and the second candle's real bodies
28  /// - third candle: black candle that opens within the second real body and closes within the first real body
29  /// The meaning of "long" is specified with SetCandleSettings
30  /// The returned value is negative (-1): two crows is always bearish;
31  /// The user should consider that two crows is significant when it appears in an uptrend, while this function
32  /// does not consider the trend.
33  /// </remarks>
35  {
36  private readonly int _bodyLongAveragePeriod;
37 
38  private decimal _bodyLongPeriodTotal;
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="TwoCrows"/> class using the specified name.
42  /// </summary>
43  /// <param name="name">The name of this indicator</param>
44  public TwoCrows(string name)
45  : base(name, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod + 2 + 1)
46  {
47  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
48  }
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="TwoCrows"/> class.
52  /// </summary>
53  public TwoCrows()
54  : this("TWOCROWS")
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 - _bodyLongAveragePeriod - 2 && Samples < Period - 2)
77  {
78  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
79  }
80  return 0m;
81  }
82 
83  decimal value;
84  if (
85  // 1st: white
86  GetCandleColor(window[2]) == CandleColor.White &&
87  // long
88  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[2]) &&
89  // 2nd: black
90  GetCandleColor(window[1]) == CandleColor.Black &&
91  // gapping up
92  GetRealBodyGapUp(window[1], window[2]) &&
93  // 3rd: black
94  GetCandleColor(input) == CandleColor.Black &&
95  // opening within 2nd rb
96  input.Open < window[1].Open && input.Open > window[1].Close &&
97  // closing within 1st rb
98  input.Close > window[2].Open && input.Close < window[2].Close
99  )
100  value = -1m;
101  else
102  value = 0m;
103 
104  // add the current range and subtract the first range: this is done after the pattern recognition
105  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
106 
107  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
108  GetCandleRange(CandleSettingType.BodyLong, window[2 + _bodyLongAveragePeriod]);
109 
110  return value;
111  }
112 
113  /// <summary>
114  /// Resets this indicator to its initial state
115  /// </summary>
116  public override void Reset()
117  {
118  _bodyLongPeriodTotal = 0m;
119  base.Reset();
120  }
121  }
122 }