Lean  $LEAN_TAG$
RiseFallThreeMethods.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 
17 using System;
19 
21 {
22  /// <summary>
23  /// Rising/Falling Three Methods candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - first candle: long white (black) candlestick
28  /// - then: group of falling(rising) small real body candlesticks(commonly black (white)) that hold within
29  /// the prior long candle's range: ideally they should be three but two or more than three are ok too
30  /// - final candle: long white(black) candle that opens above(below) the previous small candle's close
31  /// and closes above(below) the first long candle's close
32  /// The meaning of "short" and "long" is specified with SetCandleSettings; here only patterns with 3 small candles
33  /// are considered;
34  /// The returned value is positive(+1) or negative(-1)
35  /// </remarks>
37  {
38  private readonly int _bodyShortAveragePeriod;
39  private readonly int _bodyLongAveragePeriod;
40 
41  private decimal[] _bodyPeriodTotal = new decimal[5];
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="RiseFallThreeMethods"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public RiseFallThreeMethods(string name)
48  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod) + 4 + 1)
49  {
50  _bodyShortAveragePeriod = CandleSettings.Get(CandleSettingType.BodyShort).AveragePeriod;
51  _bodyLongAveragePeriod = CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod;
52  }
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="RiseFallThreeMethods"/> class.
56  /// </summary>
58  : this("RISEFALLTHREEMETHODS")
59  {
60  }
61 
62  /// <summary>
63  /// Gets a flag indicating when this indicator is ready and fully initialized
64  /// </summary>
65  public override bool IsReady
66  {
67  get { return Samples > Period; }
68  }
69 
70  /// <summary>
71  /// Computes the next value of this indicator from the given state
72  /// </summary>
73  /// <param name="window">The window of data held in this indicator</param>
74  /// <param name="input">The input given to the indicator</param>
75  /// <returns>A new value for this indicator</returns>
76  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
77  {
78  if (!IsReady)
79  {
80  if (Samples > Period - _bodyShortAveragePeriod)
81  {
82  _bodyPeriodTotal[3] += GetCandleRange(CandleSettingType.BodyShort, window[3]);
83  _bodyPeriodTotal[2] += GetCandleRange(CandleSettingType.BodyShort, window[2]);
84  _bodyPeriodTotal[1] += GetCandleRange(CandleSettingType.BodyShort, window[1]);
85  }
86 
87  if (Samples > Period - _bodyLongAveragePeriod)
88  {
89  _bodyPeriodTotal[4] += GetCandleRange(CandleSettingType.BodyLong, window[4]);
90  _bodyPeriodTotal[0] += GetCandleRange(CandleSettingType.BodyLong, input);
91  }
92 
93  return 0m;
94  }
95 
96  decimal value;
97  if (
98  // 1st long, then 3 small, 5th long
99  GetRealBody(window[4]) > GetCandleAverage(CandleSettingType.BodyLong, _bodyPeriodTotal[4], window[4]) &&
100  GetRealBody(window[3]) < GetCandleAverage(CandleSettingType.BodyShort, _bodyPeriodTotal[3], window[3]) &&
101  GetRealBody(window[2]) < GetCandleAverage(CandleSettingType.BodyShort, _bodyPeriodTotal[2], window[2]) &&
102  GetRealBody(window[1]) < GetCandleAverage(CandleSettingType.BodyShort, _bodyPeriodTotal[1], window[1]) &&
103  GetRealBody(input) > GetCandleAverage(CandleSettingType.BodyLong, _bodyPeriodTotal[0], input) &&
104  // white, 3 black, white || black, 3 white, black
105  (int)GetCandleColor(window[4]) == -(int)GetCandleColor(window[3]) &&
106  GetCandleColor(window[3]) == GetCandleColor(window[2]) &&
107  GetCandleColor(window[2]) == GetCandleColor(window[1]) &&
108  (int)GetCandleColor(window[1]) == -(int)GetCandleColor(input) &&
109  // 2nd to 4th hold within 1st: a part of the real body must be within 1st range
110  Math.Min(window[3].Open, window[3].Close) < window[4].High && Math.Max(window[3].Open, window[3].Close) > window[4].Low &&
111  Math.Min(window[2].Open, window[2].Close) < window[4].High && Math.Max(window[2].Open, window[2].Close) > window[4].Low &&
112  Math.Min(window[1].Open, window[1].Close) < window[4].High && Math.Max(window[1].Open, window[1].Close) > window[4].Low &&
113  // 2nd to 4th are falling (rising)
114  window[2].Close * (int)GetCandleColor(window[4]) < window[3].Close * (int)GetCandleColor(window[4]) &&
115  window[1].Close * (int)GetCandleColor(window[4]) < window[2].Close * (int)GetCandleColor(window[4]) &&
116  // 5th opens above (below) the prior close
117  input.Open * (int)GetCandleColor(window[4]) > window[1].Close * (int)GetCandleColor(window[4]) &&
118  // 5th closes above (below) the 1st close
119  input.Close * (int)GetCandleColor(window[4]) > window[4].Close * (int)GetCandleColor(window[4])
120  )
121  value = (int)GetCandleColor(window[4]);
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  _bodyPeriodTotal[4] += GetCandleRange(CandleSettingType.BodyLong, window[4]) -
129  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 4]);
130 
131  for (var i = 3; i >= 1; i--)
132  {
133  _bodyPeriodTotal[i] += GetCandleRange(CandleSettingType.BodyShort, window[i]) -
134  GetCandleRange(CandleSettingType.BodyShort, window[i + _bodyShortAveragePeriod]);
135  }
136 
137  _bodyPeriodTotal[0] += GetCandleRange(CandleSettingType.BodyLong, input) -
138  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod]);
139 
140  return value;
141  }
142 
143  /// <summary>
144  /// Resets this indicator to its initial state
145  /// </summary>
146  public override void Reset()
147  {
148  _bodyPeriodTotal = new decimal[5];
149  base.Reset();
150  }
151  }
152 }