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