Lean  $LEAN_TAG$
Hikkake.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  /// Hikkake candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first and second candle: inside bar (2nd has lower high and higher low than 1st)
27  /// - third candle: lower high and lower low than 2nd(higher high and higher low than 2nd)
28  /// The returned value for the hikkake bar is positive(+1) or negative(-1) meaning bullish or bearish hikkake
29  /// Confirmation could come in the next 3 days with:
30  /// - a day that closes higher than the high(lower than the low) of the 2nd candle
31  /// The returned value for the confirmation bar is equal to 1 + the bullish hikkake result or -1 - the bearish hikkake result
32  /// Note: if confirmation and a new hikkake come at the same bar, only the new hikkake is reported(the new hikkake
33  /// overwrites the confirmation of the old hikkake)
34  /// </remarks>
35  public class Hikkake : CandlestickPattern
36  {
37  private int _patternIndex;
38  private int _patternResult;
39 
40  /// <summary>
41  /// Initializes a new instance of the <see cref="Hikkake"/> class using the specified name.
42  /// </summary>
43  /// <param name="name">The name of this indicator</param>
44  public Hikkake(string name)
45  : base(name, 5 + 1)
46  {
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="Hikkake"/> class.
51  /// </summary>
52  public Hikkake()
53  : this("HIKKAKE")
54  {
55  }
56 
57  /// <summary>
58  /// Gets a flag indicating when this indicator is ready and fully initialized
59  /// </summary>
60  public override bool IsReady
61  {
62  get { return Samples >= Period; }
63  }
64 
65  /// <summary>
66  /// Computes the next value of this indicator from the given state
67  /// </summary>
68  /// <param name="window">The window of data held in this indicator</param>
69  /// <param name="input">The input given to the indicator</param>
70  /// <returns>A new value for this indicator</returns>
71  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
72  {
73  if (!IsReady)
74  {
75  if (Samples >= 3)
76  {
77  // copy here the pattern recognition code below
78  // 1st + 2nd: lower high and higher low
79  if (window[1].High < window[2].High && window[1].Low > window[2].Low &&
80  // (bull) 3rd: lower high and lower low
81  ((input.High < window[1].High && input.Low < window[1].Low)
82  ||
83  // (bear) 3rd: higher high and higher low
84  (input.High > window[1].High && input.Low > window[1].Low)
85  )
86  )
87  {
88  _patternResult = (input.High < window[1].High ? 1 : -1);
89  _patternIndex = (int)Samples - 1;
90  }
91  else
92  // search for confirmation if hikkake was no more than 3 bars ago
93  if (Samples <= _patternIndex + 4 &&
94  // close higher than the high of 2nd
95  ((_patternResult > 0 && input.Close > window[(int)Samples - _patternIndex].High)
96  ||
97  // close lower than the low of 2nd
98  (_patternResult < 0 && input.Close < window[(int)Samples - _patternIndex].Low)
99  )
100  )
101  _patternIndex = 0;
102  }
103 
104  return 0m;
105  }
106 
107  decimal value;
108  // 1st + 2nd: lower high and higher low
109  if (window[1].High < window[2].High && window[1].Low > window[2].Low &&
110  // (bull) 3rd: lower high and lower low
111  ((input.High < window[1].High && input.Low < window[1].Low)
112  ||
113  // (bear) 3rd: higher high and higher low
114  (input.High > window[1].High && input.Low > window[1].Low)
115  )
116  )
117  {
118  _patternResult = (input.High < window[1].High ? 1 : -1);
119  _patternIndex = (int) Samples - 1;
120  value = _patternResult;
121  }
122  else
123  {
124  // search for confirmation if hikkake was no more than 3 bars ago
125  if (Samples <= _patternIndex + 4 &&
126  // close higher than the high of 2nd
127  ((_patternResult > 0 && input.Close > window[(int) Samples - _patternIndex].High)
128  ||
129  // close lower than the low of 2nd
130  (_patternResult < 0 && input.Close < window[(int) Samples - _patternIndex].Low)
131  )
132  )
133  {
134  value = _patternResult + (_patternResult > 0 ? 1 : -1);
135  _patternIndex = 0;
136  }
137  else
138  value = 0;
139  }
140 
141  return value;
142  }
143 
144  /// <summary>
145  /// Resets this indicator to its initial state
146  /// </summary>
147  public override void Reset()
148  {
149  _patternIndex = 0;
150  _patternResult = 0;
151  base.Reset();
152  }
153  }
154 }