在 GitHub 上查看

VWAP 回踩策略

成交量加权平均价(VWAP)是常用的日内基准。当价格远离 VWAP 后又出现向其回归的蜡烛时,通常会有短暂的反弹。本策略交易这种回踩。

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

每根K线计算当前的 VWAP。如果看涨蜡烛收盘低于 VWAP,则做多;若看跌蜡烛收盘高于 VWAP,则做空。风险通过固定百分比止损控制,持仓通常仅维持到出现反向信号或止损触发。

由于属于日内极端的均值回归方法,该策略在震荡行情中效果更佳。

细节

  • 入场条件:阳线收盘低于 VWAP 或阴线收盘高于 VWAP。
  • 多/空:双向。
  • 退出条件:出现反向信号或止损。
  • 止损:是,按百分比。
  • 默认值:
    • CandleType = 5 分钟
    • StopLossPercent = 2 VWAP在每个UTC日开始时重新计算,按成交量对每根K线的典型价格加权。
  • 过滤条件:
    • 类别: 均值回归
    • 方向: 双向
    • 指标: VWAP
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险级别: 中等
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>
/// VWAP Bounce strategy.
/// The VWAP restarts every UTC day from the typical price of each candle weighted by its volume. A bullish candle closing below
/// the VWAP turns the position long and a bearish candle closing above it turns it short; a percent stop limits the loss.
/// </summary>
public class VwapBounceStrategy : Strategy
{
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private DateTime? _day;
	private decimal _priceVolume;
	private decimal _volume;

	/// <summary>
	/// Stop-loss percentage.
	/// </summary>
	public decimal StopLossPercent
	{
		get => _stopLossPercent.Value;
		set => _stopLossPercent.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public VwapBounceStrategy()
	{
		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetNotNegative()
			.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");

		_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();
		_day = null;
		_priceVolume = default;
		_volume = default;
	}

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

		_day = null;
		_priceVolume = default;
		_volume = default;

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

		StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);

		// The stop has to see prices between candles, not only at their close.
		foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
		{
			var quotes = new Subscription(DataType.Level1, Security);
			quotes.MarketData.BuildField = field;
			SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
		}

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

	private void ObserveProtectionQuote(Level1ChangeMessage quote)
	{
		// The high-level handler activates native protection before this callback, also between signal bars.
	}

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

		var day = candle.OpenTime.Date;

		if (_day != day)
		{
			_day = day;
			_priceVolume = 0;
			_volume = 0;
		}

		var typical = (candle.HighPrice + candle.LowPrice + candle.ClosePrice) / 3m;
		_priceVolume += typical * candle.TotalVolume;
		_volume += candle.TotalVolume;

		if (_volume <= 0 || !IsFormedAndOnlineAndAllowTrading())
			return;

		var vwap = _priceVolume / _volume;
		var close = candle.ClosePrice;

		if (close > candle.OpenPrice && close < vwap && Position <= 0)
			BuyMarket(Volume + Math.Abs(Position));
		else if (close < candle.OpenPrice && close > vwap && Position >= 0)
			SellMarket(Volume + Math.Abs(Position));
	}
}