Lean  $LEAN_TAG$
Marubozu.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  /// Marubozu candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - long real body
27  /// - no or very short upper and lower shadow
28  /// The meaning of "long" and "very short" is specified with SetCandleSettings
29  /// The returned value is positive(+1) when white(bullish), negative(-1) when black(bearish)
30  /// </remarks>
32  {
33  private readonly int _bodyLongAveragePeriod;
34  private readonly int _shadowVeryShortAveragePeriod;
35 
36  private decimal _bodyLongPeriodTotal;
37  private decimal _shadowVeryShortPeriodTotal;
38 
39  /// <summary>
40  /// Initializes a new instance of the <see cref="Marubozu"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public Marubozu(string name)
44  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod) + 1)
45  {
46  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
47  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
48  }
49 
50  /// <summary>
51  /// Initializes a new instance of the <see cref="Marubozu"/> class.
52  /// </summary>
53  public Marubozu()
54  : this("MARUBOZU")
55  {
56  }
57 
58  /// <summary>
59  /// Gets a flag indicating when this indicator is ready and fully initialized
60  /// </summary>
61  public override bool IsReady
62  {
63  get { return Samples >= Period; }
64  }
65 
66  /// <summary>
67  /// Computes the next value of this indicator from the given state
68  /// </summary>
69  /// <param name="window">The window of data held in this indicator</param>
70  /// <param name="input">The input given to the indicator</param>
71  /// <returns>A new value for this indicator</returns>
72  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
73  {
74  if (!IsReady)
75  {
76  if (Samples >= Period - _bodyLongAveragePeriod)
77  {
78  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
79  }
80 
81  if (Samples >= Period - _shadowVeryShortAveragePeriod)
82  {
83  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
84  }
85 
86  return 0m;
87  }
88 
89  decimal value;
90  if (GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, input) &&
91  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input) &&
92  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
93  )
94  value = (int)GetCandleColor(input);
95  else
96  value = 0m;
97 
98  // add the current range and subtract the first range: this is done after the pattern recognition
99  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
100 
101  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input) -
102  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod]);
103 
104  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input) -
105  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod]);
106 
107  return value;
108  }
109 
110  /// <summary>
111  /// Resets this indicator to its initial state
112  /// </summary>
113  public override void Reset()
114  {
115  _bodyLongPeriodTotal = 0m;
116  _shadowVeryShortPeriodTotal = 0m;
117  base.Reset();
118  }
119  }
120 }