Lean  $LEAN_TAG$
UpDownGapThreeMethods.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  /// Up/Down Gap Three Methods candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - first candle: white (black) candle
28  /// - second candle: white(black) candle
29  /// - upside(downside) gap between the first and the second real bodies
30  /// - third candle: black(white) candle that opens within the second real body and closes within the first real body
31  /// The returned value is positive(+1) when bullish or negative(-1) when bearish;
32  /// The user should consider that up/downside gap 3 methods is significant when it appears in a trend, while this
33  /// function does not consider it
34  /// </remarks>
36  {
37  /// <summary>
38  /// Initializes a new instance of the <see cref="UpDownGapThreeMethods"/> class using the specified name.
39  /// </summary>
40  /// <param name="name">The name of this indicator</param>
41  public UpDownGapThreeMethods(string name)
42  : base(name, 2 + 1)
43  {
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="UpDownGapThreeMethods"/> class.
48  /// </summary>
50  : this("UPDOWNGAPTHREEMETHODS")
51  {
52  }
53 
54  /// <summary>
55  /// Gets a flag indicating when this indicator is ready and fully initialized
56  /// </summary>
57  public override bool IsReady
58  {
59  get { return Samples >= Period; }
60  }
61 
62  /// <summary>
63  /// Computes the next value of this indicator from the given state
64  /// </summary>
65  /// <param name="window">The window of data held in this indicator</param>
66  /// <param name="input">The input given to the indicator</param>
67  /// <returns>A new value for this indicator</returns>
68  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
69  {
70  if (!IsReady)
71  {
72  return 0m;
73  }
74 
75  decimal value;
76  if (
77  // 1st and 2nd of same color
78  GetCandleColor(window[2]) == GetCandleColor(window[1]) &&
79  // 3rd opposite color
80  (int)GetCandleColor(window[1]) == -(int)GetCandleColor(input) &&
81  // 3rd opens within 2nd rb
82  input.Open < Math.Max(window[1].Close, window[1].Open) &&
83  input.Open > Math.Min(window[1].Close, window[1].Open) &&
84  // 3rd closes within 1st rb
85  input.Close < Math.Max(window[2].Close, window[2].Open) &&
86  input.Close > Math.Min(window[2].Close, window[2].Open) &&
87  ((
88  // when 1st is white
89  GetCandleColor(window[2]) == CandleColor.White &&
90  // upside gap
91  GetRealBodyGapUp(window[1], window[2])
92  ) ||
93  (
94  // when 1st is black
95  GetCandleColor(window[2]) == CandleColor.Black &&
96  // downside gap
97  GetRealBodyGapDown(window[1], window[2])
98  )
99  )
100  )
101  value = (int)GetCandleColor(window[2]);
102  else
103  value = 0;
104 
105  return value;
106  }
107  }
108 }