Lean  $LEAN_TAG$
MorningDojiStar.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  /// Morning Doji Star candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - first candle: long black real body
28  /// - second candle: doji gapping down
29  /// - third candle: white real body that moves well within the first candle's real body
30  /// The meaning of "doji" and "long" is specified with SetCandleSettings
31  /// The meaning of "moves well within" is specified with penetration and "moves" should mean the real body should
32  /// not be short ("short" is specified with SetCandleSettings) - Greg Morris wants it to be long, someone else want
33  /// it to be relatively long
34  /// The returned value is positive(+1): morning doji star is always bullish;
35  /// the user should consider that a morning star is significant when it appears in a downtrend,
36  /// while this function does not consider the trend
37  /// </remarks>
39  {
40  private readonly decimal _penetration;
41 
42  private readonly int _bodyDojiAveragePeriod;
43  private readonly int _bodyLongAveragePeriod;
44  private readonly int _bodyShortAveragePeriod;
45 
46  private decimal _bodyDojiPeriodTotal;
47  private decimal _bodyLongPeriodTotal;
48  private decimal _bodyShortPeriodTotal;
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="MorningDojiStar"/> class using the specified name.
52  /// </summary>
53  /// <param name="name">The name of this indicator</param>
54  /// <param name="penetration">Percentage of penetration of a candle within another candle</param>
55  public MorningDojiStar(string name, decimal penetration = 0.3m)
56  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod),
57  CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod) + 2 + 1)
58  {
59  _penetration = penetration;
60 
61  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
62  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
63  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
64  }
65 
66  /// <summary>
67  /// Initializes a new instance of the <see cref="MorningDojiStar"/> class.
68  /// </summary>
69  /// <param name="penetration">Percentage of penetration of a candle within another candle</param>
70  public MorningDojiStar(decimal penetration)
71  : this("MORNINGDOJISTAR", penetration)
72  {
73  }
74 
75  /// <summary>
76  /// Initializes a new instance of the <see cref="MorningDojiStar"/> class.
77  /// </summary>
78  public MorningDojiStar()
79  : this("MORNINGDOJISTAR")
80  {
81  }
82 
83  /// <summary>
84  /// Gets a flag indicating when this indicator is ready and fully initialized
85  /// </summary>
86  public override bool IsReady
87  {
88  get { return Samples >= Period; }
89  }
90 
91  /// <summary>
92  /// Computes the next value of this indicator from the given state
93  /// </summary>
94  /// <param name="window">The window of data held in this indicator</param>
95  /// <param name="input">The input given to the indicator</param>
96  /// <returns>A new value for this indicator</returns>
97  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
98  {
99  if (!IsReady)
100  {
101  if (Samples >= Period - _bodyLongAveragePeriod - 2 && Samples < Period - 2)
102  {
103  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
104  }
105 
106  if (Samples >= Period - _bodyDojiAveragePeriod - 1 && Samples < Period - 1)
107  {
108  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
109  }
110 
111  if (Samples >= Period - _bodyShortAveragePeriod)
112  {
113  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input);
114  }
115 
116  return 0m;
117  }
118 
119  decimal value;
120  if (
121  // 1st: long
122  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[2]) &&
123  // black
124  GetCandleColor(window[2]) == CandleColor.Black &&
125  // 2nd: doji
126  GetRealBody(window[1]) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, window[1]) &&
127  // gapping down
128  GetRealBodyGapDown(window[1], window[2]) &&
129  // 3rd: longer than short
130  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, input) &&
131  // white real body
132  GetCandleColor(input) == CandleColor.White &&
133  // closing well within 1st rb
134  input.Close > window[2].Close + GetRealBody(window[2]) * _penetration
135  )
136  value = 1m;
137  else
138  value = 0m;
139 
140  // add the current range and subtract the first range: this is done after the pattern recognition
141  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
142 
143  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
144  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 2]);
145 
146  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, window[1]) -
147  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod + 1]);
148 
149  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input) -
150  GetCandleRange(CandleSettingType.BodyShort, window[_bodyShortAveragePeriod]);
151 
152  return value;
153  }
154 
155  /// <summary>
156  /// Resets this indicator to its initial state
157  /// </summary>
158  public override void Reset()
159  {
160  _bodyLongPeriodTotal = 0;
161  _bodyDojiPeriodTotal = 0;
162  _bodyShortPeriodTotal = 0;
163  base.Reset();
164  }
165  }
166 }