GitHub で見る

VWAP Volume 戦略

VWAP と出来高インジケーターを組み合わせた戦略。平均を超える出来高で確認された VWAP ブレイクアウトで買い/売りを行います。

テストでは年平均リターン約 52% を示しています。暗号資産市場で最も優れたパフォーマンスを発揮します。

この戦略は VWAP を使って価値を測り、取引前に出来高の確認を必要とします。強い市場参加によって支持された動きに乗ることが狙いです。

出来高指標に注目するイントラデイトレーダーに適した手法です。ATR ベースのストップで損失を抑えます。

詳細

  • エントリー条件:
    • ロング: Close < VWAP && Volume > AvgVolume * VolumeThreshold
    • ショート: Close > VWAP && Volume > AvgVolume * VolumeThreshold
  • ロング/ショート: 両方
  • エグジット条件:
    • 価格が VWAP を逆方向に突き抜ける
  • ストップ: StopLossPercent を使用したパーセントベース
  • デフォルト値:
    • VolumePeriod = 20
    • VolumeThreshold = 1.5m
    • StopLossPercent = 2.0m
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: 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>
/// Strategy combining VWAP with volume confirmation.
/// Buys on VWAP breakout with above-average volume, sells on breakdown.
/// </summary>
public class VwapVolumeStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _volumePeriod;
	private readonly StrategyParam<decimal> _volumeThreshold;
	private readonly StrategyParam<int> _cooldownBars;

	private readonly List<decimal> _volumes = new();
	private readonly List<decimal> _typicalPriceVol = new();
	private decimal _cumVol;
	private decimal _cumTpv;
	private int _cooldown;

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

	/// <summary>
	/// Period for volume moving average.
	/// </summary>
	public int VolumePeriod
	{
		get => _volumePeriod.Value;
		set => _volumePeriod.Value = value;
	}

	/// <summary>
	/// Volume threshold multiplier.
	/// </summary>
	public decimal VolumeThreshold
	{
		get => _volumeThreshold.Value;
		set => _volumeThreshold.Value = value;
	}

	/// <summary>
	/// Cooldown bars between trades.
	/// </summary>
	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

	/// <summary>
	/// Initialize strategy.
	/// </summary>
	public VwapVolumeStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_volumePeriod = Param(nameof(VolumePeriod), 20)
			.SetRange(10, 50)
			.SetDisplay("Volume MA Period", "Period for volume moving average", "Indicators");

		_volumeThreshold = Param(nameof(VolumeThreshold), 1.5m)
			.SetDisplay("Volume Threshold", "Multiplier for average volume", "Trading Levels");

		_cooldownBars = Param(nameof(CooldownBars), 100)
			.SetDisplay("Cooldown Bars", "Bars between trades", "General")
			.SetRange(5, 500);
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_volumes.Clear();
		_typicalPriceVol.Clear();
		_cumVol = 0;
		_cumTpv = 0;
		_cooldown = 0;
	}

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

		var ema = new ExponentialMovingAverage { Length = VolumePeriod };

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(ema, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var close = candle.ClosePrice;
		var high = candle.HighPrice;
		var low = candle.LowPrice;
		var vol = candle.TotalVolume;
		var typicalPrice = (high + low + close) / 3m;

		_volumes.Add(vol);
		_cumVol += vol;
		_cumTpv += typicalPrice * vol;

		var volPrd = VolumePeriod;

		if (_volumes.Count < volPrd)
		{
			if (_cooldown > 0) _cooldown--;
			return;
		}

		// Manual VWAP (cumulative)
		var vwapValue = _cumVol > 0 ? _cumTpv / _cumVol : close;

		// Manual volume average
		decimal sumVol = 0;
		var count = _volumes.Count;
		for (int i = count - volPrd; i < count; i++)
			sumVol += _volumes[i];
		var avgVol = sumVol / volPrd;

		var highVolume = vol > avgVol * VolumeThreshold;

		if (_cooldown > 0)
		{
			_cooldown--;
			return;
		}

		// Buy: price above VWAP + high volume
		if (close > vwapValue && highVolume && Position == 0)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		// Sell: price below VWAP + high volume
		else if (close < vwapValue && highVolume && Position == 0)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}

		// Exit long: price below VWAP
		if (Position > 0 && close < vwapValue)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		// Exit short: price above VWAP
		else if (Position < 0 && close > vwapValue)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
	}
}