Lean  $LEAN_TAG$
Tristar.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  /// Tristar candlestick pattern indicator
23  /// </summary>
24  /// <remarks>
25  /// Must have:
26  /// - 3 consecutive doji days
27  /// - the second doji is a star
28  /// The meaning of "doji" is specified with SetCandleSettings
29  /// The returned value is positive(+1) when bullish or negative(-1) when bearish
30  /// </remarks>
31  public class Tristar : CandlestickPattern
32  {
33  private readonly int _bodyDojiAveragePeriod;
34 
35  private decimal _bodyDojiPeriodTotal;
36 
37  /// <summary>
38  /// Initializes a new instance of the <see cref="Tristar"/> class using the specified name.
39  /// </summary>
40  /// <param name="name">The name of this indicator</param>
41  public Tristar(string name)
42  : base(name, CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod + 2 + 1)
43  {
44  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
45  }
46 
47  /// <summary>
48  /// Initializes a new instance of the <see cref="Tristar"/> class.
49  /// </summary>
50  public Tristar()
51  : this("TRISTAR")
52  {
53  }
54 
55  /// <summary>
56  /// Gets a flag indicating when this indicator is ready and fully initialized
57  /// </summary>
58  public override bool IsReady
59  {
60  get { return Samples >= Period; }
61  }
62 
63  /// <summary>
64  /// Computes the next value of this indicator from the given state
65  /// </summary>
66  /// <param name="window">The window of data held in this indicator</param>
67  /// <param name="input">The input given to the indicator</param>
68  /// <returns>A new value for this indicator</returns>
69  protected override decimal ComputeNextValue(IReadOnlyWindow<IBaseDataBar> window, IBaseDataBar input)
70  {
71  if (!IsReady)
72  {
73  if (Samples >= Period - _bodyDojiAveragePeriod - 2 && Samples < Period - 2)
74  {
75  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input);
76  }
77 
78  return 0m;
79  }
80 
81  decimal value;
82  if (
83  // 1st: doji
84  GetRealBody(window[2]) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, window[2]) &&
85  // 2nd: doji
86  GetRealBody(window[1]) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, window[2]) &&
87  // 3rd: doji
88  GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, window[2]))
89  {
90  value = 0;
91  if (
92  // 2nd gaps up
93  GetRealBodyGapUp(window[1], window[2]) &&
94  // 3rd is not higher than 2nd
95  Math.Max(input.Open, input.Close) < Math.Max(window[1].Open, window[1].Close)
96  )
97  value = -1m;
98  if (
99  // 2nd gaps down
100  GetRealBodyGapDown(window[1], window[2]) &&
101  // 3rd is not lower than 2nd
102  Math.Min(input.Open, input.Close) > Math.Min(window[1].Open, window[1].Close)
103  )
104  value = 1m;
105  }
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, window[2]) -
113  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod + 2]);
114 
115  return value;
116  }
117 
118  /// <summary>
119  /// Resets this indicator to its initial state
120  /// </summary>
121  public override void Reset()
122  {
123  _bodyDojiPeriodTotal = 0m;
124  base.Reset();
125  }
126  }
127 }