在 GitHub 上查看

WPR Slowdown 策略

WPR Slowdown 策略利用 Williams %R 振荡器在极值附近动量减弱时寻找反转。当当前 Williams %R 与前一个值的差异小于 1 时认为出现“减速”。在上限以上出现减速时,策略会关闭空头仓位并在允许的情况下开多;在下限以下出现减速时,策略会关闭多头仓位并在允许的情况下开空。

入场与出场规则

  • 开多:Williams %R 高于 LevelMax 且满足减速条件,可选关闭空头。
  • 开空:Williams %R 低于 LevelMin 且满足减速条件,可选关闭多头。
  • 平多:在启用 BuyPosClose 时出现卖出信号。
  • 平空:在启用 SellPosClose 时出现买入信号。

参数

  • WprPeriod – Williams %R 的计算周期。
  • LevelMax – 上方信号阈值(默认 -20),表示超买区域。
  • LevelMin – 下方信号阈值(默认 -80),表示超卖区域。
  • SeekSlowdown – 是否检查 Williams %R 相邻值之间的减速。
  • BuyPosOpen – 允许开多。
  • SellPosOpen – 允许开空。
  • BuyPosClose – 允许在卖出信号时平多。
  • SellPosClose – 允许在买入信号时平空。
  • CandleType – 用于计算的 K 线类型(默认 6 小时 K 线)。

说明

该策略仅保留原始 MQL5 专家的 Williams %R 减速逻辑。提醒、资金管理等附加功能已被省略,若需要可手动添加止损与止盈。

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>
/// Williams %R slowdown strategy.
/// Opens or closes positions when momentum stalls near specified levels.
/// </summary>
public class WprSlowdownStrategy : Strategy
{
	private readonly StrategyParam<int> _wprPeriod;
	private readonly StrategyParam<decimal> _levelMax;
	private readonly StrategyParam<decimal> _levelMin;
	private readonly StrategyParam<bool> _seekSlowdown;
	private readonly StrategyParam<bool> _buyPosOpen;
	private readonly StrategyParam<bool> _sellPosOpen;
	private readonly StrategyParam<bool> _buyPosClose;
	private readonly StrategyParam<bool> _sellPosClose;
	private readonly StrategyParam<DataType> _candleType;

	private WilliamsR _wpr;
	private decimal? _prevWpr;

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

	/// <summary>
	/// Upper signal level (overbought threshold).
	/// </summary>
	public decimal LevelMax
	{
		get => _levelMax.Value;
		set => _levelMax.Value = value;
	}

	/// <summary>
	/// Lower signal level (oversold threshold).
	/// </summary>
	public decimal LevelMin
	{
		get => _levelMin.Value;
		set => _levelMin.Value = value;
	}

	/// <summary>
	/// Require slowdown between consecutive Williams %R values.
	/// </summary>
	public bool SeekSlowdown
	{
		get => _seekSlowdown.Value;
		set => _seekSlowdown.Value = value;
	}

	/// <summary>
	/// Allow opening long positions.
	/// </summary>
	public bool BuyPosOpen
	{
		get => _buyPosOpen.Value;
		set => _buyPosOpen.Value = value;
	}

	/// <summary>
	/// Allow opening short positions.
	/// </summary>
	public bool SellPosOpen
	{
		get => _sellPosOpen.Value;
		set => _sellPosOpen.Value = value;
	}

	/// <summary>
	/// Allow closing long positions on sell signals.
	/// </summary>
	public bool BuyPosClose
	{
		get => _buyPosClose.Value;
		set => _buyPosClose.Value = value;
	}

	/// <summary>
	/// Allow closing short positions on buy signals.
	/// </summary>
	public bool SellPosClose
	{
		get => _sellPosClose.Value;
		set => _sellPosClose.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="WprSlowdownStrategy"/> class.
	/// </summary>
	public WprSlowdownStrategy()
	{
		_wprPeriod = Param(nameof(WprPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("WPR Period", "Williams %R indicator period", "Indicator")
			
			.SetOptimize(6, 24, 1);

		_levelMax = Param(nameof(LevelMax), -20m)
			.SetDisplay("Level Max", "Upper signal level", "Indicator");

		_levelMin = Param(nameof(LevelMin), -80m)
			.SetDisplay("Level Min", "Lower signal level", "Indicator");

		_seekSlowdown = Param(nameof(SeekSlowdown), true)
			.SetDisplay("Seek Slowdown", "Require slowdown between values", "Indicator");

		_buyPosOpen = Param(nameof(BuyPosOpen), true)
			.SetDisplay("Open Long", "Allow opening long positions", "Trading");

		_sellPosOpen = Param(nameof(SellPosOpen), true)
			.SetDisplay("Open Short", "Allow opening short positions", "Trading");

		_buyPosClose = Param(nameof(BuyPosClose), true)
			.SetDisplay("Close Long", "Allow closing long positions", "Trading");

		_sellPosClose = Param(nameof(SellPosClose), true)
			.SetDisplay("Close Short", "Allow closing short positions", "Trading");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(6).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();
		_prevWpr = null;
	}

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

		_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 wpr)
	{
		if (candle.State != CandleStates.Finished)
		return;

		if (!_wpr.IsFormed)
		return;

		var slowdown = !_prevWpr.HasValue || Math.Abs(wpr - _prevWpr.Value) < 1m;

		var canBuy = wpr >= LevelMax && (!SeekSlowdown || slowdown);
		var canSell = wpr <= LevelMin && (!SeekSlowdown || slowdown);

		if (canBuy)
		{
			if (Position <= 0)
				BuyMarket();
		}
		else if (canSell)
		{
			if (Position >= 0)
				SellMarket();
		}

		_prevWpr = wpr;
	}
}