在 GitHub 上查看

VQZL Z-Score

该策略基于价格相对平滑均值的Z分数。

测试显示年化收益约42%,在股票市场表现最好。

策略计算平滑移动平均线和标准差得到Z分数。当价格超出阈值时,按走势方向入场。

详情

  • 入场条件
    • 做多Z-Score > threshold
    • 做空Z-Score < -threshold
  • 多空方向:双向。
  • 出场条件:反向信号。
  • 止损:无。
  • 默认值
    • PriceSmoothing = 15
    • ZLength = 100
    • Threshold = 1.64
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤条件
    • 类型:趋势
    • 方向:双向
    • 指标:SMA, StandardDeviation
    • 止损:无
    • 复杂度:基础
    • 时间框架:日内
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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 strategy based on price deviation from a moving average.
/// Long when z-score crosses above the threshold.
/// Short when z-score crosses below the negative threshold.
/// </summary>
public class VqzlZScoreStrategy : Strategy
{
    private readonly StrategyParam<int> _priceSmoothing;
    private readonly StrategyParam<int> _zLength;
    private readonly StrategyParam<decimal> _threshold;
    private readonly StrategyParam<DataType> _candleType;

    /// <summary>
    /// Period for price smoothing moving average.
    /// </summary>
    public int PriceSmoothing
    {
	get => _priceSmoothing.Value;
	set => _priceSmoothing.Value = value;
    }

    /// <summary>
    /// Lookback length for standard deviation calculation.
    /// </summary>
    public int ZLength
    {
	get => _zLength.Value;
	set => _zLength.Value = value;
    }

    /// <summary>
    /// Z-score threshold for entries.
    /// </summary>
    public decimal Threshold
    {
	get => _threshold.Value;
	set => _threshold.Value = value;
    }

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

    /// <summary>
    /// Initializes a new instance of <see cref="VqzlZScoreStrategy"/>.
    /// </summary>
    public VqzlZScoreStrategy()
    {
	_priceSmoothing = Param(nameof(PriceSmoothing), 15)
	    .SetGreaterThanZero()
	    .SetDisplay("Price Smoothing", "Length of smoothing moving average", "ZScore")
	    
	    .SetOptimize(5, 50, 5);

	_zLength = Param(nameof(ZLength), 100)
	    .SetGreaterThanZero()
	    .SetDisplay("Z Length", "Lookback for standard deviation", "ZScore")
	    
	    .SetOptimize(50, 200, 10);

	_threshold = Param(nameof(Threshold), 1.64m)
	    .SetGreaterThanZero()
	    .SetDisplay("Z Threshold", "Z-score threshold", "ZScore")
	    
	    .SetOptimize(1m, 3m, 0.5m);

	_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
	    .SetDisplay("Candle Type", "Type of candles", "General");
    }

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

    /// <inheritdoc />
    protected override void OnReseted()
    {
	base.OnReseted();
    }

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

	var ma = new SMA { Length = PriceSmoothing };
	var dev = new StandardDeviation { Length = ZLength };

	var subscription = SubscribeCandles(CandleType);

	subscription
	    .Bind(ma, dev, ProcessCandle)
	    .Start();

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

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

	if (devValue == 0)
	    return;

	var z = (candle.ClosePrice - maValue) / devValue;

	if (z > Threshold && Position <= 0)
	{
	    BuyMarket(Volume + Math.Abs(Position));
	}
	else if (z < -Threshold && Position >= 0)
	{
	    SellMarket(Volume + Math.Abs(Position));
	}
    }
}