Lean  $LEAN_TAG$
Kicking.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  /// Kicking candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - first candle: marubozu
28  /// - second candle: opposite color marubozu
29  /// - gap between the two candles: upside gap if black then white, downside gap if white then black
30  /// The meaning of "long body" and "very short shadow" is specified with SetCandleSettings
31  /// The returned value is positive(+1) when bullish or negative(-1) when bearish
32  /// </remarks>
33  public class Kicking : CandlestickPattern
34  {
35  private readonly int _shadowVeryShortAveragePeriod;
36  private readonly int _bodyLongAveragePeriod;
37 
38  private decimal[] _shadowVeryShortPeriodTotal = new decimal[2];
39  private decimal[] _bodyLongPeriodTotal = new decimal[2];
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="Kicking"/> class using the specified name.
43  /// </summary>
44  /// <param name="name">The name of this indicator</param>
45  public Kicking(string name)
46  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 1 + 1)
47  {
48  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
49  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="Kicking"/> class.
54  /// </summary>
55  public Kicking()
56  : this("KICKING")
57  {
58  }
59 
60  /// <summary>
61  /// Gets a flag indicating when this indicator is ready and fully initialized
62  /// </summary>
63  public override bool IsReady
64  {
65  get { return Samples >= Period; }
66  }
67 
68  /// <summary>
69  /// Computes the next value of this indicator from the given state
70  /// </summary>
71  /// <param name="window">The window of data held in this indicator</param>
72  /// <param name="input">The input given to the indicator</param>
73  /// <returns>A new value for this indicator</returns>
74  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
75  {
76  if (!IsReady)
77  {
78  if (Samples >= Period - _shadowVeryShortAveragePeriod)
79  {
80  _shadowVeryShortPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
81  _shadowVeryShortPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
82  }
83 
84  if (Samples >= Period - _bodyLongAveragePeriod)
85  {
86  _bodyLongPeriodTotal[1] += GetCandleRange(CandleSettingType.BodyLong, window[1]);
87  _bodyLongPeriodTotal[0] += GetCandleRange(CandleSettingType.BodyLong, input);
88  }
89 
90  return 0m;
91  }
92 
93  decimal value;
94  if (
95  // opposite candles
96  (int)GetCandleColor(window[1]) == -(int)GetCandleColor(input) &&
97  // 1st marubozu
98  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[1], window[1]) &&
99  GetUpperShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
100  GetLowerShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
101  // 2nd marubozu
102  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[0], input) &&
103  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
104  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
105  // gap
106  (
107  (GetCandleColor(window[1]) == CandleColor.Black && GetCandleGapUp(input, window[1]))
108  ||
109  (GetCandleColor(window[1]) == CandleColor.White && GetCandleGapDown(input, window[1]))
110  )
111  )
112  value = (int)GetCandleColor(input);
113  else
114  value = 0m;
115 
116  // add the current range and subtract the first range: this is done after the pattern recognition
117  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
118 
119  for (var i = 1; i >= 0; i--)
120  {
121  _shadowVeryShortPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[i]) -
122  GetCandleRange(CandleSettingType.ShadowVeryShort, window[i + _shadowVeryShortAveragePeriod]);
123 
124  _bodyLongPeriodTotal[i] += GetCandleRange(CandleSettingType.BodyLong, window[i]) -
125  GetCandleRange(CandleSettingType.BodyLong, window[i + _bodyLongAveragePeriod]);
126  }
127 
128  return value;
129  }
130 
131  /// <summary>
132  /// Resets this indicator to its initial state
133  /// </summary>
134  public override void Reset()
135  {
136  _shadowVeryShortPeriodTotal = new decimal[2];
137  _bodyLongPeriodTotal = new decimal[2];
138  base.Reset();
139  }
140  }
141 }