Lean  $LEAN_TAG$
KickingByLength.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 (bull/bear determined by the longer marubozu) 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; the longer of the two
32  /// marubozu determines the bullishness or bearishness of this pattern
33  /// </remarks>
35  {
36  private readonly int _shadowVeryShortAveragePeriod;
37  private readonly int _bodyLongAveragePeriod;
38 
39  private decimal[] _shadowVeryShortPeriodTotal = new decimal[2];
40  private decimal[] _bodyLongPeriodTotal = new decimal[2];
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="KickingByLength"/> class using the specified name.
44  /// </summary>
45  /// <param name="name">The name of this indicator</param>
46  public KickingByLength(string name)
47  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 1 + 1)
48  {
49  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
50  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="KickingByLength"/> class.
55  /// </summary>
56  public KickingByLength()
57  : this("KICKINGBYLENGTH")
58  {
59  }
60 
61  /// <summary>
62  /// Gets a flag indicating when this indicator is ready and fully initialized
63  /// </summary>
64  public override bool IsReady
65  {
66  get { return Samples >= Period; }
67  }
68 
69  /// <summary>
70  /// Computes the next value of this indicator from the given state
71  /// </summary>
72  /// <param name="window">The window of data held in this indicator</param>
73  /// <param name="input">The input given to the indicator</param>
74  /// <returns>A new value for this indicator</returns>
75  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
76  {
77  if (!IsReady)
78  {
79  if (Samples >= Period - _shadowVeryShortAveragePeriod)
80  {
81  _shadowVeryShortPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
82  _shadowVeryShortPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
83  }
84 
85  if (Samples >= Period - _bodyLongAveragePeriod)
86  {
87  _bodyLongPeriodTotal[1] += GetCandleRange(CandleSettingType.BodyLong, window[1]);
88  _bodyLongPeriodTotal[0] += GetCandleRange(CandleSettingType.BodyLong, input);
89  }
90 
91  return 0m;
92  }
93 
94  decimal value;
95  if (
96  // opposite candles
97  (int)GetCandleColor(window[1]) == -(int)GetCandleColor(input) &&
98  // 1st marubozu
99  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[1], window[1]) &&
100  GetUpperShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
101  GetLowerShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
102  // 2nd marubozu
103  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[0], input) &&
104  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
105  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
106  // gap
107  (
108  (GetCandleColor(window[1]) == CandleColor.Black && GetCandleGapUp(input, window[1]))
109  ||
110  (GetCandleColor(window[1]) == CandleColor.White && GetCandleGapDown(input, window[1]))
111  )
112  )
113  value = (int)GetCandleColor(GetRealBody(input) > GetRealBody(window[1]) ? input : window[1]);
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  for (var i = 1; i >= 0; i--)
121  {
122  _shadowVeryShortPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[i]) -
123  GetCandleRange(CandleSettingType.ShadowVeryShort, window[i + _shadowVeryShortAveragePeriod]);
124 
125  _bodyLongPeriodTotal[i] += GetCandleRange(CandleSettingType.BodyLong, window[i]) -
126  GetCandleRange(CandleSettingType.BodyLong, window[i + _bodyLongAveragePeriod]);
127  }
128 
129  return value;
130  }
131 
132  /// <summary>
133  /// Resets this indicator to its initial state
134  /// </summary>
135  public override void Reset()
136  {
137  _shadowVeryShortPeriodTotal = new decimal[2];
138  _bodyLongPeriodTotal = new decimal[2];
139  base.Reset();
140  }
141  }
142 }