在 GitHub 上查看

成交量突破策略

本策略观察成交量的快速扩张。当读数远高于平均水平时,价格往往酝酿新的走势。

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

当成交量突破依据历史数据及偏差倍数构建的通道时进场,可做多或做空,并配合止损。成交量回到均值附近即离场。

适合寻求早期突破的动量交易者。

详细信息

  • 入场条件: Indicator exceeds average by deviation multiplier.
  • Long/Short: 双向 directions.
  • 退出条件: Indicator reverts to average.
  • 止损: 是
  • 默认值:
    • AvgPeriod = 20
    • Multiplier = 2.0m
    • CandleType = TimeSpan.FromMinutes(5)
    • StopLoss = 2.0m
  • 筛选条件:
    • 类别: 突破
    • 方向: 双向
    • 指标: Volume
    • 止损: 是
    • 复杂度: 中等
    • 时间框架: 短期
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中等
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>
/// Strategy that trades on volume breakouts.
/// When volume rises significantly above its average, it enters position in the direction determined by price.
/// </summary>
public class VolumeBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _avgPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _stopLoss;
	
	private SimpleMovingAverage _volumeAverage;
	private SimpleMovingAverage _volumeStdDev;
	private decimal _lastAvgVolume;
	private decimal _lastStdDev;
	
	/// <summary>
	/// Period for volume average calculation.
	/// </summary>
	public int AvgPeriod
	{
		get => _avgPeriod.Value;
		set => _avgPeriod.Value = value;
	}
	
	/// <summary>
	/// Standard deviation multiplier for breakout detection.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}
	
	/// <summary>
	/// Candle type for strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}
	
	/// <summary>
	/// Stop-loss percentage.
	/// </summary>
	public decimal StopLoss
	{
		get => _stopLoss.Value;
		set => _stopLoss.Value = value;
	}
	
	/// <summary>
	/// Initialize <see cref="VolumeBreakoutStrategy"/>.
	/// </summary>
	public VolumeBreakoutStrategy()
	{
		_avgPeriod = Param(nameof(AvgPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Average Period", "Period for volume average calculation", "Indicators")
			
			.SetOptimize(10, 50, 5);
		
		_multiplier = Param(nameof(Multiplier), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "Standard deviation multiplier for breakout detection", "Indicators")
			
			.SetOptimize(1.0m, 3.0m, 0.5m);
		
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
		
		_stopLoss = Param(nameof(StopLoss), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss %", "Stop Loss percentage", "Risk Management")
			
			.SetOptimize(1.0m, 5.0m, 0.5m);
	}
	
	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}
	
	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_lastAvgVolume = 0;
		_lastStdDev = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		
		
		// Create indicators for volume analysis
		_volumeAverage = new SMA { Length = AvgPeriod };
		_volumeStdDev = new SMA { Length = AvgPeriod };
		
		// Create subscription
		var subscription = SubscribeCandles(CandleType);
		
		// Bind candles to processing method
		subscription
			.Bind(ProcessCandle)
			.Start();
			
		// Enable stop loss protection
		StartProtection(
			takeProfit: new Unit(3, UnitTypes.Percent),
			stopLoss: new Unit(StopLoss, UnitTypes.Percent));
		
		// Create chart area for visualization
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
		}
	}
	
	private void ProcessCandle(ICandleMessage candle)
	{
		if (candle.State != CandleStates.Finished)
			return;
			
		// Calculate volume indicators
		var volume = candle.TotalVolume;
		
		// Calculate volume average
		var avgValue = _volumeAverage.Process(new DecimalIndicatorValue(_volumeAverage, volume, candle.ServerTime) { IsFinal = true });
		var avgVolume = avgValue.ToDecimal();

		// Calculate standard deviation approximation
		var deviation = Math.Abs(volume - avgVolume);
		var stdDevValue = _volumeStdDev.Process(new DecimalIndicatorValue(_volumeStdDev, deviation, candle.ServerTime) { IsFinal = true });
		var stdDev = stdDevValue.ToDecimal();

		// Skip the first N candles until we have enough data
		if (!_volumeAverage.IsFormed || !_volumeStdDev.IsFormed)
		{
			_lastAvgVolume = avgVolume;
			_lastStdDev = stdDev;
			return;
		}

		// Volume breakout detection (volume increases significantly above its average)
		if (volume > avgVolume + Multiplier * stdDev && Position == 0)
		{
			// Determine direction based on price movement
			var bullish = candle.ClosePrice > candle.OpenPrice;

			// Trade in the direction of price movement
			if (bullish)
			{
				BuyMarket();
			}
			else
			{
				SellMarket();
			}
		}

		// Update last values
		_lastAvgVolume = avgVolume;
		_lastStdDev = stdDev;
	}
}