GitHub で見る

Adaptive Renko戦略

この戦略は、**Average True Range(ATR)**インジケーターで測定される市場のボラティリティにレンガサイズが追従するアダプティブRenkoグリッドを構築します。価格がどちらかの方向に1レンガ分移動するたびにトレードが実行されます。

ロジック

  • ATRは設定可能なVolatilityPeriodで計算されます。
  • レンガサイズはATR * Multiplierに等しいですが、MinBrickSize未満にはなりません。
  • 価格が前のレンガを少なくとも1レンガ分上回ると、戦略は買い(必要に応じてショートポジションを決済)ます。
  • 価格が前のレンガを少なくとも1レンガ分下回ると、戦略は売り(必要に応じてロングポジションを決済)ます。

パラメーター

  • Volume – 注文数量。
  • VolatilityPeriod – ATR計算に使用する期間。
  • Multiplier – ATRに適用する係数。
  • MinBrickSize – 価格単位での最小レンガサイズ。
  • CandleType – ATR計算に使用する時間軸。

時間軸

  • デフォルト:4時間。
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;
		}
	}
}