Открыть на GitHub

Прорыв по объему

Стратегия Volume Breakout наблюдает за резким ростом объема. Когда показания выходят за пределы среднего диапазона, цена часто начинает новое движение.

Тестирование показывает среднегодичную доходность около 103%. Стратегию лучше запускать на фондовом рынке.

Позиция открывается, как только индикатор пробивает диапазон, рассчитанный по последним данным и множителю отклонения. Возможны сделки в обе стороны со стопом.

Система подходит трейдерам импульсных стратегий, ищущим ранние прорывы. Сделки закрываются, когда объем возвращается к среднему. По умолчанию используется AvgPeriod = 20.

Подробности

  • Условия входа: индикатор превышает среднее на величину множителя отклонения.
  • Длинные/короткие: оба направления.
  • Условия выхода: индикатор возвращается к среднему.
  • Стопы: да.
  • Значения по умолчанию:
    • AvgPeriod = 20
    • Multiplier = 2
    • CandleType = TimeSpan.FromMinutes(5)
    • StopLossPercent = 2 Avg и StdDev — среднее и стандартное отклонение последних AvgPeriod значений объёма, включая текущее. Стоп — фиксированный StopLossPercent от цены входа, он отслеживается и между свечами; 0 отключает его. Полоса — среднее плюс Multiplier стандартных отклонений: объём выше неё на растущей свече открывает лонг, на падающей — шорт; позиция закрывается, когда объём опускается ниже своего среднего. Сигнал на вход против открытой позиции разворачивает её.
  • Фильтры:
    • Категория: Breakout
    • Направление: оба
    • Индикаторы: Volume
    • Стопы: да
    • Сложность: средняя
    • Таймфрейм: краткосрочный
    • Сезонность: нет
    • Нейросети: нет
    • Дивергенция: нет
    • Уровень риска: средний
using System;
using System.Collections.Generic;
using System.Linq;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Volume Breakout strategy.
/// The bands lie Multiplier standard deviations around the average of the last AvgPeriod volume values, the current one included.
/// volume above the upper band on a rising candle goes long and on a falling candle goes short, reversing an opposite position.
/// A position closes once volume falls back below its average, and a percent stop limits the loss.
/// </summary>
public class VolumeBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _avgPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private readonly Queue<decimal> _values = [];

	/// <summary>
	/// Values of volume the average and the standard deviation span.
	/// </summary>
	public int AvgPeriod
	{
		get => _avgPeriod.Value;
		set => _avgPeriod.Value = value;
	}

	/// <summary>
	/// Standard deviations between the average and a band.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

	/// <summary>
	/// Stop loss percentage from entry price.
	/// </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 VolumeBreakoutStrategy()
	{
		_avgPeriod = Param(nameof(AvgPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Average Period", "Values of volume the average and the standard deviation span", "Indicators");

		_multiplier = Param(nameof(Multiplier), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "Standard deviations between the average and a band", "Indicators");

		_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();
		_values.Clear();
	}

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

		_values.Clear();

		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 value = candle.TotalVolume;

		_values.Enqueue(value);

		if (_values.Count > AvgPeriod)
			_values.Dequeue();

		if (_values.Count < AvgPeriod)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var mean = _values.Average();
		var deviation = (decimal)Math.Sqrt((double)_values.Average(v => (v - mean) * (v - mean)));
		var upper = mean + Multiplier * deviation;

		if (value > upper && candle.ClosePrice > candle.OpenPrice && Position <= 0)
			BuyMarket(Volume + Math.Abs(Position));
		else if (value > upper && candle.ClosePrice < candle.OpenPrice && Position >= 0)
			SellMarket(Volume + Math.Abs(Position));
		else if (Position > 0 && value < mean)
			SellMarket(Position);
		else if (Position < 0 && value < mean)
			BuyMarket(-Position);
	}
}