Lean  $LEAN_TAG$
Engulfing.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  /// Engulfing candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first: black (white) real body
27  /// - second: white(black) real body that engulfs the prior real body
28  /// The returned value is positive(+1) when bullish or negative(-1) when bearish;
29  /// The user should consider that an engulfing must appear in a downtrend if bullish or in an uptrend if bearish,
30  /// while this function does not consider it
31  /// </remarks>
33  {
34  /// <summary>
35  /// Initializes a new instance of the <see cref="Engulfing"/> class using the specified name.
36  /// </summary>
37  /// <param name="name">The name of this indicator</param>
38  public Engulfing(string name)
39  : base(name, 3)
40  {
41  }
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="Engulfing"/> class.
45  /// </summary>
46  public Engulfing()
47  : this("ENGULFING")
48  {
49  }
50 
51  /// <summary>
52  /// Gets a flag indicating when this indicator is ready and fully initialized
53  /// </summary>
54  public override bool IsReady
55  {
56  get { return Samples >= Period; }
57  }
58 
59  /// <summary>
60  /// Computes the next value of this indicator from the given state
61  /// </summary>
62  /// <param name="window">The window of data held in this indicator</param>
63  /// <param name="input">The input given to the indicator</param>
64  /// <returns>A new value for this indicator</returns>
65  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
66  {
67  if (!IsReady)
68  {
69  return 0m;
70  }
71 
72  decimal value;
73  if (
74  // white engulfs black
75  (GetCandleColor(input) == CandleColor.White && GetCandleColor(window[1]) == CandleColor.Black &&
76  input.Close > window[1].Open && input.Open < window[1].Close
77  )
78  ||
79  // black engulfs white
80  (GetCandleColor(input) == CandleColor.Black && GetCandleColor(window[1]) == CandleColor.White &&
81  input.Open > window[1].Close && input.Close < window[1].Open
82  )
83  )
84  value = (int)GetCandleColor(input);
85  else
86  value = 0;
87 
88  return value;
89  }
90  }
91 }