Lean  $LEAN_TAG$
DarkCloudCover.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 
18 
20 {
21  /// <summary>
22  /// Dark Cloud Cover candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: long white candle
27  /// - second candle: black candle that opens above previous day high and closes within previous day real body;
28  /// Greg Morris wants the close to be below the midpoint of the previous real body
29  /// The meaning of "long" is specified with SetCandleSettings, the penetration of the first real body is specified
30  /// with optInPenetration
31  /// The returned value is negative(-1): dark cloud cover is always bearish
32  /// The user should consider that a dark cloud cover is significant when it appears in an uptrend, while
33  /// this function does not consider it
34  /// </remarks>
36  {
37  private readonly decimal _penetration;
38 
39  private readonly int _bodyLongAveragePeriod;
40 
41  private decimal _bodyLongPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="DarkCloudCover"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  /// <param name="penetration">Percentage of penetration of a candle within another candle</param>
48  public DarkCloudCover(string name, decimal penetration = 0.5m)
49  : base(name, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod + 1 + 1)
50  {
51  _penetration = penetration;
52 
53  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the <see cref="DarkCloudCover"/> class.
58  /// </summary>
59  /// <param name="penetration">Percentage of penetration of a candle within another candle</param>
60  public DarkCloudCover(decimal penetration)
61  : this("DARKCLOUDCOVER", penetration)
62  {
63  }
64 
65  /// <summary>
66  /// Initializes a new instance of the <see cref="DarkCloudCover"/> class.
67  /// </summary>
68  public DarkCloudCover()
69  : this("DARKCLOUDCOVER")
70  {
71  }
72 
73  /// <summary>
74  /// Gets a flag indicating when this indicator is ready and fully initialized
75  /// </summary>
76  public override bool IsReady
77  {
78  get { return Samples >= Period; }
79  }
80 
81  /// <summary>
82  /// Computes the next value of this indicator from the given state
83  /// </summary>
84  /// <param name="window">The window of data held in this indicator</param>
85  /// <param name="input">The input given to the indicator</param>
86  /// <returns>A new value for this indicator</returns>
87  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
88  {
89  if (!IsReady)
90  {
91  if (Samples >= Period - _bodyLongAveragePeriod)
92  {
93  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[1]);
94  }
95 
96  return 0m;
97  }
98 
99  decimal value;
100  if (
101  // 1st: white
102  GetCandleColor(window[1]) == CandleColor.White &&
103  // long
104  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[1]) &&
105  // 2nd: black
106  GetCandleColor(input) == CandleColor.Black &&
107  // open above prior high
108  input.Open > window[1].High &&
109  // close within prior body
110  input.Close > window[1].Open &&
111  input.Close < window[1].Close - GetRealBody(window[1]) * _penetration
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  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[1]) -
121  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 1]);
122 
123  return value;
124  }
125 
126  /// <summary>
127  /// Resets this indicator to its initial state
128  /// </summary>
129  public override void Reset()
130  {
131  _bodyLongPeriodTotal = 0;
132  base.Reset();
133  }
134  }
135 }