在 GitHub 上查看

The MasterMind 策略

该策略利用随机震荡指标(Stochastic)和威廉指标(Williams %R)来捕捉极端的超买和超卖情况。

概述

策略监控两个动量指标:

  • Stochastic Oscillator 基础周期为100,平滑参数为3/3。
  • Williams %R 周期为100。

当 Stochastic 的 %D 值低于 3 且 Williams %R 低于 -99.9 时,认为市场超卖,开多头。 当 %D 高于 97 且 Williams %R 高于 -0.1 时,认为市场超买,开空头。

进场后通过止损、止盈、跟踪止损以及可选的保本位管理风险。

参数

  • StochasticLength – Stochastic 和 Williams %R 的计算周期。
  • StopLoss – 止损距离(点)。
  • TakeProfit – 止盈距离(点)。
  • TrailingStop – 启动跟踪止损的距离(点)。
  • TrailingStep – 跟踪止损的步长(点)。
  • BreakEven – 当盈利达到该值时将止损移至入场价(点)。
  • CandleType – 用于计算的K线周期。

指标

  • StochasticOscillator
  • WilliamsR

交易规则

  1. %D < 3Williams %R < -99.9 时买入。
  2. %D > 97Williams %R > -0.1 时卖出。
  3. 入场后应用止损和止盈。
  4. 当利润达到 BreakEven 时,将止损移至入场价。
  5. 当价格移动 TrailingStop 后,按 TrailingStep 跟踪止损。

备注

本策略使用 StockSharp 的高级 API,主要用于学习示例。

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 entering on extreme Stochastic and Williams %R values.
/// </summary>
public class TheMasterMindStrategy : Strategy
{
	private readonly StrategyParam<int> _stochLength;
	private readonly StrategyParam<DataType> _candleType;

	private WilliamsR _wpr;
	private decimal? _prevD;
	private int _lastSignal;

	/// <summary>
	/// Stochastic base length.
	/// </summary>
	public int StochasticLength
	{
		get => _stochLength.Value;
		set => _stochLength.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of <see cref="TheMasterMindStrategy"/>.
	/// </summary>
	public TheMasterMindStrategy()
	{
		_stochLength = Param(nameof(StochasticLength), 14)
			.SetDisplay("Stochastic Length", "Base length for Stochastic", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for calculations", "Common");
	}

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

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

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

		var stochastic = new StochasticOscillator
		{
			K = { Length = StochasticLength },
			D = { Length = 3 }
		};

		_wpr = new WilliamsR { Length = StochasticLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(stochastic, (candle, stochValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				// Process WilliamsR manually (candle-based)
				var wprResult = _wpr.Process(candle);
				if (!_wpr.IsFormed)
					return;

				var stoch = (IStochasticOscillatorValue)stochValue;
				if (stoch.D is not decimal d || stoch.K is not decimal k)
					return;

				var wpr = wprResult.ToDecimal();

				var buySignal = _prevD >= 20m && d < 20m && wpr < -85m;
				var sellSignal = _prevD <= 80m && d > 80m && wpr > -15m;

				if (buySignal && _lastSignal != 1 && Position <= 0)
				{
					BuyMarket();
					_lastSignal = 1;
				}
				else if (sellSignal && _lastSignal != -1 && Position >= 0)
				{
					SellMarket();
					_lastSignal = -1;
				}

				_prevD = d;
			})
			.Start();

		StartProtection(
			new Unit(2000m, UnitTypes.Absolute),
			new Unit(1000m, UnitTypes.Absolute));
	}
}