Lean  $LEAN_TAG$
RickshawMan.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  /// Rickshaw Man candlestick pattern
24  /// </summary>
25  /// <remarks>
26  /// Must have:
27  /// - doji body
28  /// - two long shadows
29  /// - body near the midpoint of the high-low range
30  /// The meaning of "doji" and "near" is specified with SetCandleSettings
31  /// The returned value is always positive(+1) but this does not mean it is bullish: rickshaw man shows uncertainty
32  /// </remarks>
34  {
35  private readonly int _bodyDojiAveragePeriod;
36  private readonly int _shadowLongAveragePeriod;
37  private readonly int _nearAveragePeriod;
38 
39  private decimal _bodyDojiPeriodTotal;
40  private decimal _shadowLongPeriodTotal;
41  private decimal _nearPeriodTotal;
42 
43  /// <summary>
44  /// Initializes a new instance of the <see cref="RickshawMan"/> class using the specified name.
45  /// </summary>
46  /// <param name="name">The name of this indicator</param>
47  public RickshawMan(string name)
48  : base(name, Math.Max(Math.Max(CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod, CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod),
49  CandleSettings.Get(CandleSettingType.Near).AveragePeriod) + 1)
50  {
51  _bodyDojiAveragePeriod = CandleSettings.Get(CandleSettingType.BodyDoji).AveragePeriod;
52  _shadowLongAveragePeriod = CandleSettings.Get(CandleSettingType.ShadowLong).AveragePeriod;
53  _nearAveragePeriod = CandleSettings.Get(CandleSettingType.Near).AveragePeriod;
54  }
55 
56  /// <summary>
57  /// Initializes a new instance of the <see cref="RickshawMan"/> class.
58  /// </summary>
59  public RickshawMan()
60  : this("RICKSHAWMAN")
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 - _shadowLongAveragePeriod)
88  {
89  _shadowLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowLong, input);
90  }
91 
92  if (Samples >= Period - _nearAveragePeriod)
93  {
94  _nearPeriodTotal += GetCandleRange(CandleSettingType.Near, input);
95  }
96 
97  return 0m;
98  }
99 
100  decimal value;
101  if (
102  // doji
103  GetRealBody(input) <= GetCandleAverage(CandleSettingType.BodyDoji, _bodyDojiPeriodTotal, input) &&
104  // long shadow
105  GetLowerShadow(input) > GetCandleAverage(CandleSettingType.ShadowLong, _shadowLongPeriodTotal, input) &&
106  // long shadow
107  GetUpperShadow(input) > GetCandleAverage(CandleSettingType.ShadowLong, _shadowLongPeriodTotal, input) &&
108  // body near midpoint
109  (
110  Math.Min(input.Open, input.Close)
111  <= input.Low + GetHighLowRange(input) / 2 + GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal, input)
112  &&
113  Math.Max(input.Open, input.Close)
114  >= input.Low + GetHighLowRange(input) / 2 - GetCandleAverage(CandleSettingType.Near, _nearPeriodTotal, input)
115  )
116  )
117  value = 1m;
118  else
119  value = 0m;
120 
121  // add the current range and subtract the first range: this is done after the pattern recognition
122  // when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
123 
124  _bodyDojiPeriodTotal += GetCandleRange(CandleSettingType.BodyDoji, input) -
125  GetCandleRange(CandleSettingType.BodyDoji, window[_bodyDojiAveragePeriod]);
126 
127  _shadowLongPeriodTotal += GetCandleRange(CandleSettingType.ShadowLong, input) -
128  GetCandleRange(CandleSettingType.ShadowLong, window[_shadowLongAveragePeriod]);
129 
130  _nearPeriodTotal += GetCandleRange(CandleSettingType.Near, input) -
131  GetCandleRange(CandleSettingType.Near, window[_nearAveragePeriod]);
132 
133  return value;
134  }
135 
136  /// <summary>
137  /// Resets this indicator to its initial state
138  /// </summary>
139  public override void Reset()
140  {
141  _bodyDojiPeriodTotal = 0;
142  _shadowLongPeriodTotal = 0;
143  _nearPeriodTotal = 0;
144  base.Reset();
145  }
146  }
147 }