Delta WPR compares a fast and a slow Williams %R oscillator to capture momentum shifts. When the fast value exceeds the slow one and the slow oscillator stays above a threshold level, the strategy opens a long position and closes any short exposure. The opposite configuration – fast below slow with the slow oscillator below the level – triggers a short entry. Every new candle is processed only after completion to avoid noise.
Backtests on 4‑hour data show that the approach performs best in ranging markets where Williams %R oscillates between overbought and oversold zones.
Details
Entry Criteria:
Long: WPR slow > Level && WPR fast > WPR slow
Short: WPR slow < Level && WPR fast < WPR slow
Long/Short: Both directions.
Exit Criteria: Opposite signal.
Stops: No.
Default Values:
FastPeriod = 14
SlowPeriod = 30
Level = -50m
CandleType = TimeSpan.FromHours(4)
Filters:
Category: Oscillator
Direction: Both
Indicators: WilliamsR
Stops: No
Complexity: Basic
Timeframe: 4h
Seasonality: No
Neural Networks: No
Divergence: No
Risk Level: Medium
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on difference between fast and slow Williams %R.
/// Enter long when fast WPR is above slow WPR while slow WPR is above the level.
/// Enter short when fast WPR is below slow WPR while slow WPR is below the level.
/// </summary>
public class DeltaWprStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<decimal> _level;
private readonly StrategyParam<DataType> _candleType;
private int _prevSignal;
/// <summary>
/// Fast Williams %R period.
/// </summary>
public int FastPeriod
{
get => _fastPeriod.Value;
set => _fastPeriod.Value = value;
}
/// <summary>
/// Slow Williams %R period.
/// </summary>
public int SlowPeriod
{
get => _slowPeriod.Value;
set => _slowPeriod.Value = value;
}
/// <summary>
/// Threshold level for the slow Williams %R.
/// </summary>
public decimal Level
{
get => _level.Value;
set => _level.Value = value;
}
/// <summary>
/// Candle type used for analysis.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initialize <see cref="DeltaWprStrategy"/>.
/// </summary>
public DeltaWprStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 14)
.SetDisplay("Fast WPR Period", "Period for the fast Williams %R", "Indicators")
.SetOptimize(7, 21, 7);
_slowPeriod = Param(nameof(SlowPeriod), 30)
.SetDisplay("Slow WPR Period", "Period for the slow Williams %R", "Indicators")
.SetOptimize(20, 40, 5);
_level = Param(nameof(Level), -50m)
.SetDisplay("Signal Level", "Threshold level for signals", "Indicators")
.SetOptimize(-80m, -20m, 10m);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevSignal = 1; // Pass
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new WilliamsR { Length = FastPeriod };
var slow = new WilliamsR { Length = SlowPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fast, slow, (candle, fastValue, slowValue) =>
{
// Process only finished candles
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var signal = 1;
if (slowValue > Level && fastValue > slowValue)
signal = 0; // Up
else if (slowValue < Level && fastValue < slowValue)
signal = 2; // Down
if (signal == _prevSignal)
return;
if (signal == 0 && Position <= 0)
{
BuyMarket();
}
else if (signal == 2 && Position >= 0)
{
SellMarket();
}
_prevSignal = signal;
})
.Start();
}
}
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 WilliamsR
from StockSharp.Algo.Strategies import Strategy
class delta_wpr_strategy(Strategy):
def __init__(self):
super(delta_wpr_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast WPR Period", "Period for the fast Williams %R", "Indicators")
self._slow_period = self.Param("SlowPeriod", 30) \
.SetDisplay("Slow WPR Period", "Period for the slow Williams %R", "Indicators")
self._level = self.Param("Level", -50.0) \
.SetDisplay("Signal Level", "Threshold level for signals", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_signal = 1
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def level(self):
return self._level.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(delta_wpr_strategy, self).OnReseted()
self._prev_signal = 1
def OnStarted2(self, time):
super(delta_wpr_strategy, self).OnStarted2(time)
self._prev_signal = 1
fast = WilliamsR()
fast.Length = int(self.fast_period)
slow = WilliamsR()
slow.Length = int(self.slow_period)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast, slow, self.process_candle).Start()
def process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_value = float(fast_value)
slow_value = float(slow_value)
lvl = float(self.level)
signal = 1
if slow_value > lvl and fast_value > slow_value:
signal = 0
elif slow_value < lvl and fast_value < slow_value:
signal = 2
if signal == self._prev_signal:
return
if signal == 0 and self.Position <= 0:
self.BuyMarket()
elif signal == 2 and self.Position >= 0:
self.SellMarket()
self._prev_signal = signal
def CreateClone(self):
return delta_wpr_strategy()