Ver no GitHub

Bollinger Williams R Strategy

Strategy based on Bollinger Bands and Williams %R indicators. Enters long when price is at lower band and Williams %R is oversold (< -80) Enters short when price is at upper band and Williams %R is overbought (> -20)

Testing indicates an average annual return of about 103%. It performs best in the stocks market.

Bollinger bands expose volatility breakouts and Williams %R ensures momentum is extreme. Positions open when price closes outside a band with a matching Williams %R reading.

Best for volatility expansion traders. ATR stops handle adverse turns.

Details

  • Entry Criteria:
    • Long: Close < LowerBand && WilliamsR < -80
    • Short: Close > UpperBand && WilliamsR > -20
  • Long/Short: Both
  • Exit Criteria: Price returns to middle band
  • Stops: ATR-based using AtrMultiplier
  • Default Values:
    • BollingerPeriod = 20
    • BollingerDeviation = 2.0m
    • WilliamsRPeriod = 14
    • AtrPeriod = 14
    • AtrMultiplier = 2.0m
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()
  • Filters:
    • Category: Mean reversion
    • Direction: Both
    • Indicators: Bollinger Bands, Williams %R, R
    • Stops: Yes
    • Complexity: Intermediate
    • Timeframe: Mid-term
    • 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 based on Bollinger Bands and Williams %R indicators.
/// Enters long when price is at lower band and Williams %R is oversold (< -80)
/// Enters short when price is at upper band and Williams %R is overbought (> -20)
/// </summary>
public class BollingerWilliamsRStrategy : Strategy
{
	private readonly StrategyParam<int> _bollingerPeriod;
	private readonly StrategyParam<decimal> _bollingerDeviation;
	private readonly StrategyParam<int> _williamsRPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _atrMultiplier;
	private readonly StrategyParam<DataType> _candleType;
	private int _cooldown;
	private bool _wasBelowLower;
	private bool _wasAboveUpper;

	/// <summary>
	/// Bollinger Bands period
	/// </summary>
	public int BollingerPeriod
	{
		get => _bollingerPeriod.Value;
		set => _bollingerPeriod.Value = value;
	}

	/// <summary>
	/// Bollinger Bands deviation
	/// </summary>
	public decimal BollingerDeviation
	{
		get => _bollingerDeviation.Value;
		set => _bollingerDeviation.Value = value;
	}

	/// <summary>
	/// Williams %R period
	/// </summary>
	public int WilliamsRPeriod
	{
		get => _williamsRPeriod.Value;
		set => _williamsRPeriod.Value = value;
	}
	
	/// <summary>
	/// ATR period for stop-loss calculation
	/// </summary>
	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}
	
	/// <summary>
	/// ATR multiplier for stop-loss
	/// </summary>
	public decimal AtrMultiplier
	{
		get => _atrMultiplier.Value;
		set => _atrMultiplier.Value = value;
	}

	/// <summary>
	/// Candle type for strategy calculation
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor
	/// </summary>
	public BollingerWilliamsRStrategy()
	{
		_bollingerPeriod = Param(nameof(BollingerPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Bollinger Period", "Period for Bollinger Bands", "Indicators")
			
			.SetOptimize(15, 30, 5);

		_bollingerDeviation = Param(nameof(BollingerDeviation), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("Bollinger Deviation", "Deviation multiplier for Bollinger Bands", "Indicators")
			
			.SetOptimize(1.5m, 2.5m, 0.5m);

		_williamsRPeriod = Param(nameof(WilliamsRPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Williams %R Period", "Period for Williams %R indicator", "Indicators")
			
			.SetOptimize(10, 20, 2);
			
		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Period for ATR indicator for stop-loss", "Risk Management")
			
			.SetOptimize(10, 20, 2);
			
		_atrMultiplier = Param(nameof(AtrMultiplier), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("ATR Multiplier", "Multiplier for ATR-based stop-loss", "Risk Management")
			
			.SetOptimize(1.5m, 3.0m, 0.5m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for strategy", "General");
	}

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

		/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_cooldown = 0;
		_wasBelowLower = false;
		_wasAboveUpper = false;
	}

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

		// Create indicators
		var bollinger = new BollingerBands
		{
			Length = BollingerPeriod,
			Width = BollingerDeviation
		};

		var williamsR = new WilliamsR { Length = WilliamsRPeriod };
		
		var atr = new AverageTrueRange { Length = AtrPeriod };

		// Subscribe to candles and bind indicators
		var subscription = SubscribeCandles(CandleType);
		
		subscription
			.BindEx(bollinger, williamsR, atr, ProcessCandle)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, bollinger);
			
			// Create a separate area for Williams %R
			var williamsArea = CreateChartArea();
			if (williamsArea != null)
			{
				DrawIndicator(williamsArea, williamsR);
			}
			
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue bollingerValue, IIndicatorValue williamsRValue, IIndicatorValue atrValue)
	{
		// Skip unfinished candles
		if (candle.State != CandleStates.Finished)
			return;
		
		// Check if strategy is ready to trade
		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		// Get additional values from Bollinger Bands
		var bollingerTyped = (BollingerBandsValue)bollingerValue;

		var middleBand = bollingerTyped.MovingAverage; // Middle band is returned by default
		var upperBand = bollingerTyped.UpBand;
		var lowerBand = bollingerTyped.LowBand;
		
		// Current price (close of the candle)
		var price = candle.ClosePrice;

		// Stop-loss size based on ATR
		var stopSize = atrValue.ToDecimal() * AtrMultiplier;

		var williamsRValueDec = williamsRValue.ToDecimal();
		var isBelowLower = price <= lowerBand * 1.001m;
		var isAboveUpper = price >= upperBand * 0.999m;

		if (_cooldown > 0)
		{
			_cooldown--;
			_wasBelowLower = isBelowLower;
			_wasAboveUpper = isAboveUpper;
			return;
		}

		// Trading logic
		if (!_wasBelowLower && isBelowLower && williamsRValueDec < -45 && Position <= 0)
		{
			// Buy signal: price at/below lower band and Williams %R oversold
			BuyMarket(Volume + Math.Abs(Position));
			_cooldown = 6;
			
			// Set stop-loss
			var stopPrice = price - stopSize;
			RegisterOrder(CreateOrder(Sides.Sell, stopPrice, Math.Abs(Position + Volume).Max(Volume)));
		}
		else if (!_wasAboveUpper && isAboveUpper && williamsRValueDec > -55 && Position >= 0)
		{
			// Sell signal: price at/above upper band and Williams %R overbought
			SellMarket(Volume + Math.Abs(Position));
			_cooldown = 6;
			
			// Set stop-loss
			var stopPrice = price + stopSize;
			RegisterOrder(CreateOrder(Sides.Buy, stopPrice, Math.Abs(Position + Volume).Max(Volume)));
		}
		// Exit conditions
		else if (price >= middleBand && Position < 0)
		{
			// Exit short position when price returns to middle band
			BuyMarket(Math.Abs(Position));
		}
		else if (price <= middleBand && Position > 0)
		{
			// Exit long position when price returns to middle band
			SellMarket(Position);
		}

		_wasBelowLower = isBelowLower;
		_wasAboveUpper = isAboveUpper;
	}
}