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();
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage, WilliamsR
from StockSharp.Algo.Strategies import Strategy
class ema_plus_wpr_v2_strategy(Strategy):
def __init__(self):
super(ema_plus_wpr_v2_strategy, self).__init__()
self._ema_length = self.Param("EmaLength", 20) \
.SetDisplay("EMA Length", "EMA period", "Indicators")
self._wpr_length = self.Param("WprLength", 14) \
.SetDisplay("WPR Length", "Williams %R period", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
@property
def ema_length(self):
return self._ema_length.Value
@property
def wpr_length(self):
return self._wpr_length.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(ema_plus_wpr_v2_strategy, self).OnStarted2(time)
ema = ExponentialMovingAverage()
ema.Length = self.ema_length
wpr = WilliamsR()
wpr.Length = self.wpr_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, wpr, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, ema_val, wpr_val):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
# WPR range is -100 to 0
# Buy on oversold
if wpr_val < -80 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
# Sell on overbought
elif wpr_val > -20 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
def CreateClone(self):
return ema_plus_wpr_v2_strategy()