在 GitHub 上查看

Z-Score 买卖策略

该策略利用Z-score检测价格相对移动平均线的极端偏离。 当Z-score超过阈值时开仓,并通过冷却期避免重复信号。

细节

  • 入场条件
    • 当 z-score 大于 ZThreshold 且卖出冷却结束时做空。
    • 当 z-score 小于 -ZThreshold 且买入冷却结束时做多。
  • 多空方向:双向。
  • 出场条件
    • 反向信号。
  • 止损:无。
  • 默认值
    • RollingWindow = 80
    • ZThreshold = 2.8
    • CoolDown = 5
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器
    • 类别:均值回归
    • 方向:双向
    • 指标:SMA、StandardDeviation、Z-Score
    • 止损:无
    • 复杂度:基础
    • 时间框架:任意
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// Z-Score mean reversion strategy with cooldown between signals.
/// </summary>
public class ZScoreBuySellStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _rollingWindow;
	private readonly StrategyParam<decimal> _zThreshold;
	private readonly StrategyParam<int> _coolDown;

	private int _buyCooldownCounter;
	private int _sellCooldownCounter;

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

	/// <summary>
	/// Period for moving average and standard deviation.
	/// </summary>
	public int RollingWindow
	{
		get => _rollingWindow.Value;
		set => _rollingWindow.Value = value;
	}

	/// <summary>
	/// Absolute z-score level to trigger trades.
	/// </summary>
	public decimal ZThreshold
	{
		get => _zThreshold.Value;
		set => _zThreshold.Value = value;
	}

	/// <summary>
	/// Bars to wait after trade.
	/// </summary>
	public int CoolDown
	{
		get => _coolDown.Value;
		set => _coolDown.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="ZScoreBuySellStrategy"/>.
	/// </summary>
	public ZScoreBuySellStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");

		_rollingWindow = Param(nameof(RollingWindow), 300)
			.SetGreaterThanZero()
			.SetDisplay("Rolling Window", "Lookback period", "Parameters")
			
			.SetOptimize(20, 150, 10);

		_zThreshold = Param(nameof(ZThreshold), 2.8m)
			.SetGreaterThanZero()
			.SetDisplay("Z Threshold", "Z-score trigger level", "Parameters")
			
			.SetOptimize(1m, 5m, 0.5m);

		_coolDown = Param(nameof(CoolDown), 50)
			.SetGreaterThanZero()
			.SetDisplay("Cool Down", "Bars to wait after trade", "Parameters");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_buyCooldownCounter = _sellCooldownCounter = CoolDown;
	}

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

		var sma = new SimpleMovingAverage { Length = RollingWindow };
		var stdDev = new StandardDeviation { Length = RollingWindow };

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

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

	private void ProcessCandle(ICandleMessage candle, decimal smaValue, decimal stdDevValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (stdDevValue == 0)
			return;

		var zScore = (candle.ClosePrice - smaValue) / stdDevValue;

		if (zScore > ZThreshold)
		{
			if (_sellCooldownCounter >= CoolDown)
			{
				if (Position >= 0)
					SellMarket();
				_sellCooldownCounter = 0;
				_buyCooldownCounter = CoolDown;
			}
			else
			{
				_sellCooldownCounter++;
			}
		}
		else if (zScore < -ZThreshold)
		{
			if (_buyCooldownCounter >= CoolDown)
			{
				if (Position <= 0)
					BuyMarket();
				_sellCooldownCounter = CoolDown;
				_buyCooldownCounter = 0;
			}
			else
			{
				_buyCooldownCounter++;
			}
		}
	}
}