Auf GitHub ansehen

EMA plus WPR v2 Strategy

Strategy combining Williams %R oscillator with EMA trend filter. Trades when WPR reaches extreme levels after a retracement. Includes optional WPR-based exits, trailing stops and bar-based exit.

Details

  • Long: WPR hits -100 after retracement and EMA trend is up.
  • Short: WPR hits 0 after retracement and EMA trend is down.
  • Indicators: Williams %R, EMA.
  • Stops: fixed stop loss and take profit, optional trailing stop.
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>
/// EMA trend + Williams %R entry strategy.
/// Buys in uptrend when WPR oversold, sells in downtrend when WPR overbought.
/// </summary>
public class EmaPlusWprV2Strategy : Strategy
{
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _wprLength;
	private readonly StrategyParam<DataType> _candleType;

	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }
	public int WprLength { get => _wprLength.Value; set => _wprLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public EmaPlusWprV2Strategy()
	{
		_emaLength = Param(nameof(EmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Length", "EMA period", "Indicators");

		_wprLength = Param(nameof(WprLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("WPR Length", "Williams %R period", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var wpr = new WilliamsR { Length = WprLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, wpr, ProcessCandle)
			.Start();
	}

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

		var close = candle.ClosePrice;

		// WPR range is -100 to 0
		// Buy on oversold
		if (wprVal < -80 && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Sell on overbought
		else if (wprVal > -20 && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}
	}
}