Lean  $LEAN_TAG$
LadderBottom.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  /// Ladder Bottom candlestick pattern indicator
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - three black candlesticks with consecutively lower opens and closes
26  /// - fourth candle: black candle with an upper shadow(it's supposed to be not very short)
27  /// - fifth candle: white candle that opens above prior candle's body and closes above prior candle's high
28  /// The meaning of "very short" is specified with SetCandleSettings
29  /// The returned value is positive (+1): ladder bottom is always bullish;
30  /// The user should consider that ladder bottom is significant when it appears in a downtrend,
31  /// while this function does not consider it
32  /// </remarks>
34  {
35  private readonly int _shadowVeryShortAveragePeriod;
36 
37  private decimal _shadowVeryShortPeriodTotal;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="LadderBottom"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public LadderBottom(string name)
44  : base(name, CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod + 4 + 1)
45  {
46  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
47  }
48 
49  /// <summary>
50  /// Initializes a new instance of the <see cref="LadderBottom"/> class.
51  /// </summary>
52  public LadderBottom()
53  : this("LADDERBOTTOM")
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 - _shadowVeryShortAveragePeriod)
76  {
77  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
78  }
79 
80  return 0m;
81  }
82 
83  decimal value;
84  if (
85  // 3 black candlesticks
86  GetCandleColor(window[4]) == CandleColor.Black &&
87  GetCandleColor(window[3]) == CandleColor.Black &&
88  GetCandleColor(window[2]) == CandleColor.Black &&
89  // with consecutively lower opens
90  window[4].Open > window[3].Open && window[3].Open > window[2].Open &&
91  // and closes
92  window[4].Close > window[3].Close && window[3].Close > window[2].Close &&
93  // 4th: black with an upper shadow
94  GetCandleColor(window[1]) == CandleColor.Black &&
95  GetUpperShadow(window[1]) > GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, window[1]) &&
96  // 5th: white
97  GetCandleColor(input) == CandleColor.White &&
98  // that opens above prior candle's body
99  input.Open > window[1].Open &&
100  // and closes above prior candle's high
101  input.Close > window[1].High
102  )
103  value = 1m;
104  else
105  value = 0m;
106 
107  // add the current range and subtract the first range: this is done after the pattern recognition
108  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
109 
110  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]) -
111  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod + 1]);
112 
113  return value;
114  }
115 
116  /// <summary>
117  /// Resets this indicator to its initial state
118  /// </summary>
119  public override void Reset()
120  {
121  _shadowVeryShortPeriodTotal = 0m;
122  base.Reset();
123  }
124  }
125 }