GitHub で見る

Renko ライブチャート戦略

この戦略はクラシックなRenkoブリックチャートをエミュレートし、ブリックの方向変化に基づいて取引します。MetaTraderスクリプト RenkoLiveChart_v600 から変換されました。

ロジック

この戦略は完成した時間ベースのローソク足を使用してRenkoブリックを構築します。価格が最後のブリック価格から選択されたボックスサイズ以上動いた場合、新しいブリックが形成されます。上向きのブリックでロングポジションを開き、下向きのブリックでショートポジションを開きます。

パラメーター

  • Candle Type – ブリック構築に使用する入力ローソク足の時間軸。
  • Brick Size – Renkoブリックの高さを定義する価格ステップ。
  • Brick Offset – 最初のブリックに適用するブリック単位の初期オフセット。
  • Show Wicks – ローソク足を描画する際にチャート上にヒゲを表示。

注意事項

  • 取引は完成したローソク足のみで実行されます。
  • ポジション保護は戦略開始時に自動的に開始されます。
  • この実装はRenkoのコア動作に集中しており、外部ファイル処理などオリジナルスクリプトの高度な機能は無視しています。
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Renko live chart emulation strategy.
/// </summary>
public class RenkoLiveChartStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<decimal> _brickSize;

	private decimal _renkoPrice;

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

	public RenkoLiveChartStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Working candle timeframe", "General");

		_brickSize = Param(nameof(BrickSize), 500m)
			.SetGreaterThanZero()
			.SetDisplay("Brick Size", "Renko brick size", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_renkoPrice = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		SubscribeCandles(CandleType).Bind(ProcessCandle).Start();
	}

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

		var close = candle.ClosePrice;
		var size = BrickSize;

		if (_renkoPrice == 0m)
		{
			_renkoPrice = close;
			return;
		}

		var diff = close - _renkoPrice;
		if (Math.Abs(diff) < size)
			return;

		var direction = Math.Sign(diff);
		_renkoPrice += direction * size;

		if (direction > 0 && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (direction < 0 && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}