Lean  $LEAN_TAG$
ThreeInside.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  /// Three Inside Up/Down candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: long white(black) real body
27  /// - second candle: short real body totally engulfed by the first
28  /// - third candle: black(white) candle that closes lower(higher) than the first candle's open
29  /// The meaning of "short" and "long" is specified with SetCandleSettings
30  /// The returned value is positive (+1) for the three inside up or negative (-1) for the three inside down;
31  /// The user should consider that a three inside up is significant when it appears in a downtrend and a three inside
32  /// down is significant when it appears in an uptrend, while this function does not consider the trend
33  /// </remarks>
35  {
36  private readonly int _bodyLongAveragePeriod;
37  private readonly int _bodyShortAveragePeriod;
38 
39  private decimal _bodyLongPeriodTotal;
40  private decimal _bodyShortPeriodTotal;
41 
42  /// <summary>
43  /// Initializes a new instance of the <see cref="ThreeInside"/> class using the specified name.
44  /// </summary>
45  /// <param name="name">The name of this indicator</param>
46  public ThreeInside(string name)
47  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 2 + 1)
48  {
49  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
50  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
51  }
52 
53  /// <summary>
54  /// Initializes a new instance of the <see cref="ThreeInside"/> class.
55  /// </summary>
56  public ThreeInside()
57  : this("THREEINSIDE")
58  {
59  }
60 
61  /// <summary>
62  /// Gets a flag indicating when this indicator is ready and fully initialized
63  /// </summary>
64  public override bool IsReady
65  {
66  get { return Samples >= Period; }
67  }
68 
69  /// <summary>
70  /// Computes the next value of this indicator from the given state
71  /// </summary>
72  /// <param name="window">The window of data held in this indicator</param>
73  /// <param name="input">The input given to the indicator</param>
74  /// <returns>A new value for this indicator</returns>
75  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
76  {
77  if (!IsReady)
78  {
79  if (Samples >= Period - _bodyLongAveragePeriod - 2 && Samples < Period - 2)
80  {
81  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, input);
82  }
83 
84  if (Samples >= Period - _bodyShortAveragePeriod - 1 && Samples < Period - 1)
85  {
86  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input);
87  }
88 
89  return 0m;
90  }
91 
92  decimal value;
93  if (
94  // 1st: long
95  GetRealBody(window[2]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyLongPeriodTotal, window[2]) &&
96  // 2nd: short
97  GetRealBody(window[1]) <= GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, window[1]) &&
98  // engulfed by 1st
99  Math.Max(window[1].Close, window[1].Open) < Math.Max(window[2].Close, window[2].Open) &&
100  Math.Min(window[1].Close, window[1].Open) > Math.Min(window[2].Close, window[2].Open) &&
101  // 3rd: opposite to 1st
102  ((GetCandleColor(window[2]) == CandleColor.White && GetCandleColor(input) == CandleColor.Black && input.Close < window[2].Open) ||
103  // and closing out
104  (GetCandleColor(window[2]) == CandleColor.Black && GetCandleColor(input) == CandleColor.White && input.Close > window[2].Open)
105  )
106  )
107  value = -(int)GetCandleColor(window[2]);
108  else
109  value = 0m;
110 
111  // add the current range and subtract the first range: this is done after the pattern recognition
112  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
113 
114  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
115  GetCandleRange(CandleSettingType.BodyLong, window[2 + _bodyLongAveragePeriod]);
116 
117  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, window[1]) -
118  GetCandleRange(CandleSettingType.BodyShort, window[1 + _bodyShortAveragePeriod]);
119 
120  return value;
121  }
122 
123  /// <summary>
124  /// Resets this indicator to its initial state
125  /// </summary>
126  public override void Reset()
127  {
128  _bodyLongPeriodTotal = 0;
129  _bodyShortPeriodTotal = 0;
130  base.Reset();
131  }
132  }
133 }