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