Ver no GitHub

Max Pain Strategy

This strategy enters long positions when both volume and price movement exceed configurable thresholds while the VIX index remains below a specified level. A volatility-based stop-loss is set on entry and the position is closed after a fixed number of periods.

Details

  • Entry Criteria:
    • Long: volume greater than average volume × VolumeMultiplier and price change greater than previous close × PriceChangeMultiplier with VIX below VixThreshold.
  • Long/Short: Long only.
  • Exit Criteria:
    • Stop-loss at StopLossMultiplier × volatility below entry price.
    • Close position after HoldPeriods bars.
  • Stops: Yes.
  • Default Values:
    • LookbackPeriod = 70.
    • VolumeMultiplier = 1.
    • PriceChangeMultiplier = 0.029.
    • StopLossMultiplier = 2.4.
    • VixThreshold = 44.
    • HoldPeriods = 8.
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame().
    • VixCandleType = TimeSpan.FromMinutes(5).TimeFrame().
  • Filters:
    • Category: Breakout
    • Direction: Long only
    • Indicators: Volume, price action, volatility
    • Stops: Yes
    • Complexity: Basic
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
using System;
using System.Linq;
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;

public class MaxPainStrategy : Strategy
{
	private readonly StrategyParam<int> _lookbackPeriod;
	private readonly StrategyParam<int> _holdPeriods;
	private readonly StrategyParam<DataType> _candleType;

	private int _barIndex;
	private int? _entryBar;
	private readonly List<decimal> _volumes = new();
	private decimal _prevClose;

	public int LookbackPeriod { get => _lookbackPeriod.Value; set => _lookbackPeriod.Value = value; }
	public int HoldPeriods { get => _holdPeriods.Value; set => _holdPeriods.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MaxPainStrategy()
	{
		_lookbackPeriod = Param(nameof(LookbackPeriod), 20);
		_holdPeriods = Param(nameof(HoldPeriods), 8);
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_barIndex = 0;
		_entryBar = null;
		_prevClose = 0m;
		_volumes.Clear();
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		StartProtection(null, null);

		_barIndex = 0;
		_entryBar = null;
		_prevClose = 0;
		_volumes.Clear();

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

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

		_barIndex++;
		_volumes.Add(candle.TotalVolume);
		if (_volumes.Count > LookbackPeriod)
			_volumes.RemoveAt(0);

		if (_volumes.Count < LookbackPeriod || _prevClose == 0)
		{
			_prevClose = candle.ClosePrice;
			return;
		}

		var avgVolume = _volumes.Average();
		var priceChange = Math.Abs(candle.ClosePrice - _prevClose);

		// Volume spike with significant price move
		var painZone = candle.TotalVolume > avgVolume * 1.2m && priceChange > _prevClose * 0.003m;

		if (painZone && Position <= 0)
		{
			BuyMarket();
			_entryBar = _barIndex;
		}

		if (Position > 0 && _entryBar.HasValue && _barIndex >= _entryBar.Value + HoldPeriods)
		{
			SellMarket();
			_entryBar = null;
		}

		_prevClose = candle.ClosePrice;
	}
}