Lean  $LEAN_TAG$
StalledPattern.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  /// Stalled Pattern candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - three white candlesticks with consecutively higher closes
28  /// - first candle: long white
29  /// - second candle: long white with no or very short upper shadow opening within or near the previous white real body
30  /// and closing higher than the prior candle
31  /// - third candle: small white that gaps away or "rides on the shoulder" of the prior long real body(= it's at
32  /// the upper end of the prior real body)
33  /// The meanings of "long", "very short", "short", "near" are specified with SetCandleSettings;
34  /// The returned value is negative(-1): stalled pattern is always bearish;
35  /// The user should consider that stalled pattern is significant when it appears in uptrend, while this function
36  /// does not consider it
37  /// </remarks>
39  {
40  private readonly int _bodyLongAveragePeriod;
41  private readonly int _bodyShortAveragePeriod;
42  private readonly int _shadowVeryShortAveragePeriod;
43  private readonly int _nearAveragePeriod;
44 
45  private decimal[] _bodyLongPeriodTotal = new decimal[3];
46  private decimal _bodyShortPeriodTotal;
47  private decimal _shadowVeryShortPeriodTotal;
48  private decimal[] _nearPeriodTotal = new decimal[3];
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="StalledPattern"/> class using the specified name.
52  /// </summary>
53  /// <param name="name">The name of this indicator</param>
54  public StalledPattern(string name)
55  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod),
56  Math.Max(CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod, CandleSettings.Get(CandleSettingType.Near).AveragePeriod)) + 2 + 1)
57  {
58  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
59  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
60  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
61  _nearAveragePeriod = CandleSettings.Get(CandleSettingType.Near).AveragePeriod;
62  }
63 
64  /// <summary>
65  /// Initializes a new instance of the <see cref="StalledPattern"/> class.
66  /// </summary>
67  public StalledPattern()
68  : this("STALLEDPATTERN")
69  {
70  }
71 
72  /// <summary>
73  /// Gets a flag indicating when this indicator is ready and fully initialized
74  /// </summary>
75  public override bool IsReady
76  {
77  get { return Samples >= Period; }
78  }
79 
80  /// <summary>
81  /// Computes the next value of this indicator from the given state
82  /// </summary>
83  /// <param name="window">The window of data held in this indicator</param>
84  /// <param name="input">The input given to the indicator</param>
85  /// <returns>A new value for this indicator</returns>
86  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
87  {
88  if (!IsReady)
89  {
90  if (Samples >= Period - _bodyLongAveragePeriod)
91  {
92  _bodyLongPeriodTotal[2] += GetCandleRange(CandleSettingType.BodyLong, window[2]);
93  _bodyLongPeriodTotal[1] += GetCandleRange(CandleSettingType.BodyLong, window[1]);
94  }
95 
96  if (Samples >= Period - _bodyShortAveragePeriod)
97  {
98  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input);
99  }
100 
101  if (Samples >= Period - _shadowVeryShortAveragePeriod)
102  {
103  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
104  }
105 
106  if (Samples >= Period - _nearAveragePeriod)
107  {
108  _nearPeriodTotal[2] += GetCandleRange(CandleSettingType.Near, window[2]);
109  _nearPeriodTotal[1] += GetCandleRange(CandleSettingType.Near, window[1]);
110  }
111 
112  return 0m;
113  }
114 
115  decimal value;
116  if (
117  // 1st white
118  GetCandleColor(window[2]) == CandleColor.White &&
119  // 2nd white
120  GetCandleColor(window[1]) == CandleColor.White &&
121  // 3rd white
122  GetCandleColor(input) == CandleColor.White &&
123  // consecutive higher closes
124  input.Close > window[1].Close && window[1].Close > window[2].Close &&
125  // 1st: long real body
126  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[2], window[2]) &&
127  // 2nd: long real body
128  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal[1], window[1]) &&
129  // very short upper shadow
130  GetUpperShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, window[1]) &&
131  // opens within/near 1st real body
132  window[1].Open > window[2].Open &&
133  window[1].Open <= window[2].Close + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[2], window[2]) &&
134  // 3rd: small real body
135  GetRealBody(input) < GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, input) &&
136  // rides on the shoulder of 2nd real body
137  input.Open >= window[1].Close - GetRealBody(input) - GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[1], window[1])
138  )
139  value = -1m;
140  else
141  value = 0m;
142 
143  // add the current range and subtract the first range: this is done after the pattern recognition
144  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
145 
146  for (var i = 2; i >= 1; i--)
147  {
148  _bodyLongPeriodTotal[i] += GetCandleRange(CandleSettingType.BodyLong, window[i]) -
149  GetCandleRange(CandleSettingType.BodyLong, window[i + _bodyLongAveragePeriod]);
150  _nearPeriodTotal[i] += GetCandleRange(CandleSettingType.Near, window[i]) -
151  GetCandleRange(CandleSettingType.Near, window[i + _nearAveragePeriod]);
152  }
153 
154  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input) -
155  GetCandleRange(CandleSettingType.BodyShort, window[_bodyShortAveragePeriod]);
156 
157  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]) -
158  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_bodyShortAveragePeriod + 1]);
159 
160  return value;
161  }
162 
163  /// <summary>
164  /// Resets this indicator to its initial state
165  /// </summary>
166  public override void Reset()
167  {
168  _bodyLongPeriodTotal = new decimal[3];
169  _bodyShortPeriodTotal = 0;
170  _shadowVeryShortPeriodTotal = 0;
171  _nearPeriodTotal = new decimal[3];
172  base.Reset();
173  }
174  }
175 }