Lean  $LEAN_TAG$
DojiStar.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  /// Doji Star candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: long real body
27  /// - second candle: star(open gapping up in an uptrend or down in a downtrend) with a doji
28  /// The meaning of "doji" and "long" is specified with SetCandleSettings
29  /// The returned value is positive(+1) when bullish or negative(-1) when bearish;
30  /// it's defined bullish when the long candle is white and the star gaps up, bearish when the long candle
31  /// is black and the star gaps down; the user should consider that a doji star is bullish when it appears
32  /// in an uptrend and it's bearish when it appears in a downtrend, so to determine the bullishness or
33  /// bearishness of the pattern the trend must be analyzed
34  /// </remarks>
36  {
37  private readonly int _bodyLongAveragePeriod;
38  private readonly int _bodyDojiAveragePeriod;
39 
40  private decimal _bodyLongPeriodTotal;
41  private decimal _bodyDojiPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="DojiStar"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public DojiStar(string name)
48  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod) + 1 + 1)
49  {
50  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
51  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
52  }
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="DojiStar"/> class.
56  /// </summary>
57  public DojiStar()
58  : this("DOJISTAR")
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 - _bodyLongAveragePeriod - 1 && Samples < Period - 1)
81  {
82  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
83  }
84 
85  if (Samples >= Period - _bodyDojiAveragePeriod)
86  {
87  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
88  }
89 
90  return 0m;
91  }
92 
93  decimal value;
94  if (
95  // 1st: long real body
96  GetRealBody(window[1]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[1]) &&
97  // 2nd: doji
98  GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) &&
99  // that gaps up if 1st is white
100  ((GetCandleColor(window[1]) == CandleColor.White && GetRealBodyGapUp(input, window[1]))
101  ||
102  // or down if 1st is black
103  (GetCandleColor(window[1]) == CandleColor.Black && GetRealBodyGapDown(input, window[1]))
104  ))
105  value = -(int)GetCandleColor(window[1]);
106  else
107  value = 0m;
108 
109  // add the current range and subtract the first range: this is done after the pattern recognition
110  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
111 
112  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[1]) -
113  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 1]);
114 
115  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input) -
116  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod]);
117 
118  return value;
119  }
120 
121  /// <summary>
122  /// Resets this indicator to its initial state
123  /// </summary>
124  public override void Reset()
125  {
126  _bodyLongPeriodTotal = 0m;
127  _bodyDojiPeriodTotal = 0m;
128  base.Reset();
129  }
130  }
131 }