Lean  $LEAN_TAG$
LongLeggedDoji.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  /// Long Legged Doji candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - doji body
27  /// - one or two long shadows
28  /// The meaning of "doji" is specified with SetCandleSettings
29  /// The returned value is always positive(+1) but this does not mean it is bullish: long legged doji shows uncertainty
30  /// </remarks>
32  {
33  private readonly int _bodyDojiAveragePeriod;
34  private readonly int _shadowLongAveragePeriod;
35 
36  private decimal _bodyDojiPeriodTotal;
37  private decimal _shadowLongPeriodTotal;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="LongLeggedDoji"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public LongLeggedDoji(string name)
44  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod) + 1)
45  {
46  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
47  _shadowLongAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod;
48  }
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="LongLeggedDoji"/> class.
52  /// </summary>
53  public LongLeggedDoji()
54  : this("LONGLEGGEDDOJI")
55  {
56  }
57 
58  /// <summary>
59  /// Gets a flag indicating when this indicator is ready and fully initialized
60  /// </summary>
61  public override bool IsReady
62  {
63  get { return Samples >= Period; }
64  }
65 
66  /// <summary>
67  /// Computes the next value of this indicator from the given state
68  /// </summary>
69  /// <param name="window">The window of data held in this indicator</param>
70  /// <param name="input">The input given to the indicator</param>
71  /// <returns>A new value for this indicator</returns>
72  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
73  {
74  if (!IsReady)
75  {
76  if (Samples >= Period - _bodyDojiAveragePeriod)
77  {
78  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
79  }
80 
81  if (Samples >= Period - _shadowLongAveragePeriod)
82  {
83  _shadowLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowLong, input);
84  }
85 
86  return 0m;
87  }
88 
89  decimal value;
90  if (GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) &&
91  (GetLowerShadow(input) > GetCandleAverage(CandleSettingType.ShadowLong, _shadowLongPeriodTotal, input)
92  ||
93  GetUpperShadow(input) > GetCandleAverage(CandleSettingType.ShadowLong, _shadowLongPeriodTotal, input)
94  )
95  )
96  value = 1m;
97  else
98  value = 0m;
99 
100  // add the current range and subtract the first range: this is done after the pattern recognition
101  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
102 
103  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input) -
104  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod]);
105 
106  _shadowLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowLong, input) -
107  GetCandleRange(CandleSettingType.ShadowLong, window[_shadowLongAveragePeriod]);
108 
109  return value;
110  }
111 
112  /// <summary>
113  /// Resets this indicator to its initial state
114  /// </summary>
115  public override void Reset()
116  {
117  _bodyDojiPeriodTotal = 0m;
118  _shadowLongPeriodTotal = 0m;
119  base.Reset();
120  }
121  }
122 }