GitHub で見る

Double RSI 戦略

Double RSI は、2つのRelative Strength Index計算を使用します:1つはトレードチャート上、 もう1つは上位時間軸上です。両方のRSI読みが同じ方向を支持する場合のみ取引が行われ、 短期エントリーを長期のモメンタムと合わせます。

メインの時間軸は、RSIが買われすぎゾーンや売られすぎゾーンからクロスするのを探します。 上位時間軸のRSIがその動きを確認すると、戦略はポジションを開きます。オプションの 利確設定で、事前に定義された動き後に利益を確定することができます。

詳細

  • データ: 2つの時間軸の価格ローソク足。
  • エントリー条件:
    • ロング: 下位時間軸のRSIが売られすぎゾーンを抜け、かつ上位時間軸のRSIが強気。
    • ショート: 下位時間軸のRSIが買われすぎゾーンを抜け、かつ上位時間軸のRSIが弱気。
  • エグジット条件: 反対方向のRSIシグナル、または UseTP が真の場合は利確。
  • ストップ: デフォルトではなし。
  • デフォルト値:
    • CandleType = tf(5)
    • RSILength = 14
    • MTFTimeframe = tf(15)
    • UseTP = False
  • フィルター:
    • カテゴリ: モメンタム
    • 方向: ロング/ショート
    • インジケーター: RSI (multi‑timeframe)
    • 複雑さ: 中程度
    • リスクレベル: 中
using System;
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;

/// <summary>
/// Double RSI Strategy.
/// Uses a short RSI and a long RSI for confirmation.
/// Buys when both RSIs are oversold and sells when both are overbought.
/// </summary>
public class DoubleRsiStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleTypeParam;
	private readonly StrategyParam<int> _rsiShortLength;
	private readonly StrategyParam<int> _rsiLongLength;
	private readonly StrategyParam<decimal> _oversold;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<int> _cooldownBars;

	private RelativeStrengthIndex _rsiShort;
	private RelativeStrengthIndex _rsiLong;
	private int _cooldownRemaining;

	public DoubleRsiStrategy()
	{
		_candleTypeParam = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle type", "Candle type for strategy calculation.", "General");

		_rsiShortLength = Param(nameof(RSIShortLength), 7)
			.SetGreaterThanZero()
			.SetDisplay("Short RSI", "Short RSI period", "RSI");

		_rsiLongLength = Param(nameof(RSILongLength), 21)
			.SetGreaterThanZero()
			.SetDisplay("Long RSI", "Long RSI period", "RSI");

		_oversold = Param(nameof(Oversold), 35m)
			.SetDisplay("Oversold", "RSI oversold level", "RSI");

		_overbought = Param(nameof(Overbought), 65m)
			.SetDisplay("Overbought", "RSI overbought level", "RSI");

		_cooldownBars = Param(nameof(CooldownBars), 10)
			.SetDisplay("Cooldown Bars", "Bars to wait between trades", "Risk");
	}

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

	public int RSIShortLength
	{
		get => _rsiShortLength.Value;
		set => _rsiShortLength.Value = value;
	}

	public int RSILongLength
	{
		get => _rsiLongLength.Value;
		set => _rsiLongLength.Value = value;
	}

	public decimal Oversold
	{
		get => _oversold.Value;
		set => _oversold.Value = value;
	}

	public decimal Overbought
	{
		get => _overbought.Value;
		set => _overbought.Value = value;
	}

	public int CooldownBars
	{
		get => _cooldownBars.Value;
		set => _cooldownBars.Value = value;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_rsiShort = null;
		_rsiLong = null;
		_cooldownRemaining = 0;
	}

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

		_rsiShort = new RelativeStrengthIndex { Length = RSIShortLength };
		_rsiLong = new RelativeStrengthIndex { Length = RSILongLength };

		var subscription = SubscribeCandles(CandleType);

		subscription
			.Bind(_rsiShort, _rsiLong, OnProcess)
			.Start();

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

	private void OnProcess(ICandleMessage candle, decimal rsiShort, decimal rsiLong)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_rsiShort.IsFormed || !_rsiLong.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_cooldownRemaining > 0)
		{
			_cooldownRemaining--;
			return;
		}

		// Buy: both RSIs oversold
		if (rsiShort < Oversold && rsiLong < Oversold && Position <= 0)
		{
			if (Position < 0)
				BuyMarket(Math.Abs(Position));
			BuyMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		// Sell: both RSIs overbought
		else if (rsiShort > Overbought && rsiLong > Overbought && Position >= 0)
		{
			if (Position > 0)
				SellMarket(Math.Abs(Position));
			SellMarket(Volume);
			_cooldownRemaining = CooldownBars;
		}
		// Exit long: short RSI overbought
		else if (Position > 0 && rsiShort > Overbought)
		{
			SellMarket(Math.Abs(Position));
			_cooldownRemaining = CooldownBars;
		}
		// Exit short: short RSI oversold
		else if (Position < 0 && rsiShort < Oversold)
		{
			BuyMarket(Math.Abs(Position));
			_cooldownRemaining = CooldownBars;
		}
	}
}