Lean  $LEAN_TAG$
CandlestickPattern.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  /// Abstract base class for a candlestick pattern indicator
23  /// </summary>
24  public abstract class CandlestickPattern : WindowIndicator<IBaseDataBar>
25  {
26  /// <summary>
27  /// Creates a new <see cref="CandlestickPattern"/> with the specified name
28  /// </summary>
29  /// <param name="name">The name of this indicator</param>
30  /// <param name="period">The number of data points to hold in the window</param>
31  protected CandlestickPattern(string name, int period)
32  : base(name, period)
33  {
34  }
35 
36  /// <summary>
37  /// Returns the candle color of a candle
38  /// </summary>
39  /// <param name="tradeBar">The input candle</param>
40  protected static CandleColor GetCandleColor(IBaseDataBar tradeBar)
41  {
42  return tradeBar.Close >= tradeBar.Open ? CandleColor.White : CandleColor.Black;
43  }
44 
45  /// <summary>
46  /// Returns the distance between the close and the open of a candle
47  /// </summary>
48  /// <param name="tradeBar">The input candle</param>
49  protected static decimal GetRealBody(IBaseDataBar tradeBar)
50  {
51  return Math.Abs(tradeBar.Close - tradeBar.Open);
52  }
53 
54  /// <summary>
55  /// Returns the full range of the candle
56  /// </summary>
57  /// <param name="tradeBar">The input candle</param>
58  protected static decimal GetHighLowRange(IBaseDataBar tradeBar)
59  {
60  return tradeBar.High - tradeBar.Low;
61  }
62 
63  /// <summary>
64  /// Returns the range of a candle
65  /// </summary>
66  /// <param name="type">The type of setting to use</param>
67  /// <param name="tradeBar">The input candle</param>
68  protected static decimal GetCandleRange(CandleSettingType type, IBaseDataBar tradeBar)
69  {
70  switch (CandleSettings.Get(type).RangeType)
71  {
72  case CandleRangeType.RealBody:
73  return GetRealBody(tradeBar);
74 
75  case CandleRangeType.HighLow:
76  return GetHighLowRange(tradeBar);
77 
78  case CandleRangeType.Shadows:
79  return GetUpperShadow(tradeBar) + GetLowerShadow(tradeBar);
80 
81  default:
82  return 0m;
83  }
84  }
85 
86  /// <summary>
87  /// Returns true if the candle is higher than the previous one
88  /// </summary>
89  protected static bool GetCandleGapUp(IBaseDataBar tradeBar, IBaseDataBar previousBar)
90  {
91  return tradeBar.Low > previousBar.High;
92  }
93 
94  /// <summary>
95  /// Returns true if the candle is lower than the previous one
96  /// </summary>
97  protected static bool GetCandleGapDown(IBaseDataBar tradeBar, IBaseDataBar previousBar)
98  {
99  return tradeBar.High < previousBar.Low;
100  }
101 
102  /// <summary>
103  /// Returns true if the candle is higher than the previous one (with no body overlap)
104  /// </summary>
105  protected static bool GetRealBodyGapUp(IBaseDataBar tradeBar, IBaseDataBar previousBar)
106  {
107  return Math.Min(tradeBar.Open, tradeBar.Close) > Math.Max(previousBar.Open, previousBar.Close);
108  }
109 
110  /// <summary>
111  /// Returns true if the candle is lower than the previous one (with no body overlap)
112  /// </summary>
113  protected static bool GetRealBodyGapDown(IBaseDataBar tradeBar, IBaseDataBar previousBar)
114  {
115  return Math.Max(tradeBar.Open, tradeBar.Close) < Math.Min(previousBar.Open, previousBar.Close);
116  }
117 
118  /// <summary>
119  /// Returns the range of the candle's lower shadow
120  /// </summary>
121  /// <param name="tradeBar">The input candle</param>
122  protected static decimal GetLowerShadow(IBaseDataBar tradeBar)
123  {
124  return (tradeBar.Close >= tradeBar.Open ? tradeBar.Open : tradeBar.Close) - tradeBar.Low;
125  }
126 
127  /// <summary>
128  /// Returns the range of the candle's upper shadow
129  /// </summary>
130  /// <param name="tradeBar">The input candle</param>
131  protected static decimal GetUpperShadow(IBaseDataBar tradeBar)
132  {
133  return tradeBar.High - (tradeBar.Close >= tradeBar.Open ? tradeBar.Close : tradeBar.Open);
134  }
135 
136  /// <summary>
137  /// Returns the average range of the previous candles
138  /// </summary>
139  /// <param name="type">The type of setting to use</param>
140  /// <param name="sum">The sum of the previous candles ranges</param>
141  /// <param name="tradeBar">The input candle</param>
142  protected static decimal GetCandleAverage(CandleSettingType type, decimal sum, IBaseDataBar tradeBar)
143  {
144  var defaultSetting = CandleSettings.Get(type);
145 
146  return defaultSetting.Factor *
147  (defaultSetting.AveragePeriod != 0 ? sum / defaultSetting.AveragePeriod : GetCandleRange(type, tradeBar)) /
148  (defaultSetting.RangeType == CandleRangeType.Shadows ? 2.0m : 1.0m);
149  }
150  }
151 }