Lean  $LEAN_TAG$
DragonflyDoji.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  /// Dragonfly Doji candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - doji body
27  /// - open and close at the high of the day = no or very short upper shadow
28  /// - lower shadow(to distinguish from other dojis, here lower shadow should not be very short)
29  /// The meaning of "doji" and "very short" is specified with SetCandleSettings
30  /// The returned value is always positive(+1) but this does not mean it is bullish: dragonfly doji must be considered
31  /// relatively to the trend
32  /// </remarks>
34  {
35  private readonly int _bodyDojiAveragePeriod;
36  private readonly int _shadowVeryShortAveragePeriod;
37 
38  private decimal _bodyDojiPeriodTotal;
39  private decimal _shadowVeryShortPeriodTotal;
40 
41  /// <summary>
42  /// Initializes a new instance of the <see cref="DragonflyDoji"/> class using the specified name.
43  /// </summary>
44  /// <param name="name">The name of this indicator</param>
45  public DragonflyDoji(string name)
46  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod) + 1)
47  {
48  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
49  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
50  }
51 
52  /// <summary>
53  /// Initializes a new instance of the <see cref="DragonflyDoji"/> class.
54  /// </summary>
55  public DragonflyDoji()
56  : this("DRAGONFLYDOJI")
57  {
58  }
59 
60  /// <summary>
61  /// Gets a flag indicating when this indicator is ready and fully initialized
62  /// </summary>
63  public override bool IsReady
64  {
65  get { return Samples >= Period; }
66  }
67 
68  /// <summary>
69  /// Computes the next value of this indicator from the given state
70  /// </summary>
71  /// <param name="window">The window of data held in this indicator</param>
72  /// <param name="input">The input given to the indicator</param>
73  /// <returns>A new value for this indicator</returns>
74  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
75  {
76  if (!IsReady)
77  {
78  if (Samples >= Period - _bodyDojiAveragePeriod)
79  {
80  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
81  }
82 
83  if (Samples >= Period - _shadowVeryShortAveragePeriod)
84  {
85  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
86  }
87 
88  return 0m;
89  }
90 
91  decimal value;
92  if (GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) &&
93  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input) &&
94  GetLowerShadow(input) > GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
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  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input) -
107  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod]);
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  _shadowVeryShortPeriodTotal = 0m;
119  base.Reset();
120  }
121  }
122 }