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