Auf GitHub ansehen

Bollinger-Band-Umkehr-Strategie

Preisextreme außerhalb der Bollinger Bands kehren oft zur mittleren Band zurück. Dieser Ansatz geht gegen diese Ausdehnungen vor: Er kauft Einbrüche unterhalb des unteren Bands, wenn die Kerze grün schließt, und verkauft Rallyes über dem oberen Band nach einer roten Kerze.

Tests zeigen eine durchschnittliche jährliche Rendite von etwa 94%. Sie funktioniert am besten auf dem Aktienmarkt.

Der Algorithmus berechnet Bollinger Bands auf jedem Balken und prüft, ob der Schlusskurs das äußere Band durchbricht. Wenn eine bullische Kerze unter dem unteren Band schließt, wird ein Long eröffnet; wenn eine bärische Kerze über dem oberen Band schließt, wird ein Short eingegangen. Der Stop basiert auf einem ATR-Vielfachen, während Ausstiege erfolgen, wenn der Preis zur mittleren Band zurückkehrt.

Mean-Reversion-Trades dauern typischerweise nur wenige Balken, was dieses Setup für kurzfristige Volatilitätskontraktionen geeignet macht.

Details

  • Einstiegskriterien: Schluss unter unterem Band mit bullischer Kerze oder Schluss über oberem Band mit bärischer Kerze.
  • Long/Short: Beide.
  • Ausstiegskriterien: Preis kreuzt mittleres Band oder Stop-Loss.
  • Stops: Ja, ATR-basiert.
  • Standardwerte:
    • BollingerPeriod = 20
    • BollingerDeviation = 2.0
    • AtrMultiplier = 2.0
    • CandleType = 5 minute
  • Filter:
    • Kategorie: Mean Reversion
    • Richtung: Beide
    • Indikatoren: Bollinger Bands, ATR
    • Stops: Ja
    • Komplexität: Grundlegend
    • Zeitrahmen: Intraday
    • Saisonalität: Nein
    • Neuronale Netze: Nein
    • Divergenz: Nein
    • Risikolevel: Mittel
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>
/// Bollinger Band Reversal strategy.
/// Enters long when price is below the lower Bollinger Band and candle is bullish.
/// Enters short when price is above the upper Bollinger Band and candle is bearish.
/// Exits at middle band.
/// </summary>
public class BollingerBandReversalStrategy : Strategy
{
	private readonly StrategyParam<int> _bollingerPeriod;
	private readonly StrategyParam<decimal> _bollingerDeviation;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private int _cooldown;

	/// <summary>
	/// Bollinger Bands period.
	/// </summary>
	public int BollingerPeriod
	{
		get => _bollingerPeriod.Value;
		set => _bollingerPeriod.Value = value;
	}

	/// <summary>
	/// Bollinger Bands deviation multiplier.
	/// </summary>
	public decimal BollingerDeviation
	{
		get => _bollingerDeviation.Value;
		set => _bollingerDeviation.Value = value;
	}

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

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public BollingerBandReversalStrategy()
	{
		_bollingerPeriod = Param(nameof(BollingerPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Bollinger Period", "Period for Bollinger Bands", "Indicators");

		_bollingerDeviation = Param(nameof(BollingerDeviation), 2.0m)
			.SetNotNegative()
			.SetDisplay("Bollinger Deviation", "Standard deviations for Bollinger Bands", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_cooldownBars = Param(nameof(CooldownBars), 500)
			.SetRange(1, 1000)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = default;
	}

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

		_cooldown = 0;

		var bollingerBands = new BollingerBands
		{
			Length = BollingerPeriod,
			Width = BollingerDeviation
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(bollingerBands, ProcessCandle)
			.Start();

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

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

		if (!bollingerValue.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

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

		var bb = (BollingerBandsValue)bollingerValue;
		var upperBand = bb.UpBand;
		var lowerBand = bb.LowBand;
		var middleBand = bb.MovingAverage;

		var isBullish = candle.ClosePrice > candle.OpenPrice;
		var isBearish = candle.ClosePrice < candle.OpenPrice;

		if (Position == 0 && candle.ClosePrice < lowerBand && isBullish)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
		else if (Position == 0 && candle.ClosePrice > upperBand && isBearish)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position > 0 && candle.ClosePrice > middleBand)
		{
			SellMarket();
			_cooldown = CooldownBars;
		}
		else if (Position < 0 && candle.ClosePrice < middleBand)
		{
			BuyMarket();
			_cooldown = CooldownBars;
		}
	}
}