Lean  $LEAN_TAG$
UniqueThreeRiver.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  /// Unique Three River candlestick pattern
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - first candle: long black candle
27  /// - second candle: black harami candle with a lower low than the first candle's low
28  /// - third candle: small white candle with open not lower than the second candle's low, better if its open and
29  /// close are under the second candle's close
30  /// The meaning of "short" and "long" is specified with SetCandleSettings
31  /// The returned value is positive(+1): unique 3 river is always bullish and should appear in a downtrend
32  /// to be significant, 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="UniqueThreeRiver"/> class using the specified name.
44  /// </summary>
45  /// <param name="name">The name of this indicator</param>
46  public UniqueThreeRiver(string name)
47  : base(name, Math.Max(CandleSettings.Get(CandleSettingType.BodyLong).AveragePeriod, CandleSettings.Get(CandleSettingType.BodyShort).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="UniqueThreeRiver"/> class.
55  /// </summary>
57  : this("UNIQUETHREERIVER")
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)
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  // black
97  GetCandleColor(window[2]) == CandleColor.Black &&
98  // 2nd: black
99  GetCandleColor(window[1]) == CandleColor.Black &&
100  // harami
101  window[1].Close > window[2].Close && window[1].Open <= window[2].Open &&
102  // lower low
103  window[1].Low < window[2].Low &&
104  // 3rd: short
105  GetRealBody(input) < GetCandleAverage(CandleSettingType.BodyShort, _bodyShortPeriodTotal, input) &&
106  // white
107  GetCandleColor(input) == CandleColor.White &&
108  // open not lower
109  input.Open > window[1].Low
110  )
111  value = 1m;
112  else
113  value = 0m;
114 
115  // add the current range and subtract the first range: this is done after the pattern recognition
116  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
117 
118  _bodyLongPeriodTotal += GetCandleRange(CandleSettingType.BodyLong, window[2]) -
119  GetCandleRange(CandleSettingType.BodyLong, window[_bodyLongAveragePeriod + 2]);
120 
121  _bodyShortPeriodTotal += GetCandleRange(CandleSettingType.BodyShort, input) -
122  GetCandleRange(CandleSettingType.BodyShort, window[_bodyShortAveragePeriod]);
123 
124  return value;
125  }
126 
127  /// <summary>
128  /// Resets this indicator to its initial state
129  /// </summary>
130  public override void Reset()
131  {
132  _bodyLongPeriodTotal = 0;
133  _bodyShortPeriodTotal = 0;
134  base.Reset();
135  }
136  }
137 }