Lean  $LEAN_TAG$
Doji.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 
17 
19 {
20  /// <summary>
21  /// Doji candlestick pattern indicator
22  /// </summary>
23  /// <remarks>
24  /// Must have:
25  /// - open quite equal to close
26  /// How much can be the maximum distance between open and close is specified with SetCandleSettings
27  /// The returned value is always positive(+1) but this does not mean it is bullish: doji shows uncertainty and it is
28  /// neither bullish nor bearish when considered alone
29  /// </remarks>
30  public class Doji : CandlestickPattern
31  {
32  private readonly int _bodyDojiAveragePeriod;
33 
34  private decimal _bodyDojiPeriodTotal;
35 
36  /// <summary>
37  /// Initializes a new instance of the <see cref="Doji"/> class using the specified name.
38  /// </summary>
39  /// <param name="name">The name of this indicator</param>
40  public Doji(string name)
41  : base(name, CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod + 1)
42  {
43  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
44  }
45 
46  /// <summary>
47  /// Initializes a new instance of the <see cref="Doji"/> class.
48  /// </summary>
49  public Doji()
50  : this("DOJI")
51  {
52  }
53 
54  /// <summary>
55  /// Gets a flag indicating when this indicator is ready and fully initialized
56  /// </summary>
57  public override bool IsReady
58  {
59  get { return Samples >= Period; }
60  }
61 
62  /// <summary>
63  /// Computes the next value of this indicator from the given state
64  /// </summary>
65  /// <param name="window">The window of data held in this indicator</param>
66  /// <param name="input">The input given to the indicator</param>
67  /// <returns>A new value for this indicator</returns>
68  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
69  {
70  if (!IsReady)
71  {
72  if (Samples >= Period - _bodyDojiAveragePeriod)
73  {
74  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
75  }
76 
77  return 0m;
78  }
79 
80  var value = GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) ? 1m : 0m;
81 
82  // add the current range and subtract the first range: this is done after the pattern recognition
83  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
84 
85  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input) -
86  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod]);
87 
88  return value;
89  }
90 
91  /// <summary>
92  /// Resets this indicator to its initial state
93  /// </summary>
94  public override void Reset()
95  {
96  _bodyDojiPeriodTotal = 0m;
97  base.Reset();
98  }
99  }
100 }