View on GitHub

Delta WPR

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();
	}
}