在 GitHub 上查看

Volatility Adjusted Mean Reversion 策略

此均值回归变体根据ATR与标准差之比调整入场阈值。若相对噪声的波动增大,触发交易所需的距离也随之增加,避免在剧烈波动时过早进场。

测试表明年均收益约为 115%,该策略在股票市场表现最佳。

当价格低于均线超过调整后的阈值时做多;价格高于均线同样距离时做空。价格重新靠近均线时离场。止损等于2倍ATR,在等待回归期间控制风险。

该自适应阈值适用于波动性变化较大的市场。

细节

  • 入场条件:
    • 多头: Price < MA - Multiplier * ATR/StdDev
    • 空头: Price > MA + Multiplier * ATR/StdDev
  • 多/空: 双向
  • 离场条件:
    • 多头: 收盘价>= MA
    • 空头: 收盘价<= MA
  • 止损: 基于ATR动态设置
  • 默认值:
    • Period = 20
    • Multiplier = 2 MA、ATR和StdDev(收盘价标准差)均基于Period根K线;阈值可化简为Multiplier倍StdDev。止损距入场收盘价Multiplier倍ATR,默认两倍ATR,按K线收盘检查。与持仓方向相反的入场信号会反转持仓。
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤器:
    • 类别: Mean Reversion
    • 方向: 双向
    • 指标: ATR, StdDev
    • 止损: 是
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Volatility Adjusted Mean Reversion strategy.
/// The threshold is Multiplier times the ATR divided by the ATR to standard deviation ratio, all over Period candles. A close more than the
/// threshold below the Period simple moving average goes long and one that far above it goes short, reversing an opposite position.
/// A long closes once the close is back at or above the average and a short once it is back at or below it. The stop lies Multiplier ATR
/// from the entry close and is checked on candle closes.
/// </summary>
public class VolatilityAdjustedMeanReversionStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _stopPrice;

	/// <summary>
	/// Period of the SMA, ATR and standard deviation.
	/// </summary>
	public int Period
	{
		get => _period.Value;
		set => _period.Value = value;
	}

	/// <summary>
	/// Multiplier of the threshold and the stop.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public VolatilityAdjustedMeanReversionStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Period of the SMA, ATR and standard deviation", "Parameters");

		_multiplier = Param(nameof(Multiplier), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "Multiplier of the threshold and the stop", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();
		_stopPrice = default;
	}

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

		_stopPrice = default;

		var sma = new SimpleMovingAverage { Length = Period };
		var atr = new AverageTrueRange { Length = Period };
		var stdev = new StandardDeviation { Length = Period };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(sma, atr, stdev, ProcessCandle)
			.Start();

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

			var oscillators = CreateChartArea();
			if (oscillators != null)
			{
				DrawIndicator(oscillators, atr);
			}
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue smaValue, IIndicatorValue atrValue, IIndicatorValue stdevValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!smaValue.IsFormed || !atrValue.IsFormed || !stdevValue.IsFormed)
			return;

		var atr = atrValue.GetValue<decimal>();
		var deviation = stdevValue.GetValue<decimal>();

		if (atr <= 0 || deviation <= 0)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var sma = smaValue.GetValue<decimal>();
		var threshold = Multiplier * atr / (atr / deviation);
		var close = candle.ClosePrice;

		if (close < sma - threshold && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
			_stopPrice = close - Multiplier * atr;
		}
		else if (close > sma + threshold && Position >= 0)
		{
			SellMarket(Volume + Math.Abs(Position));
			_stopPrice = close + Multiplier * atr;
		}
		else if (Position > 0 && (close >= sma || close <= _stopPrice))
		{
			SellMarket(Position);
		}
		else if (Position < 0 && (close <= sma || close >= _stopPrice))
		{
			BuyMarket(-Position);
		}
	}
}