Открыть на GitHub

Автоматическая торговля по полосам Боллинджера

Стратегия использует полосы Боллинджера, RSI и стохастический осциллятор для автоматического открытия сделок в заданном временном окне по GMT. Короткая позиция открывается, когда предыдущая свеча закрывается выше верхней полосы Боллинджера при RSI выше 75 и значении %K стохастика выше 85. Длинная позиция открывается, когда свеча закрывается ниже нижней полосы при RSI ниже 25 и %K стохастика ниже 155. Одновременно допускается только одна позиция в каждом направлении. Открытые позиции защищаются трейлинг-стопом в пунктах.

Параметры

  • OpenBuy – разрешить открытие длинных позиций.
  • OpenSell – разрешить открытие коротких позиций.
  • GmtTradeStart – начало торговли (час GMT, исключая).
  • GmtTradeStop – окончание торговли (час GMT, исключая).
  • BbPeriod – период полос Боллинджера.
  • RsiPeriod – период индикатора RSI.
  • StochKPeriod – период %K стохастика.
  • StochDPeriod – период %D стохастика.
  • StochSlowing – сглаживание стохастика.
  • TrailingStop – расстояние трейлинг-стопа в пунктах.
  • CandleType – таймфрейм свечей.
using System;
using System.Collections.Generic;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Bollinger Bands auto trade strategy with RSI confirmation.
/// Sells when price touches upper band and RSI is overbought.
/// Buys when price touches lower band and RSI is oversold.
/// </summary>
public class AutoTradeWithBollingerBandsStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _bbPeriod;
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _rsiOverbought;
	private readonly StrategyParam<decimal> _rsiOversold;

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public int BbPeriod
	{
		get => _bbPeriod.Value;
		set => _bbPeriod.Value = value;
	}

	public int RsiPeriod
	{
		get => _rsiPeriod.Value;
		set => _rsiPeriod.Value = value;
	}

	public decimal RsiOverbought
	{
		get => _rsiOverbought.Value;
		set => _rsiOverbought.Value = value;
	}

	public decimal RsiOversold
	{
		get => _rsiOversold.Value;
		set => _rsiOversold.Value = value;
	}

	public AutoTradeWithBollingerBandsStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");

		_bbPeriod = Param(nameof(BbPeriod), 20)
			.SetDisplay("BB Period", "Bollinger Bands period", "Indicators");

		_rsiPeriod = Param(nameof(RsiPeriod), 6)
			.SetDisplay("RSI Period", "RSI period", "Indicators");

		_rsiOverbought = Param(nameof(RsiOverbought), 70m)
			.SetDisplay("RSI Overbought", "RSI overbought level", "Indicators");

		_rsiOversold = Param(nameof(RsiOversold), 30m)
			.SetDisplay("RSI Oversold", "RSI oversold level", "Indicators");
	}

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

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

		var bollinger = new BollingerBands { Length = BbPeriod, Width = 2m };
		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

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

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

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

		var bb = (BollingerBandsValue)bbValue;
		if (bb.UpBand is not decimal upper ||
			bb.LowBand is not decimal lower ||
			bb.MovingAverage is not decimal middle)
			return;

		var rsi = rsiValue.ToDecimal();
		var price = candle.ClosePrice;

		// Sell when price above upper band and RSI overbought
		if (price > upper && rsi > RsiOverbought && Position >= 0)
		{
			SellMarket();
		}
		// Buy when price below lower band and RSI oversold
		else if (price < lower && rsi < RsiOversold && Position <= 0)
		{
			BuyMarket();
		}
		// Exit long when price returns to middle
		else if (Position > 0 && price >= middle)
		{
			SellMarket();
		}
		// Exit short when price returns to middle
		else if (Position < 0 && price <= middle)
		{
			BuyMarket();
		}
	}
}