/* * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; using QuantConnect.Data.Market; namespace QuantConnect.Indicators.CandlestickPatterns { /// /// Abstract base class for a candlestick pattern indicator /// public abstract class CandlestickPattern : WindowIndicator { /// /// Creates a new with the specified name /// /// The name of this indicator /// The number of data points to hold in the window protected CandlestickPattern(string name, int period) : base(name, period) { } /// /// Returns the candle color of a candle /// /// The input candle protected static CandleColor GetCandleColor(IBaseDataBar tradeBar) { return tradeBar.Close >= tradeBar.Open ? CandleColor.White : CandleColor.Black; } /// /// Returns the distance between the close and the open of a candle /// /// The input candle protected static decimal GetRealBody(IBaseDataBar tradeBar) { return Math.Abs(tradeBar.Close - tradeBar.Open); } /// /// Returns the full range of the candle /// /// The input candle protected static decimal GetHighLowRange(IBaseDataBar tradeBar) { return tradeBar.High - tradeBar.Low; } /// /// Returns the range of a candle /// /// The type of setting to use /// The input candle protected static decimal GetCandleRange(CandleSettingType type, IBaseDataBar tradeBar) { switch (CandleSettings.Get(type).RangeType) { case CandleRangeType.RealBody: return GetRealBody(tradeBar); case CandleRangeType.HighLow: return GetHighLowRange(tradeBar); case CandleRangeType.Shadows: return GetUpperShadow(tradeBar) + GetLowerShadow(tradeBar); default: return 0m; } } /// /// Returns true if the candle is higher than the previous one /// protected static bool GetCandleGapUp(IBaseDataBar tradeBar, IBaseDataBar previousBar) { return tradeBar.Low > previousBar.High; } /// /// Returns true if the candle is lower than the previous one /// protected static bool GetCandleGapDown(IBaseDataBar tradeBar, IBaseDataBar previousBar) { return tradeBar.High < previousBar.Low; } /// /// Returns true if the candle is higher than the previous one (with no body overlap) /// protected static bool GetRealBodyGapUp(IBaseDataBar tradeBar, IBaseDataBar previousBar) { return Math.Min(tradeBar.Open, tradeBar.Close) > Math.Max(previousBar.Open, previousBar.Close); } /// /// Returns true if the candle is lower than the previous one (with no body overlap) /// protected static bool GetRealBodyGapDown(IBaseDataBar tradeBar, IBaseDataBar previousBar) { return Math.Max(tradeBar.Open, tradeBar.Close) < Math.Min(previousBar.Open, previousBar.Close); } /// /// Returns the range of the candle's lower shadow /// /// The input candle protected static decimal GetLowerShadow(IBaseDataBar tradeBar) { return (tradeBar.Close >= tradeBar.Open ? tradeBar.Open : tradeBar.Close) - tradeBar.Low; } /// /// Returns the range of the candle's upper shadow /// /// The input candle protected static decimal GetUpperShadow(IBaseDataBar tradeBar) { return tradeBar.High - (tradeBar.Close >= tradeBar.Open ? tradeBar.Close : tradeBar.Open); } /// /// Returns the average range of the previous candles /// /// The type of setting to use /// The sum of the previous candles ranges /// The input candle protected static decimal GetCandleAverage(CandleSettingType type, decimal sum, IBaseDataBar tradeBar) { var defaultSetting = CandleSettings.Get(type); return defaultSetting.Factor * (defaultSetting.AveragePeriod != 0 ? sum / defaultSetting.AveragePeriod : GetCandleRange(type, tradeBar)) / (defaultSetting.RangeType == CandleRangeType.Shadows ? 2.0m : 1.0m); } } }