Lean  $LEAN_TAG$
AdvanceBlock.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  /// Advance Block candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - three white candlesticks with consecutively higher closes
28  /// - each candle opens within or near the previous white real body
29  /// - first candle: long white with no or very short upper shadow(a short shadow is accepted too for more flexibility)
30  /// - second and third candles, or only third candle, show signs of weakening: progressively smaller white real bodies
31  /// and/or relatively long upper shadows; see below for specific conditions
32  /// The meanings of "long body", "short shadow", "far" and "near" are specified with SetCandleSettings;
33  /// The returned value is negative(-1): advance block is always bearish;
34  /// The user should consider that advance block is significant when it appears in uptrend, while this function
35  /// does not consider it
36  /// </remarks>
38  {
39  private readonly int _shadowShortAveragePeriod;
40  private readonly int _shadowLongAveragePeriod;
41  private readonly int _nearAveragePeriod;
42  private readonly int _farAveragePeriod;
43  private readonly int _bodyLongAveragePeriod;
44 
45  private decimal[] _shadowShortPeriodTotal = new decimal[3];
46  private decimal[] _shadowLongPeriodTotal = new decimal[2];
47  private decimal[] _nearPeriodTotal = new decimal[3];
48  private decimal[] _farPeriodTotal = new decimal[3];
49  private decimal _bodyLongPeriodTotal;
50 
51  /// <summary>
52  /// Initializes a new instance of the <see cref="AdvanceBlock"/> class using the specified name.
53  /// </summary>
54  /// <param name="name">The name of this indicator</param>
55  public AdvanceBlock(string name)
56  : base(name, Math.Max(Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowShort).AveragePeriod),
57  Math.Max(CandleSettings.Get(CandleSettingType.Far).AveragePeriod, CandleSettings.Get(CandleSettingType.Near).AveragePeriod)),
58  CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 2 + 1)
59  {
60  _shadowShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowShort).AveragePeriod;
61  _shadowLongAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod;
62  _nearAveragePeriod = CandleSettings.Get(CandleSettingType.Near).AveragePeriod;
63  _farAveragePeriod = CandleSettings.Get(CandleSettingType.Far).AveragePeriod;
64  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
65  }
66 
67  /// <summary>
68  /// Initializes a new instance of the <see cref="AdvanceBlock"/> class.
69  /// </summary>
70  public AdvanceBlock()
71  : this("ADVANCEBLOCK")
72  {
73  }
74 
75  /// <summary>
76  /// Gets a flag indicating when this indicator is ready and fully initialized
77  /// </summary>
78  public override bool IsReady
79  {
80  get { return Samples >= Period; }
81  }
82 
83  /// <summary>
84  /// Computes the next value of this indicator from the given state
85  /// </summary>
86  /// <param name="window">The window of data held in this indicator</param>
87  /// <param name="input">The input given to the indicator</param>
88  /// <returns>A new value for this indicator</returns>
89  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
90  {
91  if (!IsReady)
92  {
93  if (Samples >= Period - _shadowShortAveragePeriod)
94  {
95  _shadowShortPeriodTotal[2] += GetCandleRange(CandleSettingType.ShadowShort, window[2]);
96  _shadowShortPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowShort, window[1]);
97  _shadowShortPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowShort, input);
98  }
99 
100  if (Samples >= Period - _shadowLongAveragePeriod)
101  {
102  _shadowLongPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowLong, window[1]);
103  _shadowLongPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowLong, input);
104  }
105 
106  if (Samples >= Period - _bodyLongAveragePeriod)
107  {
108  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]);
109  }
110 
111  if (Samples >= Period - _nearAveragePeriod)
112  {
113  _nearPeriodTotal[2] += GetCandleRange(CandleSettingType.Near, window[2]);
114  _nearPeriodTotal[1] += GetCandleRange(CandleSettingType.Near, window[1]);
115  }
116 
117  if (Samples >= Period - _farAveragePeriod)
118  {
119  _farPeriodTotal[2] += GetCandleRange(CandleSettingType.Far, window[2]);
120  _farPeriodTotal[1] += GetCandleRange(CandleSettingType.Far, window[1]);
121  }
122 
123  return 0m;
124  }
125 
126  decimal value;
127  if (
128  // 1st white
129  GetCandleColor(window[2]) == CandleColor.White &&
130  // 2nd white
131  GetCandleColor(window[1]) == CandleColor.White &&
132  // 3rd white
133  GetCandleColor(input) == CandleColor.White &&
134  // consecutive higher closes
135  input.Close > window[1].Close && window[1].Close > window[2].Close &&
136  // 2nd opens within/near 1st real body
137  window[1].Open > window[2].Open &&
138  window[1].Open <= window[2].Close + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[2], window[2]) &&
139  // 3rd opens within/near 2nd real body
140  input.Open > window[1].Open &&
141  input.Open <= window[1].Close + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[1], window[1]) &&
142  // 1st: long real body
143  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[2]) &&
144  // 1st: short upper shadow
145  GetUpperShadow(window[2]) < GetCandleAverage(CandleSettingType.ShadowShort, _shadowShortPeriodTotal[2], window[2]) &&
146  (
147  // ( 2 far smaller than 1 && 3 not longer than 2 )
148  // advance blocked with the 2nd, 3rd must not carry on the advance
149  (
150  GetRealBody(window[1]) < GetRealBody(window[2]) - GetCandleAverage(CandleSettingType.Far, _farPeriodTotal[2], window[2]) &&
151  GetRealBody(input) < GetRealBody(window[1]) + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[1], window[1])
152  ) ||
153  // 3 far smaller than 2
154  // advance blocked with the 3rd
155  (
156  GetRealBody(input) < GetRealBody(window[1]) - GetCandleAverage(CandleSettingType.Far, _farPeriodTotal[1], window[1])
157  ) ||
158  // ( 3 smaller than 2 && 2 smaller than 1 && (3 or 2 not short upper shadow) )
159  // advance blocked with progressively smaller real bodies and some upper shadows
160  (
161  GetRealBody(input) < GetRealBody(window[1]) &&
162  GetRealBody(window[1]) < GetRealBody(window[2]) &&
163  (
164  GetUpperShadow(input) > GetCandleAverage(CandleSettingType.ShadowShort, _shadowShortPeriodTotal[0], input) ||
165  GetUpperShadow(window[1]) > GetCandleAverage(CandleSettingType.ShadowShort, _shadowShortPeriodTotal[1], window[1])
166  )
167  ) ||
168  // ( 3 smaller than 2 && 3 long upper shadow )
169  // advance blocked with 3rd candle's long upper shadow and smaller body
170  (
171  GetRealBody(input) < GetRealBody(window[1]) &&
172  GetUpperShadow(input) > GetCandleAverage(CandleSettingType.ShadowLong, _shadowLongPeriodTotal[0], input)
173  )
174  )
175  )
176  value = -1m;
177  else
178  value = 0m;
179 
180  // add the current range and subtract the first range: this is done after the pattern recognition
181  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
182 
183  for (var i = 2; i >= 0; i--)
184  {
185  _shadowShortPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowShort, window[i]) -
186  GetCandleRange(CandleSettingType.ShadowShort, window[i + _shadowShortAveragePeriod]);
187  }
188 
189  for (var i = 1; i >= 0; i--)
190  {
191  _shadowLongPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowLong, window[i]) -
192  GetCandleRange(CandleSettingType.ShadowLong, window[i + _shadowLongAveragePeriod]);
193  }
194 
195  for (var i = 2; i >= 1; i--)
196  {
197  _farPeriodTotal[i] += GetCandleRange(CandleSettingType.Far, window[i]) -
198  GetCandleRange(CandleSettingType.Far, window[i + _farAveragePeriod]);
199  _nearPeriodTotal[i] += GetCandleRange(CandleSettingType.Near, window[i]) -
200  GetCandleRange(CandleSettingType.Near, window[i + _nearAveragePeriod]);
201  }
202 
203  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
204  GetCandleRange(CandleSettingType.BodyLong, window[2 + _bodyLongAveragePeriod]);
205 
206  return value;
207  }
208 
209  /// <summary>
210  /// Resets this indicator to its initial state
211  /// </summary>
212  public override void Reset()
213  {
214  _shadowShortPeriodTotal = new decimal[3];
215  _shadowLongPeriodTotal = new decimal[2];
216  _nearPeriodTotal = new decimal[3];
217  _farPeriodTotal = new decimal[3];
218  _bodyLongPeriodTotal = 0;
219  base.Reset();
220  }
221  }
222 }