Lean  $LEAN_TAG$
Thrusting.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 using System;
18 
20 {
21  /// <summary>
22  /// Thrusting candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: long black candle
27  /// - second candle: white candle with open below previous day low and close into previous day body under the midpoint;
28  /// to differentiate it from in-neck the close should not be equal to the black candle's close
29  /// The meaning of "equal" is specified with SetCandleSettings
30  /// The returned value is negative(-1): thrusting pattern is always bearish
31  /// The user should consider that the thrusting pattern is significant when it appears in a downtrend and it could be
32  /// even bullish "when coming in an uptrend or occurring twice within several days" (Steve Nison says), while this
33  /// function does not consider the trend
34  /// </remarks>
36  {
37  private readonly int _equalAveragePeriod;
38  private readonly int _bodyLongAveragePeriod;
39 
40  private decimal _equalPeriodTotal;
41  private decimal _bodyLongPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="Thrusting"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public Thrusting(string name)
48  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.Equal).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 1 + 1)
49  {
50  _equalAveragePeriod = CandleSettings.Get(CandleSettingType.Equal).AveragePeriod;
51  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
52  }
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="Thrusting"/> class.
56  /// </summary>
57  public Thrusting()
58  : this("THRUSTING")
59  {
60  }
61 
62  /// <summary>
63  /// Gets a flag indicating when this indicator is ready and fully initialized
64  /// </summary>
65  public override bool IsReady
66  {
67  get { return Samples >= Period; }
68  }
69 
70  /// <summary>
71  /// Computes the next value of this indicator from the given state
72  /// </summary>
73  /// <param name="window">The window of data held in this indicator</param>
74  /// <param name="input">The input given to the indicator</param>
75  /// <returns>A new value for this indicator</returns>
76  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
77  {
78  if (!IsReady)
79  {
80  if (Samples >= Period - _equalAveragePeriod)
81  {
82  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]);
83  }
84 
85  if (Samples >= Period - _bodyLongAveragePeriod)
86  {
87  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[1]);
88  }
89 
90  return 0m;
91  }
92 
93  decimal value;
94  if (
95  // 1st: black
96  GetCandleColor(window[1]) == CandleColor.Black &&
97  // long
98  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[1]) &&
99  // 2nd: white
100  GetCandleColor(input) == CandleColor.White &&
101  // open below prior low
102  input.Open < window[1].Low &&
103  // close into prior body
104  input.Close > window[1].Close + GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[1]) &&
105  // under the midpoint
106  input.Close <= window[1].Close + GetRealBody(window[1]) * 0.5m
107  )
108  value = -1m;
109  else
110  value = 0m;
111 
112  // add the current range and subtract the first range: this is done after the pattern recognition
113  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
114 
115  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]) -
116  GetCandleRange(CandleSettingType.Equal, window[_equalAveragePeriod + 1]);
117 
118  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[1]) -
119  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 1]);
120 
121  return value;
122  }
123 
124  /// <summary>
125  /// Resets this indicator to its initial state
126  /// </summary>
127  public override void Reset()
128  {
129  _equalPeriodTotal = 0m;
130  _bodyLongPeriodTotal = 0m;
131  base.Reset();
132  }
133  }
134 }