This sample converts the original MQL "Bb_0_1" expert advisor into the StockSharp high level API. The strategy listens to one candle subscription and uses Bollinger Bands to bracket the current price. When the market sits between the upper and lower bands, the algorithm places three layered buy stop orders above price and three layered sell stop orders below price. Each layer is configured with individual take-profit distances while sharing the same stop reference taken from the opposite band.
Trading logic
Subscribe to the configured timeframe and calculate Bollinger Bands with the requested period and deviation.
Inside the trading window (StartHour < hour < EndHour) and while the price remains between the bands, place pending orders:
Three buy stops at the current upper band level with take-profits displaced by FirstTakeProfit, SecondTakeProfit, and ThirdTakeProfit price steps above the entry.
Three sell stops at the current lower band level with mirrored take-profits below the entry.
All entries inherit the opposite band as their initial protective stop.
Pending orders are automatically re-registered whenever the bands move closer to price so that the orders follow the indicator envelopes.
Once a stop order executes, the strategy registers explicit stop-loss and take-profit orders for the filled volume.
Trailing protection is optional: UseBandTrailingStop selects the opposite band for trailing, otherwise the middle band (EMA) is used. Stops only trail when the close moves beyond the entry price and the indicator value provides a better level.
Parameters
Name
Description
CandleType
Time frame used for the Bollinger Band calculations.
Take-profit distances expressed in price steps for every layer.
UseBandTrailingStop
Select the trailing reference: opposite band (true) or Bollinger middle line (false).
Implementation notes
Order volume mirrors the original expert advisor by using a static size (Volume). Risk-based position sizing from the MQL code is not implemented because the StockSharp sample environment does not provide account history.
Indicator shift parameters from the MQL script are not exposed because the high level API already delivers aligned values for the current candle.
Protective orders are normal stop and limit orders that are refreshed whenever the band-based trailing conditions improve the stop level.
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 Band breakout strategy.
/// Buys when price closes above upper band, sells when price closes below lower band.
/// Exits at middle band.
/// </summary>
public class BollingerBandPendingStopsStrategy : Strategy
{
private readonly StrategyParam<int> _bandPeriod;
private readonly StrategyParam<decimal> _bandWidth;
private readonly StrategyParam<DataType> _candleType;
public int BandPeriod { get => _bandPeriod.Value; set => _bandPeriod.Value = value; }
public decimal BandWidth { get => _bandWidth.Value; set => _bandWidth.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public BollingerBandPendingStopsStrategy()
{
_bandPeriod = Param(nameof(BandPeriod), 20)
.SetDisplay("Band Period", "Bollinger bands period", "Indicators");
_bandWidth = Param(nameof(BandWidth), 1m)
.SetDisplay("Band Width", "Bollinger bands deviation", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
protected override void OnReseted() { base.OnReseted(); }
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var bb = new BollingerBands { Length = BandPeriod, Width = BandWidth };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(bb, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue value)
{
if (candle.State != CandleStates.Finished)
return;
if (!value.IsFinal || value.IsEmpty)
return;
var bbVal = value.IsEmpty ? null : value as BollingerBandsValue;
if (bbVal == null)
return;
var upper = bbVal.UpBand;
var lower = bbVal.LowBand;
var middle = bbVal.MovingAverage;
if (upper == null || lower == null || middle == null)
return;
var close = candle.ClosePrice;
// Breakout above upper band - buy
if (close > upper.Value && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
// Breakout below lower band - sell
else if (close < lower.Value && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
// Exit at middle band
else if (Position > 0 && close < middle.Value)
{
SellMarket();
}
else if (Position < 0 && close > middle.Value)
{
BuyMarket();
}
}
}
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
from StockSharp.Algo.Strategies import Strategy
class bollinger_band_pending_stops_strategy(Strategy):
def __init__(self):
super(bollinger_band_pending_stops_strategy, self).__init__()
self._band_period = self.Param("BandPeriod", 20) \
.SetDisplay("Band Period", "Bollinger bands period", "Indicators")
self._band_width = self.Param("BandWidth", 1.0) \
.SetDisplay("Band Width", "Bollinger bands deviation", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
@property
def band_period(self):
return self._band_period.Value
@property
def band_width(self):
return self._band_width.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(bollinger_band_pending_stops_strategy, self).OnReseted()
def OnStarted2(self, time):
super(bollinger_band_pending_stops_strategy, self).OnStarted2(time)
bb = BollingerBands()
bb.Length = self.band_period
bb.Width = self.band_width
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(bb, self.process_candle).Start()
def process_candle(self, candle, bb_value):
if candle.State != CandleStates.Finished:
return
if not bb_value.IsFinal or bb_value.IsEmpty:
return
if bb_value.UpBand is None or bb_value.LowBand is None or bb_value.MovingAverage is None:
return
upper = float(bb_value.UpBand)
lower = float(bb_value.LowBand)
middle = float(bb_value.MovingAverage)
close = float(candle.ClosePrice)
if close > upper and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif close < lower and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
elif self.Position > 0 and close < middle:
self.SellMarket()
elif self.Position < 0 and close > middle:
self.BuyMarket()
def CreateClone(self):
return bollinger_band_pending_stops_strategy()