Lean  $LEAN_TAG$
SeparatingLines.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  /// Separating Lines candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: black (white) candle
27  /// - second candle: bullish(bearish) belt hold with the same open as the prior candle
28  /// The meaning of "long body" and "very short shadow" of the belt hold is specified with SetCandleSettings
29  /// The returned value is positive(+1) when bullish or negative(-1) when bearish;
30  /// The user should consider that separating lines is significant when coming in a trend and the belt hold has
31  /// the same direction of the trend, while this function does not consider it
32  /// </remarks>
34  {
35  private readonly int _shadowVeryShortAveragePeriod;
36  private readonly int _bodyLongAveragePeriod;
37  private readonly int _equalAveragePeriod;
38 
39  private decimal _shadowVeryShortPeriodTotal;
40  private decimal _bodyLongPeriodTotal;
41  private decimal _equalPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="SeparatingLines"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public SeparatingLines(string name)
48  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod),
49  CandleSettings.Get(CandleSettingType.Equal).AveragePeriod) + 1 + 1)
50  {
51  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
52  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
53  _equalAveragePeriod = CandleSettings.Get(CandleSettingType.Equal).AveragePeriod;
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the <see cref="SeparatingLines"/> class.
58  /// </summary>
59  public SeparatingLines()
60  : this("SEPARATINGLINES")
61  {
62  }
63 
64  /// <summary>
65  /// Gets a flag indicating when this indicator is ready and fully initialized
66  /// </summary>
67  public override bool IsReady
68  {
69  get { return Samples >= Period; }
70  }
71 
72  /// <summary>
73  /// Computes the next value of this indicator from the given state
74  /// </summary>
75  /// <param name="window">The window of data held in this indicator</param>
76  /// <param name="input">The input given to the indicator</param>
77  /// <returns>A new value for this indicator</returns>
78  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
79  {
80  if (!IsReady)
81  {
82  if (Samples >= Period - _shadowVeryShortAveragePeriod)
83  {
84  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
85  }
86 
87  if (Samples >= Period - _bodyLongAveragePeriod)
88  {
89  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
90  }
91 
92  if (Samples >= Period - _equalAveragePeriod)
93  {
94  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]);
95  }
96 
97  return 0m;
98  }
99 
100  decimal value;
101  if (
102  // opposite candles
103  (int)GetCandleColor(window[1]) == -(int)GetCandleColor(input) &&
104  // same open
105  input.Open <= window[1].Open + GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[1]) &&
106  input.Open >= window[1].Open - GetCandleAverage(CandleSettingType.Equal, _equalPeriodTotal, window[1]) &&
107  // belt hold: long body
108  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, input) &&
109  (
110  // with no lower shadow if bullish
111  (GetCandleColor(input) == CandleColor.White &&
112  GetLowerShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
113  )
114  ||
115  // with no upper shadow if bearish
116  (GetCandleColor(input) == CandleColor.Black &&
117  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input)
118  )
119  )
120  )
121  value = (int)GetCandleColor(input);
122  else
123  value = 0m;
124 
125  // add the current range and subtract the first range: this is done after the pattern recognition
126  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
127 
128  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input) -
129  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod]);
130 
131  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input) -
132  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod]);
133 
134  _equalPeriodTotal += GetCandleRange(CandleSettingType.Equal, window[1]) -
135  GetCandleRange(CandleSettingType.Equal, window[_equalAveragePeriod + 1]);
136 
137  return value;
138  }
139 
140  /// <summary>
141  /// Resets this indicator to its initial state
142  /// </summary>
143  public override void Reset()
144  {
145  _shadowVeryShortPeriodTotal = 0m;
146  _bodyLongPeriodTotal = 0m;
147  _equalPeriodTotal = 0m;
148  base.Reset();
149  }
150  }
151 }