Lean  $LEAN_TAG$
ClosingMarubozu.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  /// Closing Marubozu candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - long white(black) real body
27  /// - no or very short upper(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="ClosingMarubozu"/> class using the specified name.
41  /// </summary>
42  /// <param name="name">The name of this indicator</param>
43  public ClosingMarubozu(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="ClosingMarubozu"/> class.
52  /// </summary>
53  public ClosingMarubozu()
54  : this("CLOSINGMARUBOZU")
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 (
91  // long body
92  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, input) &&
93  (
94  (
95  // white body and very short upper shadow
96  GetCandleColor(input) == CandleColor.White &&
97  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
98  ) ||
99  (
100  // black body and very short lower shadow
101  GetCandleColor(input) == CandleColor.Black &&
102  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
103  )
104  ))
105  value = (int)GetCandleColor(input);
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, input) -
113  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod]);
114 
115  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input) -
116  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod]);
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  _shadowVeryShortPeriodTotal = 0m;
128  base.Reset();
129  }
130  }
131 }