This strategy uses Bollinger Bands built on a simple moving average. A long position opens when the price closes above the upper band, and a short position opens when it closes below the lower band.
Details
Entry Criteria:
Long: Close price crosses above the upper band.
Short: Close price crosses below the lower band.
Long/Short: Both sides.
Exit Criteria:
Opposite signal.
Stops: None.
Default Values:
Length = 50
Multiplier = 2
Filters:
Category: Trend following
Direction: Both
Indicators: Bollinger Bands
Stops: No
Complexity: Low
Timeframe: Any
Seasonality: No
Neural networks: No
Divergence: No
Risk level: Medium
using System;
using System.Linq;
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;
public class MarkdownThePineEditorsHiddenGemStrategy : Strategy
{
private readonly StrategyParam<int> _length;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private BollingerBands _bollinger;
public int Length { get => _length.Value; set => _length.Value = value; }
public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public MarkdownThePineEditorsHiddenGemStrategy()
{
_length = Param(nameof(Length), 50);
_multiplier = Param(nameof(Multiplier), 2m);
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_bollinger = new BollingerBands
{
Length = Length,
Width = Multiplier
};
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(_bollinger, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue bollingerValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!_bollinger.IsFormed)
return;
var bb = (BollingerBandsValue)bollingerValue;
if (bb.UpBand is not decimal upper || bb.LowBand is not decimal lower)
return;
if (Position <= 0 && candle.ClosePrice > upper)
{
BuyMarket();
}
else if (Position >= 0 && candle.ClosePrice < lower)
{
SellMarket();
}
}
}
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 markdown_the_pine_editors_hidden_gem_strategy(Strategy):
"""
Bollinger Bands breakout: buy above upper band, sell below lower band.
"""
def __init__(self):
super(markdown_the_pine_editors_hidden_gem_strategy, self).__init__()
self._length = self.Param("Length", 50).SetDisplay("Length", "BB period", "Indicators")
self._multiplier = self.Param("Multiplier", 2.0).SetDisplay("Multiplier", "BB width", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Candles", "General")
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(markdown_the_pine_editors_hidden_gem_strategy, self).OnReseted()
def OnStarted2(self, time):
super(markdown_the_pine_editors_hidden_gem_strategy, self).OnStarted2(time)
self._bb = BollingerBands()
self._bb.Length = self._length.Value
self._bb.Width = self._multiplier.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(self._bb, self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, self._bb)
self.DrawOwnTrades(area)
def _process_candle(self, candle, bb_value):
if candle.State != CandleStates.Finished:
return
if not self._bb.IsFormed:
return
upper = bb_value.UpBand
lower = bb_value.LowBand
if upper is None or lower is None:
return
upper_f = float(upper)
lower_f = float(lower)
close = float(candle.ClosePrice)
if self.Position <= 0 and close > upper_f:
self.BuyMarket()
elif self.Position >= 0 and close < lower_f:
self.SellMarket()
def CreateClone(self):
return markdown_the_pine_editors_hidden_gem_strategy()