Lean  $LEAN_TAG$
Takuri.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  /// Takuri (Dragonfly Doji with very long lower shadow) candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - doji body
27  /// - open and close at the high of the day = no or very short upper shadow
28  /// - very long lower shadow
29  /// The meaning of "doji", "very short" and "very long" is specified with SetCandleSettings
30  /// The returned value is always positive(+1) but this does not mean it is bullish: takuri must be considered
31  /// relatively to the trend
32  /// </remarks>
33  public class Takuri : CandlestickPattern
34  {
35  private readonly int _bodyDojiAveragePeriod;
36  private readonly int _shadowVeryShortAveragePeriod;
37  private readonly int _shadowVeryLongAveragePeriod;
38 
39  private decimal _bodyDojiPeriodTotal;
40  private decimal _shadowVeryShortPeriodTotal;
41  private decimal _shadowVeryLongPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="Takuri"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public Takuri(string name)
48  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod),
49  CandleSettings.Get(CandleSettingType.ShadowVeryLong).AveragePeriod) + 1)
50  {
51  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
52  _shadowVeryShortAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryShort).AveragePeriod;
53  _shadowVeryLongAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowVeryLong).AveragePeriod;
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the <see cref="Takuri"/> class.
58  /// </summary>
59  public Takuri()
60  : this("TAKURI")
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 - _bodyDojiAveragePeriod)
83  {
84  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
85  }
86 
87  if (Samples >= Period - _shadowVeryShortAveragePeriod)
88  {
89  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input);
90  }
91 
92  if (Samples >= Period - _shadowVeryLongAveragePeriod)
93  {
94  _shadowVeryLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryLong, input);
95  }
96 
97  return 0m;
98  }
99 
100  decimal value;
101  if (GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) &&
102  GetUpperShadow(input) < GetCandleAverage(CandleSettingType.ShadowVeryShort, _shadowVeryShortPeriodTotal, input) &&
103  GetLowerShadow(input) > GetCandleAverage(CandleSettingType.ShadowVeryLong, _shadowVeryLongPeriodTotal, input)
104  )
105  value = 1m;
106  else
107  value = 0m;
108 
109  // add the current range and subtract the first range: this is done after the pattern recognition
110  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
111 
112  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input) -
113  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod]);
114 
115  _shadowVeryShortPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryShort, input) -
116  GetCandleRange(CandleSettingType.ShadowVeryShort, window[_shadowVeryShortAveragePeriod]);
117 
118  _shadowVeryLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowVeryLong, input) -
119  GetCandleRange(CandleSettingType.ShadowVeryLong, window[_shadowVeryLongAveragePeriod]);
120 
121  return value;
122  }
123 
124  /// <summary>
125  /// Resets this indicator to its initial state
126  /// </summary>
127  public override void Reset()
128  {
129  _bodyDojiPeriodTotal = 0m;
130  _shadowVeryShortPeriodTotal = 0m;
131  _shadowVeryLongPeriodTotal = 0m;
132  base.Reset();
133  }
134  }
135 }