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