Ver no GitHub

Estratégia EMA mais WPR v2

Estratégia que combina o oscilador Williams %R com o filtro de tendência EMA. Opera quando o WPR atinge níveis extremos após uma retração. Inclui saídas opcionais baseadas em WPR, trailing stops e saída baseada em barras.

Detalhes

  • Comprado: WPR atinge -100 após retração e a tendência EMA é de alta.
  • Vendido: WPR atinge 0 após retração e a tendência EMA é de baixa.
  • Indicadores: Williams %R, EMA.
  • Stops: stop-loss e take-profit fixos, trailing stop opcional.
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();
		}
	}
}