Lean  $LEAN_TAG$
StickSandwich.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 
17 
19 {
20  /// <summary>
21  /// Stick Sandwich candlestick pattern indicator
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - first candle: black candle
26  /// - second candle: white candle that trades only above the prior close(low > prior close)
27  /// - third candle: black candle with the close equal to the first candle's close
28  /// The meaning of "equal" is specified with SetCandleSettings
29  /// The returned value is always positive(+1): stick sandwich is always bullish;
30  /// The user should consider that stick sandwich is significant when coming in a downtrend,
31  /// while this function does not consider it
32  /// </remarks>
34  {
35  private readonly int _equalAveragePeriod;
36 
37  private decimal _equalPeriodTotal;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="StickSandwich"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public StickSandwich(string name)
44  : base(name, CandleSettings.Get(CandleSettingType.Equal).AveragePeriod + 2 + 1)
45  {
46  _equalAveragePeriod = CandleSettings.Get(CandleSettingType.Equal).AveragePeriod;
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="StickSandwich"/> class.
51  /// </summary>
52  public StickSandwich()
53  : this("STICKSANDWICH")
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 >= Period - _equalAveragePeriod)
76  {
77  _equalPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]);
78  }
79 
80  return 0m;
81  }
82 
83  decimal value;
84  if (
85  // first black
86  GetCandleColor(window[2]) == CandleColor.Black &&
87  // second white
88  GetCandleColor(window[1]) == CandleColor.White &&
89  // third black
90  GetCandleColor(input) == CandleColor.Black &&
91  // 2nd low > prior close
92  window[1].Low > window[2].Close &&
93  // 1st and 3rd same close
94  input.Close <= window[2].Close + GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[2]) &&
95  input.Close >= window[2].Close - GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[2])
96  )
97  value = 1m;
98  else
99  value = 0m;
100 
101  // add the current range and subtract the first range: this is done after the pattern recognition
102  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
103 
104  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[2]) -
105  GetCandleRange(CandleSettingType.Equal, window[_equalAveragePeriod + 2]);
106 
107  return value;
108  }
109 
110  /// <summary>
111  /// Resets this indicator to its initial state
112  /// </summary>
113  public override void Reset()
114  {
115  _equalPeriodTotal = 0m;
116  base.Reset();
117  }
118  }
119 }