Lean  $LEAN_TAG$
TasukiGap.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  /// Tasuki Gap candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - upside (downside) gap
27  /// - first candle after the window: white(black) candlestick
28  /// - second candle: black(white) candlestick that opens within the previous real body and closes under(above)
29  /// the previous real body inside the gap
30  /// - the size of two real bodies should be near the same
31  /// The meaning of "near" is specified with SetCandleSettings
32  /// The returned value is positive(+1) when bullish or negative(-1) when bearish;
33  /// The user should consider that tasuki gap is significant when it appears in a trend, while this function does
34  /// not consider it
35  /// </remarks>
37  {
38  private readonly int _nearAveragePeriod;
39 
40  private decimal _nearPeriodTotal;
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="TasukiGap"/> class using the specified name.
44  /// </summary>
45  /// <param name="name">The name of this indicator</param>
46  public TasukiGap(string name)
47  : base(name, CandleSettings.Get(CandleSettingType.Near).AveragePeriod + 2 + 1)
48  {
49  _nearAveragePeriod = CandleSettings.Get(CandleSettingType.Near).AveragePeriod;
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="TasukiGap"/> class.
54  /// </summary>
55  public TasukiGap()
56  : this("TASUKIGAP")
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 - _nearAveragePeriod)
79  {
80  _nearPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]);
81  }
82 
83  return 0m;
84  }
85 
86  decimal value;
87  if (
88  (
89  // upside gap
90  GetRealBodyGapUp(window[1], window[2]) &&
91  // 1st: white
92  GetCandleColor(window[1]) == CandleColor.White &&
93  // 2nd: black
94  GetCandleColor(input) == CandleColor.Black &&
95  // that opens within the white rb
96  input.Open < window[1].Close && input.Open > window[1].Open &&
97  // and closes under the white rb
98  input.Close < window[1].Open &&
99  // inside the gap
100  input.Close > Math.Max(window[2].Close, window[2].Open) &&
101  // size of 2 rb near the same
102  Math.Abs(GetRealBody(window[1]) - GetRealBody(input)) < GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal, window[1])
103  ) ||
104  (
105  // downside gap
106  GetRealBodyGapDown(window[1], window[2]) &&
107  // 1st: black
108  GetCandleColor(window[1]) == CandleColor.Black &&
109  // 2nd: white
110  GetCandleColor(input) == CandleColor.White &&
111  // that opens within the black rb
112  input.Open < window[1].Open && input.Open > window[1].Close &&
113  // and closes above the black rb
114  input.Close > window[1].Open &&
115  // inside the gap
116  input.Close < Math.Min(window[2].Close, window[2].Open) &&
117  // size of 2 rb near the same
118  Math.Abs(GetRealBody(window[1]) - GetRealBody(input)) < GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal, window[1])
119  )
120  )
121  value = (int)GetCandleColor(window[1]);
122  else
123  value = 0m;
124 
125  // add the current range and subtract the first range: this is done after the pattern recognition
126  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
127 
128  _nearPeriodTotal += GetCandleRange(CandleSettingType.Near, window[1]) -
129  GetCandleRange(CandleSettingType.Near, window[_nearAveragePeriod + 1]);
130 
131  return value;
132  }
133 
134  /// <summary>
135  /// Resets this indicator to its initial state
136  /// </summary>
137  public override void Reset()
138  {
139  _nearPeriodTotal = 0m;
140  base.Reset();
141  }
142  }
143 }