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