Bbsr Extreme
The Bbsr Extreme strategy combines Bollinger Bands breakouts with a trend filter based on a moving average. A long position appears when price rebounds from the lower band while the average is rising. A short position is opened on a pullback from the upper band when the average declines. Exits rely on ATR-based stop loss and take profit.
Details
- Entry Criteria: Price crosses bands with trend confirmation.
- Long/Short: Both directions.
- Exit Criteria: ATR stop or take profit.
- Stops: Yes, ATR based.
- Default Values:
BollingerPeriod = 20BollingerMultiplier = 2MaLength = 7AtrLength = 14AtrStopMultiplier = 2AtrProfitMultiplier = 3CandleType = TimeSpan.FromMinutes(5).TimeFrame()
- Filters:
- Category: Trend following
- Direction: Both
- Indicators: Bollinger Bands, EMA, ATR
- Stops: Yes
- Complexity: Basic
- Timeframe: Intraday (5m)
- Seasonality: No
- Neural Networks: No
- Divergence: No
- Risk Level: Medium
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy uses Bollinger Bands breakout with moving average trend filter and ATR-based exits.
/// </summary>
public class BbsrExtremeStrategy : Strategy
{
private readonly StrategyParam<int> _bollingerPeriod;
private readonly StrategyParam<decimal> _bollingerMultiplier;
private readonly StrategyParam<int> _maLength;
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<decimal> _atrStopMultiplier;
private readonly StrategyParam<decimal> _atrProfitMultiplier;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevLower;
private decimal _prevUpper;
private decimal _prevClose;
private decimal _prevMa;
private bool _isInitialized;
private decimal _entryPrice;
private int _cooldown;
/// <summary>
/// Period for Bollinger Bands calculation.
/// </summary>
public int BollingerPeriod
{
get => _bollingerPeriod.Value;
set => _bollingerPeriod.Value = value;
}
/// <summary>
/// Standard deviation multiplier for Bollinger Bands.
/// </summary>
public decimal BollingerMultiplier
{
get => _bollingerMultiplier.Value;
set => _bollingerMultiplier.Value = value;
}
/// <summary>
/// Moving average length.
/// </summary>
public int MaLength
{
get => _maLength.Value;
set => _maLength.Value = value;
}
/// <summary>
/// ATR period.
/// </summary>
public int AtrLength
{
get => _atrLength.Value;
set => _atrLength.Value = value;
}
/// <summary>
/// ATR multiplier for stop calculation.
/// </summary>
public decimal AtrStopMultiplier
{
get => _atrStopMultiplier.Value;
set => _atrStopMultiplier.Value = value;
}
/// <summary>
/// ATR multiplier for take profit.
/// </summary>
public decimal AtrProfitMultiplier
{
get => _atrProfitMultiplier.Value;
set => _atrProfitMultiplier.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes the strategy.
/// </summary>
public BbsrExtremeStrategy()
{
_bollingerPeriod = Param(nameof(BollingerPeriod), 30)
.SetGreaterThanZero()
.SetDisplay("Bollinger Period", "Bollinger Bands length", "Indicators");
_bollingerMultiplier = Param(nameof(BollingerMultiplier), 2.5m)
.SetGreaterThanZero()
.SetDisplay("Bollinger Multiplier", "Standard deviation multiplier", "Indicators");
_maLength = Param(nameof(MaLength), 7)
.SetGreaterThanZero()
.SetDisplay("MA Length", "Length for moving average", "Indicators");
_atrLength = Param(nameof(AtrLength), 14)
.SetGreaterThanZero()
.SetDisplay("ATR Length", "ATR indicator period", "Risk Management");
_atrStopMultiplier = Param(nameof(AtrStopMultiplier), 2m)
.SetGreaterThanZero()
.SetDisplay("ATR Stop Multiplier", "ATR multiplier for stop", "Risk Management");
_atrProfitMultiplier = Param(nameof(AtrProfitMultiplier), 3m)
.SetGreaterThanZero()
.SetDisplay("ATR Profit Multiplier", "ATR multiplier for take profit", "Risk Management");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).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();
_prevLower = default;
_prevUpper = default;
_prevClose = default;
_prevMa = default;
_isInitialized = false;
_entryPrice = 0m;
_cooldown = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bollinger = new BollingerBands
{
Length = BollingerPeriod,
Width = BollingerMultiplier
};
var ma = new EMA
{
Length = MaLength
};
var atr = new AverageTrueRange
{
Length = AtrLength
};
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx([bollinger, ma, atr], ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ma);
DrawIndicator(area, bollinger);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue[] values)
{
if (candle.State != CandleStates.Finished)
return;
if (values[0] is not BollingerBandsValue bbValue ||
bbValue.UpBand is not decimal upper ||
bbValue.LowBand is not decimal lower ||
!values[1].IsFormed || !values[2].IsFormed)
return;
var maValue = values[1].GetValue<decimal>();
var atrValue = values[2].GetValue<decimal>();
if (!_isInitialized)
{
_prevLower = lower;
_prevUpper = upper;
_prevClose = candle.ClosePrice;
_prevMa = maValue;
_isInitialized = true;
return;
}
if (_cooldown > 0)
_cooldown--;
var bull = _prevClose < _prevLower && candle.ClosePrice > lower && maValue > _prevMa;
var bear = _prevClose > _prevUpper && candle.ClosePrice < upper && maValue < _prevMa;
if (bull && Position <= 0 && _cooldown == 0)
{
BuyMarket();
_entryPrice = candle.ClosePrice;
_cooldown = 20;
}
else if (bear && Position >= 0 && _cooldown == 0)
{
SellMarket();
_entryPrice = candle.ClosePrice;
_cooldown = 20;
}
var stop = AtrStopMultiplier * atrValue;
var profit = AtrProfitMultiplier * atrValue;
if (Position > 0)
{
var stopPrice = _entryPrice - stop;
var profitPrice = _entryPrice + profit;
if (candle.LowPrice <= stopPrice || candle.HighPrice >= profitPrice)
{
SellMarket();
_entryPrice = 0m;
_cooldown = 20;
}
}
else if (Position < 0)
{
var stopPrice = _entryPrice + stop;
var profitPrice = _entryPrice - profit;
if (candle.HighPrice >= stopPrice || candle.LowPrice <= profitPrice)
{
BuyMarket();
_entryPrice = 0m;
_cooldown = 20;
}
}
_prevLower = lower;
_prevUpper = upper;
_prevClose = candle.ClosePrice;
_prevMa = maValue;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import BollingerBands, ExponentialMovingAverage, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class bbsr_extreme_strategy(Strategy):
def __init__(self):
super(bbsr_extreme_strategy, self).__init__()
self._bb_period = self.Param("BollingerPeriod", 30) \
.SetGreaterThanZero() \
.SetDisplay("Bollinger Period", "Bollinger Bands length", "Indicators")
self._bb_mult = self.Param("BollingerMultiplier", 2.5) \
.SetGreaterThanZero() \
.SetDisplay("Bollinger Multiplier", "Standard deviation multiplier", "Indicators")
self._ma_length = self.Param("MaLength", 7) \
.SetGreaterThanZero() \
.SetDisplay("MA Length", "Length for moving average", "Indicators")
self._atr_length = self.Param("AtrLength", 14) \
.SetGreaterThanZero() \
.SetDisplay("ATR Length", "ATR indicator period", "Risk Management")
self._atr_stop_mult = self.Param("AtrStopMultiplier", 2.0) \
.SetGreaterThanZero() \
.SetDisplay("ATR Stop Multiplier", "ATR multiplier for stop", "Risk Management")
self._atr_profit_mult = self.Param("AtrProfitMultiplier", 3.0) \
.SetGreaterThanZero() \
.SetDisplay("ATR Profit Multiplier", "ATR multiplier for take profit", "Risk Management")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(60))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._prev_lower = 0.0
self._prev_upper = 0.0
self._prev_close = 0.0
self._prev_ma = 0.0
self._is_initialized = False
self._entry_price = 0.0
self._cooldown = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(bbsr_extreme_strategy, self).OnReseted()
self._prev_lower = 0.0
self._prev_upper = 0.0
self._prev_close = 0.0
self._prev_ma = 0.0
self._is_initialized = False
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(bbsr_extreme_strategy, self).OnStarted2(time)
bb = BollingerBands()
bb.Length = self._bb_period.Value
bb.Width = self._bb_mult.Value
ma = ExponentialMovingAverage()
ma.Length = self._ma_length.Value
atr = AverageTrueRange()
atr.Length = self._atr_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(bb, ma, atr, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, ma)
self.DrawIndicator(area, bb)
self.DrawOwnTrades(area)
def OnProcess(self, candle, bb_value, ma_value, atr_value):
if candle.State != CandleStates.Finished:
return
bb = bb_value
upper = bb.UpBand
lower = bb.LowBand
if upper is None or lower is None:
return
if not ma_value.IsFormed or not atr_value.IsFormed:
return
upper_v = float(upper)
lower_v = float(lower)
ma_v = float(ma_value)
atr_v = float(atr_value)
close = float(candle.ClosePrice)
low = float(candle.LowPrice)
high = float(candle.HighPrice)
if not self._is_initialized:
self._prev_lower = lower_v
self._prev_upper = upper_v
self._prev_close = close
self._prev_ma = ma_v
self._is_initialized = True
return
if self._cooldown > 0:
self._cooldown -= 1
bull = self._prev_close < self._prev_lower and close > lower_v and ma_v > self._prev_ma
bear = self._prev_close > self._prev_upper and close < upper_v and ma_v < self._prev_ma
if bull and self.Position <= 0 and self._cooldown == 0:
self.BuyMarket()
self._entry_price = close
self._cooldown = 20
elif bear and self.Position >= 0 and self._cooldown == 0:
self.SellMarket()
self._entry_price = close
self._cooldown = 20
stop_mult = float(self._atr_stop_mult.Value)
profit_mult = float(self._atr_profit_mult.Value)
stop = stop_mult * atr_v
profit = profit_mult * atr_v
if self.Position > 0:
if low <= self._entry_price - stop or high >= self._entry_price + profit:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 20
elif self.Position < 0:
if high >= self._entry_price + stop or low <= self._entry_price - profit:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 20
self._prev_lower = lower_v
self._prev_upper = upper_v
self._prev_close = close
self._prev_ma = ma_v
def CreateClone(self):
return bbsr_extreme_strategy()