Ver no GitHub

WPR Histogram Strategy

This strategy trades based on the behavior of the Williams %R indicator. It monitors when the indicator leaves overbought or oversold zones and enters positions in the opposite direction.

Logic

  • When Williams %R rises above the high level and then drops back, it is considered a signal that the market leaves the overbought zone. The strategy opens a long position.
  • When Williams %R falls below the low level and then rises back, the market leaves the oversold zone. The strategy opens a short position.
  • Existing opposite positions are closed before opening a new one.

Parameters

  • WPR Period – period of Williams %R calculation.
  • High Level – threshold for the overbought zone.
  • Low Level – threshold for the oversold zone.
  • Candle Type – type and timeframe of candles used for calculations.

Notes

The strategy uses only market orders and does not set stop-loss or take-profit levels. It relies on the user-defined Volume property for position sizing.

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 Williams %R histogram indicator.
/// Opens a long position when price leaves overbought zone.
/// Opens a short position when price leaves oversold zone.
/// </summary>
public class WprHistogramStrategy : Strategy
{
	private readonly StrategyParam<int> _wprPeriod;
	private readonly StrategyParam<decimal> _highLevel;
	private readonly StrategyParam<decimal> _lowLevel;
	private readonly StrategyParam<DataType> _candleType;

	// Previous zone state: 0 - overbought, 1 - neutral, 2 - oversold
	private int? _previousZone;

	/// <summary>
	/// Williams %R period.
	/// </summary>
	public int WprPeriod
	{
		get => _wprPeriod.Value;
		set => _wprPeriod.Value = value;
	}

	/// <summary>
	/// High level threshold.
	/// </summary>
	public decimal HighLevel
	{
		get => _highLevel.Value;
		set => _highLevel.Value = value;
	}

	/// <summary>
	/// Low level threshold.
	/// </summary>
	public decimal LowLevel
	{
		get => _lowLevel.Value;
		set => _lowLevel.Value = value;
	}

	/// <summary>
	/// Candle type used for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes strategy parameters.
	/// </summary>
	public WprHistogramStrategy()
	{
		_wprPeriod = Param(nameof(WprPeriod), 14)
		.SetGreaterThanZero()
		.SetDisplay("WPR Period", "Period for Williams %R", "Indicator")
		
		.SetOptimize(7, 28, 7);

		_highLevel = Param(nameof(HighLevel), -30m)
		.SetDisplay("High Level", "Overbought threshold", "Indicator")
		
		.SetOptimize(-40m, -20m, 10m);

		_lowLevel = Param(nameof(LowLevel), -70m)
		.SetDisplay("Low Level", "Oversold threshold", "Indicator")
		
		.SetOptimize(-80m, -60m, 10m);

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

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_previousZone = null;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var wpr = new WilliamsR { Length = WprPeriod };

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

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, wpr);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal wprValue)
	{
		// Only finished candles are processed
		if (candle.State != CandleStates.Finished)
		return;

		var currentZone = 1;
		if (wprValue > HighLevel)
		currentZone = 0;
		else if (wprValue < LowLevel)
		currentZone = 2;

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_previousZone = currentZone;
			return;
		}

		if (_previousZone == null)
		{
			_previousZone = currentZone;
			return;
		}

		// Leaving overbought zone - open long
		if (_previousZone == 0 && currentZone != 0 && Position <= 0)
		{
			BuyMarket();
		}
		// Leaving oversold zone - open short
		else if (_previousZone == 2 && currentZone != 2 && Position >= 0)
		{
			SellMarket();
		}

		_previousZone = currentZone;
	}
}