Lean  $LEAN_TAG$
ThreeWhiteSoldiers.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  /// Three Advancing White Soldiers candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - three white candlesticks with consecutively higher closes
28  /// - Greg Morris wants them to be long, Steve Nison doesn't; anyway they should not be short
29  /// - each candle opens within or near the previous white real body
30  /// - each candle must have no or very short upper shadow
31  /// - to differentiate this pattern from advance block, each candle must not be far shorter than the prior candle
32  /// The meanings of "not short", "very short shadow", "far" and "near" are specified with SetCandleSettings;
33  /// here the 3 candles must be not short, if you want them to be long use SetCandleSettings on BodyShort;
34  /// The returned value is positive (+1): advancing 3 white soldiers is always bullish;
35  /// The user should consider that 3 white soldiers is significant when it appears in downtrend, while this function
36  /// does not consider it
37  /// </remarks>
39  {
40  private readonly int _shadowVeryShortAveragePeriod;
41  private readonly int _nearAveragePeriod;
42  private readonly int _farAveragePeriod;
43  private readonly int _bodyShortAveragePeriod;
44 
45  private decimal[] _shadowVeryShortPeriodTotal = new decimal[3];
46  private decimal[] _nearPeriodTotal = new decimal[3];
47  private decimal[] _farPeriodTotal = new decimal[3];
48  private decimal _bodyShortPeriodTotal;
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="ThreeWhiteSoldiers"/> class using the specified name.
52  /// </summary>
53  /// <param name="name">The name of this indicator</param>
54  public ThreeWhiteSoldiers(string name)
55  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod),
56  Math.Max(CandleSettings.Get(CandleSettingType.Far).AveragePeriod, CandleSettings.Get(CandleSettingType.Near).AveragePeriod)) + 2 + 1)
57  {
58  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
59  _nearAveragePeriod = CandleSettings.Get(CandleSettingType.Near).AveragePeriod;
60  _farAveragePeriod = CandleSettings.Get(CandleSettingType.Far).AveragePeriod;
61  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
62  }
63 
64  /// <summary>
65  /// Initializes a new instance of the <see cref="ThreeWhiteSoldiers"/> class.
66  /// </summary>
68  : this("THREEWHITESOLDIERS")
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 - _shadowVeryShortAveragePeriod)
91  {
92  _shadowVeryShortPeriodTotal[2] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[2]);
93  _shadowVeryShortPeriodTotal[1] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[1]);
94  _shadowVeryShortPeriodTotal[0] += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
95  }
96 
97  if (Samples >= Period - _nearAveragePeriod)
98  {
99  _nearPeriodTotal[2] += GetCandleRange(CandleSettingType.Near, window[2]);
100  _nearPeriodTotal[1] += GetCandleRange(CandleSettingType.Near, window[1]);
101  }
102 
103  if (Samples >= Period - _farAveragePeriod)
104  {
105  _farPeriodTotal[2] += GetCandleRange(CandleSettingType.Far, window[2]);
106  _farPeriodTotal[1] += GetCandleRange(CandleSettingType.Far, window[1]);
107  }
108 
109  if (Samples >= Period - _bodyShortAveragePeriod)
110  {
111  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input);
112  }
113 
114  return 0m;
115  }
116 
117  decimal value;
118  if (
119  // 1st white
120  GetCandleColor(window[2]) == CandleColor.White &&
121  // very short upper shadow
122  GetUpperShadow(window[2]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[2], window[2]) &&
123  // 2nd white
124  GetCandleColor(window[1]) == CandleColor.White &&
125  // very short upper shadow
126  GetUpperShadow(window[1]) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[1], window[1]) &&
127  // 3rd white
128  GetCandleColor(input) == CandleColor.White &&
129  // very short upper shadow
130  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal[0], input) &&
131  // consecutive higher closes
132  input.Close > window[1].Close && window[1].Close > window[2].Close &&
133  // 2nd opens within/near 1st real body
134  window[1].Open > window[2].Open &&
135  window[1].Open <= window[2].Close + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[2], window[2]) &&
136  // 3rd opens within/near 2nd real body
137  input.Open > window[1].Open &&
138  input.Open <= window[1].Close + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal[1], window[1]) &&
139  // 2nd not far shorter than 1st
140  GetRealBody(window[1]) > GetRealBody(window[2]) - GetCandleAverage(CandleSettingType.Far, _farPeriodTotal[2], window[2]) &&
141  // 3rd not far shorter than 2nd
142  GetRealBody(input) > GetRealBody(window[1]) - GetCandleAverage(CandleSettingType.Far, _farPeriodTotal[1], window[1]) &&
143  // not short real body
144  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, input)
145  )
146  value = 1m;
147  else
148  value = 0m;
149 
150  // add the current range and subtract the first range: this is done after the pattern recognition
151  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
152 
153  for (var i = 2; i >= 0; i--)
154  {
155  _shadowVeryShortPeriodTotal[i] += GetCandleRange(CandleSettingType.ShadowVeryShort, window[i]) -
156  GetCandleRange(CandleSettingType.ShadowVeryShort, window[i + _shadowVeryShortAveragePeriod]);
157  }
158 
159  for (var i = 2; i >= 1; i--)
160  {
161  _farPeriodTotal[i] += GetCandleRange(CandleSettingType.Far, window[i]) -
162  GetCandleRange(CandleSettingType.Far, window[i + _farAveragePeriod]);
163  }
164 
165  for (var i = 2; i >= 1; i--)
166  {
167  _nearPeriodTotal[i] += GetCandleRange(CandleSettingType.Near, window[i]) -
168  GetCandleRange(CandleSettingType.Near, window[i + _nearAveragePeriod]);
169  }
170 
171  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input) -
172  GetCandleRange(CandleSettingType.BodyShort, window[_bodyShortAveragePeriod]);
173 
174  return value;
175  }
176 
177  /// <summary>
178  /// Resets this indicator to its initial state
179  /// </summary>
180  public override void Reset()
181  {
182  _shadowVeryShortPeriodTotal = new decimal[3];
183  _nearPeriodTotal = new decimal[3];
184  _farPeriodTotal = new decimal[3];
185  _bodyShortPeriodTotal = 0;
186  base.Reset();
187  }
188  }
189 }