Esta estrategia combina las Bandas de Bollinger con el indicador Supertrend para identificar entradas durante movimientos direccionales fuertes. Las Bandas de Bollinger miden la expansión de volatilidad mientras que la línea Supertrend sigue la tendencia general y actúa como stop dinámico.
Las pruebas indican un rendimiento anual promedio de aproximadamente 79%. Funciona mejor en el mercado de acciones.
Una operación larga se activa cuando el precio cierra por encima de la Banda de Bollinger superior y permanece por encima de la línea Supertrend, confirmando el alineamiento del momentum y la tendencia. Una operación corta ocurre cuando el precio cierra por debajo de la banda inferior mientras se mantiene bajo el nivel Supertrend. Las operaciones se cierran una vez que el precio cruza de vuelta a través del Supertrend, indicando que el momentum ha desaparecido.
Debido a que el sistema espera rupturas más allá de la volatilidad normal, es adecuado para traders que buscan capturar movimientos sostenidos en lugar de reversiones rápidas. El stop Supertrend se ajusta dinámicamente a los movimientos del mercado, ayudando a gestionar el riesgo sin intervención manual.
Detalles
Criterios de entrada:
Largo: Close > upper Bollinger Band && Close > Supertrend
Corto: Close < lower Bollinger Band && Close < Supertrend
Largo/Corto: Ambos lados.
Criterios de salida:
Largo: Salir cuando el precio cruza por debajo del Supertrend
Corto: Salir cuando el precio cruza por encima del Supertrend
Stops: Sí, vía stop dinámico Supertrend.
Valores predeterminados:
BollingerPeriod = 20
BollingerDeviation = 2
SupertrendPeriod = 10
SupertrendMultiplier = 3
CandleType = TimeSpan.FromMinutes(15)
Un largo se cierra cuando el precio cierra por debajo de la línea Supertrend y un corto cuando cierra por encima. Una señal de entrada contra una posición abierta la invierte.
Filtros:
Categoría: Tendencia
Dirección: Ambos
Indicadores: Bollinger Bands, Supertrend
Stops: Sí
Complejidad: Intermedio
Marco temporal: Intradía
Estacionalidad: No
Redes neuronales: No
Divergencia: No
Nivel de riesgo: Medio
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 Supertrend strategy.
/// A close above the upper Bollinger band that is also above the Supertrend line goes long and a close below the lower band that is also
/// below the Supertrend goes short, reversing an opposite position. The Supertrend is the trailing stop: a long closes when price closes
/// below the line and a short when it closes above it.
/// </summary>
public class BollingerSupertrendStrategy : Strategy
{
private readonly StrategyParam<int> _bollingerPeriod;
private readonly StrategyParam<decimal> _bollingerDeviation;
private readonly StrategyParam<int> _supertrendPeriod;
private readonly StrategyParam<decimal> _supertrendMultiplier;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Period of the Bollinger Bands.
/// </summary>
public int BollingerPeriod
{
get => _bollingerPeriod.Value;
set => _bollingerPeriod.Value = value;
}
/// <summary>
/// Standard deviation multiplier of the bands.
/// </summary>
public decimal BollingerDeviation
{
get => _bollingerDeviation.Value;
set => _bollingerDeviation.Value = value;
}
/// <summary>
/// ATR period of Supertrend.
/// </summary>
public int SupertrendPeriod
{
get => _supertrendPeriod.Value;
set => _supertrendPeriod.Value = value;
}
/// <summary>
/// ATR multiplier of Supertrend.
/// </summary>
public decimal SupertrendMultiplier
{
get => _supertrendMultiplier.Value;
set => _supertrendMultiplier.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public BollingerSupertrendStrategy()
{
_bollingerPeriod = Param(nameof(BollingerPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("BB Period", "Period of the Bollinger Bands", "Bollinger");
_bollingerDeviation = Param(nameof(BollingerDeviation), 2m)
.SetGreaterThanZero()
.SetDisplay("BB Deviation", "Standard deviation multiplier of the bands", "Bollinger");
_supertrendPeriod = Param(nameof(SupertrendPeriod), 10)
.SetGreaterThanZero()
.SetDisplay("Supertrend Period", "ATR period of Supertrend", "Supertrend");
_supertrendMultiplier = Param(nameof(SupertrendMultiplier), 3m)
.SetGreaterThanZero()
.SetDisplay("Supertrend Multiplier", "ATR multiplier of Supertrend", "Supertrend");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).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 OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bollinger = new BollingerBands { Length = BollingerPeriod, Width = BollingerDeviation };
var supertrend = new SuperTrend { Length = SupertrendPeriod, Multiplier = SupertrendMultiplier };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(bollinger, supertrend, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, bollinger);
DrawIndicator(area, supertrend);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue bollingerValue, IIndicatorValue supertrendValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!bollingerValue.IsFormed || !supertrendValue.IsFormed)
return;
var bands = (BollingerBandsValue)bollingerValue;
if (bands.UpBand is not decimal upper || bands.LowBand is not decimal lower || supertrendValue is not SuperTrendIndicatorValue trend)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var line = trend.Value;
var close = candle.ClosePrice;
if (close > upper && close > line && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (close < lower && close < line && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
else if (Position > 0 && close < line)
SellMarket(Position);
else if (Position < 0 && close > line)
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import BollingerBands, SuperTrend
from StockSharp.Algo.Strategies import Strategy
class bollinger_supertrend_strategy(Strategy):
"""
Bollinger Supertrend strategy.
A close above the upper Bollinger band that is also above the Supertrend line goes long and a close below the lower band that is also
below the Supertrend goes short, reversing an opposite position. The Supertrend is the trailing stop: a long closes when price closes
below the line and a short when it closes above it.
"""
def __init__(self):
super(bollinger_supertrend_strategy, self).__init__()
self._bollinger_period = self.Param("BollingerPeriod", 20).SetGreaterThanZero().SetDisplay("BB Period", "Period of the Bollinger Bands", "Bollinger")
self._bollinger_deviation = self.Param("BollingerDeviation", 2.0).SetGreaterThanZero().SetDisplay("BB Deviation", "Standard deviation multiplier of the bands", "Bollinger")
self._supertrend_period = self.Param("SupertrendPeriod", 10).SetGreaterThanZero().SetDisplay("Supertrend Period", "ATR period of Supertrend", "Supertrend")
self._supertrend_multiplier = self.Param("SupertrendMultiplier", 3.0).SetGreaterThanZero().SetDisplay("Supertrend Multiplier", "ATR multiplier of Supertrend", "Supertrend")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))).SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(bollinger_supertrend_strategy, self).OnStarted2(time)
bollinger = BollingerBands()
bollinger.Length = self._bollinger_period.Value
bollinger.Width = Decimal(self._bollinger_deviation.Value)
supertrend = SuperTrend()
supertrend.Length = self._supertrend_period.Value
supertrend.Multiplier = Decimal(self._supertrend_multiplier.Value)
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(bollinger, supertrend, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, bollinger)
self.DrawIndicator(area, supertrend)
self.DrawOwnTrades(area)
def _process_candle(self, candle, bollinger_value, supertrend_value):
if candle.State != CandleStates.Finished:
return
if not bollinger_value.IsFormed or not supertrend_value.IsFormed:
return
if bollinger_value.UpBand is None or bollinger_value.LowBand is None:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
upper = bollinger_value.UpBand
lower = bollinger_value.LowBand
line = supertrend_value.Value
close = candle.ClosePrice
if close > upper and close > line and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif close < lower and close < line and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif self.Position > 0 and close < line:
self.SellMarket(self.Position)
elif self.Position < 0 and close > line:
self.BuyMarket(-self.Position)
def CreateClone(self):
return bollinger_supertrend_strategy()