Ver en GitHub

Keltner Channel Strategy

This strategy trades Keltner Channel breakouts and EMA trend crossings.

Details

  • Entry Criteria:
    • Long: price crosses below the lower Keltner band or EMA9 crosses above EMA21 while price is above EMA50.
    • Short: price crosses above the upper Keltner band or EMA9 crosses below EMA21 while price is below EMA50.
  • Long/Short: Both sides.
  • Exit Criteria:
    • Price crosses the middle band in the opposite direction or the EMAs cross back.
    • Stop loss at 1.5 ATR.
    • Take profit at 3 ATR.
  • Stops: Yes.
  • Default Values:
    • Length = 20
    • Multiplier = 1.5
    • AtrMultiplier = 1.5
    • FastEmaPeriod = 9
    • SlowEmaPeriod = 21
    • TrendEmaPeriod = 50
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()
  • Filters:
    • Category: Channel
    • Direction: Both
    • Indicators: EMA, ATR, Keltner
    • Stops: Yes
    • Complexity: Basic
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
using System;
using System.Collections.Generic;

using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Keltner Channel breakout and EMA trend strategy.
/// </summary>
public class KeltnerChannelStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<decimal> _atrMultiplier;
	private readonly StrategyParam<int> _fastEmaPeriod;
	private readonly StrategyParam<int> _slowEmaPeriod;
	private readonly StrategyParam<int> _trendEmaPeriod;
	private readonly StrategyParam<int> _maxEntries;
	private readonly StrategyParam<int> _cooldownBars;
	private readonly StrategyParam<DataType> _candleType;
	
	private decimal _prevClose;
	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _stopPrice;
	private decimal _takePrice;
	private int _entriesExecuted;
	private int _barsSinceSignal;
	
	/// <summary>
	/// EMA period for Keltner Channels.
	/// </summary>
	public int Length
	{
		get => _length.Value;
		set => _length.Value = value;
	}
	
	/// <summary>
	/// ATR multiplier for channel width.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}
	
	/// <summary>
	/// ATR multiplier for stops.
	/// </summary>
	public decimal AtrMultiplier
	{
		get => _atrMultiplier.Value;
		set => _atrMultiplier.Value = value;
	}
	
	/// <summary>
	/// Fast EMA period.
	/// </summary>
	public int FastEmaPeriod
	{
		get => _fastEmaPeriod.Value;
		set => _fastEmaPeriod.Value = value;
	}
	
	/// <summary>
	/// Slow EMA period.
	/// </summary>
	public int SlowEmaPeriod
	{
		get => _slowEmaPeriod.Value;
		set => _slowEmaPeriod.Value = value;
	}
	
	/// <summary>
	/// Trend filter EMA period.
	/// </summary>
	public int TrendEmaPeriod
	{
		get => _trendEmaPeriod.Value;
		set => _trendEmaPeriod.Value = value;
	}

	/// <summary>
	/// Maximum entries per run.
	/// </summary>
	public int MaxEntries
	{
		get => _maxEntries.Value;
		set => _maxEntries.Value = value;
	}

	/// <summary>
	/// Minimum bars between orders.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}
	
	/// <summary>
	/// Candle type for strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}
	
	/// <summary>
	/// Initializes a new instance of <see cref="KeltnerChannelStrategy"/>.
	/// </summary>
	public KeltnerChannelStrategy()
	{
		_length = Param(nameof(Length), 20)
		.SetRange(5, 100)
		.SetDisplay("Length", "EMA period for Keltner channels", "Keltner");
		
		_multiplier = Param(nameof(Multiplier), 1.5m)
		.SetRange(0.5m, 5m)
		.SetDisplay("Multiplier", "ATR multiplier for channel", "Keltner");
		
		_atrMultiplier = Param(nameof(AtrMultiplier), 1.5m)
		.SetRange(0.5m, 5m)
		.SetDisplay("ATR Multiplier", "ATR multiplier for stops", "Risk Management");
		
		_fastEmaPeriod = Param(nameof(FastEmaPeriod), 9)
		.SetRange(2, 50)
		.SetDisplay("Fast EMA", "Fast EMA period", "Trend");
		
		_slowEmaPeriod = Param(nameof(SlowEmaPeriod), 21)
		.SetRange(5, 100)
		.SetDisplay("Slow EMA", "Slow EMA period", "Trend");
		
		_trendEmaPeriod = Param(nameof(TrendEmaPeriod), 50)
		.SetRange(10, 200)
		.SetDisplay("Trend EMA", "Trend filter EMA period", "Trend");

		_maxEntries = Param(nameof(MaxEntries), 45)
		.SetRange(1, 200)
		.SetDisplay("Max Entries", "Maximum entries per run", "Risk");

		_cooldownBars = Param(nameof(CooldownBars), 1000)
		.SetRange(1, 200000)
		.SetDisplay("Cooldown Bars", "Minimum bars between orders", "Risk");
		
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
		.SetDisplay("Candle Type", "Type of candles", "General");
	}
	
	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}
	
	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		
		_prevClose = 0m;
		_prevFast = 0m;
		_prevSlow = 0m;
		_stopPrice = 0m;
		_takePrice = 0m;
		_entriesExecuted = 0;
		_barsSinceSignal = 0;
	}
	
	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_entriesExecuted = 0;
		_barsSinceSignal = CooldownBars;
		
		var kc = new KeltnerChannels
		{
			Length = Length,
			Multiplier = Multiplier
		};
		
		var emaFast = new ExponentialMovingAverage { Length = FastEmaPeriod };
		var emaSlow = new ExponentialMovingAverage { Length = SlowEmaPeriod };
		var emaTrend = new ExponentialMovingAverage { Length = TrendEmaPeriod };
		var atr = new AverageTrueRange { Length = Length };

		var sub = SubscribeCandles(CandleType);
		sub.BindEx(kc, emaFast, emaSlow, emaTrend, atr, ProcessCandle).Start();
		
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, sub);
			DrawIndicator(area, kc);
			DrawOwnTrades(area);
		}
	}
	
	private void ProcessCandle(ICandleMessage candle, IIndicatorValue kcValue, IIndicatorValue emaFastValue, IIndicatorValue emaSlowValue, IIndicatorValue emaTrendValue, IIndicatorValue atrValue)
	{
		if (candle.State != CandleStates.Finished)
		return;

		_barsSinceSignal++;

		var kcTyped = (KeltnerChannelsValue)kcValue;
		if (kcTyped.Middle is not decimal middle || kcTyped.Upper is not decimal upper || kcTyped.Lower is not decimal lower)
			return;
		var emaFast = emaFastValue.ToDecimal();
		var emaSlow = emaSlowValue.ToDecimal();
		var emaTrend = emaTrendValue.ToDecimal();
		var atr = atrValue.ToDecimal();

		var price = candle.ClosePrice;

		var crossUnderLower = _prevClose >= lower && price < lower;
		var crossOverUpper = _prevClose <= upper && price > upper;
		
		var crossOverEma = _prevFast <= _prevSlow && emaFast > emaSlow;
		var crossUnderEma = _prevFast >= _prevSlow && emaFast < emaSlow;
		
		var longEntryKC = crossUnderLower;
		var shortEntryKC = crossOverUpper;
		
		var longEntryTrend = crossOverEma && price > emaTrend;
		var shortEntryTrend = crossUnderEma && price < emaTrend;
		
		var exitLongKC = _prevClose <= middle && price > middle;
		var exitShortKC = _prevClose >= middle && price < middle;
		var exitLongTrend = crossUnderEma;
		var exitShortTrend = crossOverEma;
		
		var atrDistance = atr * AtrMultiplier;

		if (_barsSinceSignal < CooldownBars)
		{
			_prevClose = price;
			_prevFast = emaFast;
			_prevSlow = emaSlow;
			return;
		}
		
		if (Position <= 0 && _entriesExecuted < MaxEntries && (longEntryKC || longEntryTrend))
		{
			var volume = Volume + Math.Abs(Position);
			BuyMarket(volume);
			_stopPrice = price - atrDistance;
			_takePrice = price + 2m * atrDistance;
			_entriesExecuted++;
			_barsSinceSignal = 0;
		}
		else if (Position >= 0 && _entriesExecuted < MaxEntries && (shortEntryKC || shortEntryTrend))
		{
			var volume = Volume + Math.Abs(Position);
			SellMarket(volume);
			_stopPrice = price + atrDistance;
			_takePrice = price - 2m * atrDistance;
			_entriesExecuted++;
			_barsSinceSignal = 0;
		}
		
		if (Position > 0)
		{
			if (exitLongKC || exitLongTrend || price <= _stopPrice || price >= _takePrice)
			{
				SellMarket(Math.Abs(Position));
				_barsSinceSignal = 0;
			}
		}
		else if (Position < 0)
		{
			if (exitShortKC || exitShortTrend || price >= _stopPrice || price <= _takePrice)
			{
				BuyMarket(Math.Abs(Position));
				_barsSinceSignal = 0;
			}
		}
		
		_prevClose = price;
		_prevFast = emaFast;
		_prevSlow = emaSlow;
	}
}