Ver en GitHub

Estrategia Adaptive Renko

Esta estrategia construye una cuadrícula Renko adaptativa donde el tamaño del ladrillo sigue la volatilidad del mercado medida por el indicador Average True Range (ATR). Se ejecuta una operación cada vez que el precio recorre un ladrillo completo en cualquier dirección.

Lógica

  • El ATR se calcula sobre un VolatilityPeriod configurable.
  • El tamaño del ladrillo es igual a ATR * Multiplier pero no puede ser menor que MinBrickSize.
  • Cuando el precio sube por encima del ladrillo anterior al menos un tamaño de ladrillo, la estrategia compra (cerrando posiciones cortas si es necesario).
  • Cuando el precio cae por debajo del ladrillo anterior al menos un tamaño de ladrillo, la estrategia vende (cerrando posiciones largas si es necesario).

Parámetros

  • Volume – volumen de la orden.
  • VolatilityPeriod – período utilizado para el ATR.
  • Multiplier – coeficiente aplicado al ATR.
  • MinBrickSize – tamaño mínimo permitido del ladrillo en unidades de precio.
  • CandleType – marco temporal para el cálculo del ATR.

Marco temporal

  • Predeterminado: 4 horas.
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;
		}
	}
}