Lean  $LEAN_TAG$
UpsideGapTwoCrows.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 using System;
18 
20 {
21  /// <summary>
22  /// Upside Gap Two Crows candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: white candle, usually long
27  /// - second candle: small black real body
28  /// - gap between the first and the second candle's real bodies
29  /// - third candle: black candle with a real body that engulfs the preceding candle
30  /// and closes above the white candle's close
31  /// The meaning of "short" and "long" is specified with SetCandleSettings
32  /// The returned value is negative(-1): upside gap two crows is always bearish;
33  /// The user should consider that an upside gap two crows is significant when it appears in an uptrend,
34  /// while this function does not consider the trend
35  /// </remarks>
37  {
38  private readonly int _bodyLongAveragePeriod;
39  private readonly int _bodyShortAveragePeriod;
40 
41  private decimal _bodyLongPeriodTotal;
42  private decimal _bodyShortPeriodTotal;
43 
44  /// <summary>
45  /// Initializes a new instance of the <see cref="UpsideGapTwoCrows"/> class using the specified name.
46  /// </summary>
47  /// <param name="name">The name of this indicator</param>
48  public UpsideGapTwoCrows(string name)
49  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod) + 2 + 1)
50  {
51  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
52  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
53  }
54 
55  /// <summary>
56  /// Initializes a new instance of the <see cref="UpsideGapTwoCrows"/> class.
57  /// </summary>
59  : this("UPSIDEGAPTWOCROWS")
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 - _bodyLongAveragePeriod - 2 && Samples < Period - 2)
82  {
83  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
84  }
85 
86  if (Samples >= Period - _bodyShortAveragePeriod - 1 && Samples < Period - 1)
87  {
88  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input);
89  }
90 
91  return 0m;
92  }
93 
94  decimal value;
95  if (
96  // 1st: white
97  GetCandleColor(window[2]) == CandleColor.White &&
98  // long
99  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[2]) &&
100  // 2nd: black
101  GetCandleColor(window[1]) == CandleColor.Black &&
102  // short
103  GetRealBody(window[1]) <= GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, window[1]) &&
104  // gapping up
105  GetRealBodyGapUp(window[1], window[2]) &&
106  // 3rd: black
107  GetCandleColor(input) == CandleColor.Black &&
108  // 3rd: engulfing prior rb
109  input.Open > window[1].Open && input.Close < window[1].Close &&
110  // closing above 1st
111  input.Close > window[2].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  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
121  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 2]);
122 
123  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, window[1]) -
124  GetCandleRange(CandleSettingType.BodyShort, window[_bodyShortAveragePeriod + 1]);
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  _bodyLongPeriodTotal = 0;
135  _bodyShortPeriodTotal = 0;
136  base.Reset();
137  }
138  }
139 }