GitHub で見る

WPR減速戦略

WPR減速戦略は、モメンタムが極端なレベル付近で失速したときの反転を検出するために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 – インジケーター計算に使用するローソク足タイプ(デフォルト6時間足ローソク足)。

注意事項

この戦略はオリジナルの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;
	}
}