GitHub で見る

マルチ時間軸 RSI グリッド戦略(矢印付き)

この戦略は、現在の時間軸と 2 つの上位時間軸の RSI が買われすぎまたは売られすぎのレベルに達したときにトレードします。すべての RSI が一致したときに最初のポジションを建て、その後 ATR ベースのグリッドとロット乗数の増加を使って追加ポジションを積み増します。戦略は日次利益率を目標とし、毎日リセットし、逆シグナルまたはドローダウンで決済します。

パラメーター

  • ローソク足タイプ
  • RSI 長さ
  • 売られすぎレベル
  • 買われすぎレベル
  • 上位時間軸 1
  • 上位時間軸 2
  • グリッド乗数ファクター
  • ロット乗数ファクター
  • 最大グリッドレベル数
  • 日次利益目標 %
  • ATR 長さ
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

public class MultiTimeframeRsiGridWithArrowsStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<DataType> _candleType;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MultiTimeframeRsiGridWithArrowsStrategy()
	{
		_fastLength = Param(nameof(FastLength), 10).SetGreaterThanZero();
		_slowLength = Param(nameof(SlowLength), 30).SetGreaterThanZero();
		_rsiLength = Param(nameof(RsiLength), 14).SetGreaterThanZero();
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame());
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		var fast = new ExponentialMovingAverage { Length = FastLength };
		var slow = new ExponentialMovingAverage { Length = SlowLength };
		var rsi = new RelativeStrengthIndex { Length = RsiLength };
		var prevFast = 0m; var prevSlow = 0m; var init = false;
		var sub = SubscribeCandles(CandleType);
		sub.Bind(fast, slow, rsi, (c, f, s, r) =>
		{
			if (c.State != CandleStates.Finished || !fast.IsFormed || !slow.IsFormed || !rsi.IsFormed) return;
			if (!init) { prevFast = f; prevSlow = s; init = true; return; }
			if (prevFast <= prevSlow && f > s && r > 45 && Position <= 0) BuyMarket();
			else if (prevFast >= prevSlow && f < s && r < 55 && Position > 0) SellMarket();
			prevFast = f; prevSlow = s;
		}).Start();
		var area = CreateChartArea();
		if (area != null) { DrawCandles(area, sub); DrawIndicator(area, fast); DrawIndicator(area, slow); DrawOwnTrades(area); }
	}
}