Auf GitHub ansehen

MFS 3 Bars Pattern Strategy

This strategy detects a three-bar bullish reversal sequence inside a downtrend. It looks for a large green "ignite" bar, a small red pullback, and a bullish confirmation bar closing above the pullback high. The trend filter requires long SMA > medium SMA > short SMA and the ignite close below the short SMA.

Once the pattern appears the strategy opens a long position, placing stop-loss at the ignite bar low and a take-profit at a configurable risk-reward multiple.

Details

  • Entry Criteria: Ignite bar, pullback bar, and confirmation bar in a downtrend.
  • Long/Short: Long only.
  • Exit Criteria: Stop-loss at ignite low or take-profit at risk-reward multiple.
  • Stops: Yes, stop and target orders.
  • Default Values:
    • CandleType = 15 minute
    • SmaShortLength = 20
    • SmaMedLength = 50
    • SmaLongLength = 200
    • IgniteMultiplier = 3
    • MaxPullbackSize = 0.33
    • MinConfirmationSize = 0.33
    • RiskReward = 2
  • Filters:
    • Category: Pattern
    • Direction: Long
    • Indicators: Candlestick, Moving Average
    • Stops: Yes
    • Complexity: Intermediate
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
using System;
using System.Linq;
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;

public class Mfs3BarsPatternStrategy : Strategy
{
	private readonly StrategyParam<int> _smaLength;
	private readonly StrategyParam<decimal> _riskReward;
	private readonly StrategyParam<int> _signalCooldownBars;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _sma;
	private ICandleMessage _prev1;
	private ICandleMessage _prev2;
	private decimal _stopPrice;
	private decimal _takePrice;
	private int _barsFromSignal;

	public int SmaLength { get => _smaLength.Value; set => _smaLength.Value = value; }
	public decimal RiskReward { get => _riskReward.Value; set => _riskReward.Value = value; }
	public int SignalCooldownBars { get => _signalCooldownBars.Value; set => _signalCooldownBars.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Mfs3BarsPatternStrategy()
	{
		_smaLength = Param(nameof(SmaLength), 30)
			.SetGreaterThanZero()
			.SetDisplay("SMA Length", "SMA period", "General");
		_riskReward = Param(nameof(RiskReward), 2.5m)
			.SetGreaterThanZero()
			.SetDisplay("Risk Reward", "Target reward to risk ratio", "General");
		_signalCooldownBars = Param(nameof(SignalCooldownBars), 50)
			.SetGreaterThanZero()
			.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candles timeframe", "General");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_sma = null;
		_prev1 = null;
		_prev2 = null;
		_stopPrice = 0m;
		_takePrice = 0m;
		_barsFromSignal = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_sma = new SimpleMovingAverage { Length = SmaLength };
		_prev1 = null;
		_prev2 = null;
		_stopPrice = 0;
		_takePrice = 0;
		_barsFromSignal = SignalCooldownBars;

		var dummyEma = new ExponentialMovingAverage { Length = 10 };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(_sma, dummyEma, ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal smaValue, decimal dummyValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_sma.IsFormed || _prev1 == null || _prev2 == null)
		{
			_prev2 = _prev1;
			_prev1 = candle;
			return;
		}

		// 3-bar pattern: big bullish bar, small pullback, bullish confirmation
		var bar1Bullish = _prev2.ClosePrice > _prev2.OpenPrice;
		var bar1Body = Math.Abs(_prev2.ClosePrice - _prev2.OpenPrice);
		var bar2Bearish = _prev1.ClosePrice < _prev1.OpenPrice;
		var bar2Body = Math.Abs(_prev1.ClosePrice - _prev1.OpenPrice);
		var bar3Bullish = candle.ClosePrice > candle.OpenPrice;
		var bodyPercent = candle.ClosePrice > 0m ? bar1Body / candle.ClosePrice * 100m : 0m;
		var strongBar1 = bodyPercent >= 0.05m;
		var confirmation = candle.ClosePrice > _prev1.HighPrice;

		var pattern = bar1Bullish && strongBar1 && bar2Bearish && bar2Body < bar1Body * 0.5m && bar3Bullish && confirmation;
		_barsFromSignal++;

		if (_barsFromSignal >= SignalCooldownBars && pattern && Position == 0)
		{
			BuyMarket();
			_stopPrice = _prev2.LowPrice;
			_takePrice = candle.ClosePrice + (candle.ClosePrice - _stopPrice) * RiskReward;
			_barsFromSignal = 0;
		}

		if (Position > 0)
		{
			if (candle.LowPrice <= _stopPrice || candle.HighPrice >= _takePrice)
				SellMarket();
		}

		_prev2 = _prev1;
		_prev1 = candle;
	}
}