在 GitHub 上查看

Delta WPR

Delta WPR 策略比较快慢 Williams %R 振荡器的差值以捕捉动量变化。当快速值高于慢速值且慢速值在设定水平之上时,策略开多并平掉任何空头头寸。相反,当快速值低于慢速值且慢速值位于该水平之下时,策略开空并关闭多头。策略仅在每根K线收盘后进行计算,以减少噪音。

在四小时数据上的回测显示,该方法在 Williams %R 徘徊于超买和超卖区域的震荡行情中表现最佳。

细节

  • 入场条件
    • 多头:WPR 慢线 > Level && WPR 快线 > WPR 慢线
    • 空头:WPR 慢线 < Level && WPR 快线 < WPR 慢线
  • 多/空:双向。
  • 出场条件:相反信号。
  • 止损:无。
  • 默认值
    • FastPeriod = 14
    • SlowPeriod = 30
    • Level = -50m
    • CandleType = TimeSpan.FromHours(4)
  • 筛选
    • 类别: 振荡器
    • 方向: 双向
    • 指标: WilliamsR
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 4h
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中
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();
	}
}