Ver no GitHub

Adaptive Renko Strategy

This strategy builds an adaptive Renko grid where the brick size follows market volatility measured by the Average True Range (ATR) indicator. A trade is executed whenever price travels a full brick in either direction.

Logic

  • ATR is calculated over a configurable VolatilityPeriod.
  • The brick size equals ATR * Multiplier but cannot be less than MinBrickSize.
  • When price rises above the previous brick by at least one brick size, the strategy buys (closing short positions if needed).
  • When price falls below the previous brick by at least one brick size, the strategy sells (closing long positions if needed).

Parameters

  • Volume – order volume.
  • VolatilityPeriod – period used for ATR.
  • Multiplier – coefficient applied to ATR.
  • MinBrickSize – minimal allowed brick size in price units.
  • CandleType – timeframe for ATR calculation.

Timeframe

  • Default: 4 hours.
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 that trades on adaptive renko movements based on ATR volatility.
/// </summary>
public class AdaptiveRenkoStrategy : Strategy
{
	private readonly StrategyParam<int> _volatilityPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<decimal> _minBrick;
	private readonly StrategyParam<DataType> _candleType;

	private readonly AverageTrueRange _atr = new();

	private decimal _lastBrickPrice;
	private bool _hasBrick;


	public int VolatilityPeriod
	{
		get => _volatilityPeriod.Value;
		set => _volatilityPeriod.Value = value;
	}

	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

	public decimal MinBrickSize
	{
		get => _minBrick.Value;
		set => _minBrick.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public AdaptiveRenkoStrategy()
	{

		_volatilityPeriod = Param(nameof(VolatilityPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Volatility Period", "ATR calculation period", "Indicator")
			
			.SetOptimize(5, 20, 1);

		_multiplier = Param(nameof(Multiplier), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "ATR multiplier", "Indicator")
			
			.SetOptimize(0.5m, 2m, 0.5m);

		_minBrick = Param(nameof(MinBrickSize), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Min Brick", "Minimum brick size", "Indicator")
			
			.SetOptimize(1m, 5m, 1m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Time frame for ATR calculation", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_atr.Length = VolatilityPeriod;
		_atr.Reset();
		_lastBrickPrice = 0m;
		_hasBrick = false;
	}

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

		_atr.Length = VolatilityPeriod;

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_atr, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
		}

		StartProtection(null, null);
	}

	private void ProcessCandle(ICandleMessage candle, decimal atr)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var brick = Math.Max(atr * Multiplier, MinBrickSize);

		if (!_hasBrick)
		{
			_lastBrickPrice = candle.ClosePrice;
			_hasBrick = true;
			return;
		}

		var diff = candle.ClosePrice - _lastBrickPrice;

		if (diff >= brick)
		{
			if (Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}

			_lastBrickPrice = candle.ClosePrice;
		}
		else if (diff <= -brick)
		{
			if (Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}

			_lastBrickPrice = candle.ClosePrice;
		}
	}
}