GitHub で見る

ライブRSI戦略

複数のRSI計算(close、weighted、typical、median、open)とParabolic SARを使用してトレンドの反転を検出します。RSI値が強気の順序に並び価格がSARを上回るときにロングに入り、並びが弱気で価格がSARを下回るときにショートに入ります。SAR値はトレーリングストップとして機能します。

詳細

  • エントリー条件:
    • RSIシーケンスが強気でかつ価格がSARを上回るときにロング。
    • RSIシーケンスが弱気でかつ価格がSARを下回るときにショート。
  • ロング/ショート: 両方。
  • エグジット条件:
    • 反対のトレンドシグナルまたはSARトレーリングストップ。
  • ストップ: オプションの固定ストップロスとSARベースのトレーリングストップ。
  • デフォルト値:
    • RSI Period = 30
    • SAR Step = 0.08
    • Stop Loss = 40
    • Check Hour = false
    • Start Hour = 17
    • End Hour = 1
    • Candle Type = 1時間
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: ロング & ショート
    • インジケーター: RSI, Parabolic SAR
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: オプション(時間フィルター)
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// RSI trend reversal strategy with Parabolic SAR confirmation.
/// Buys when RSI crosses above 50 and SAR is below price.
/// Sells when RSI crosses below 50 and SAR is above price.
/// </summary>
public class LiveRSIStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _hasPrev;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public LiveRSIStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Period for RSI", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0;
		_hasPrev = false;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var sar = new ParabolicSar();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(rsi, sar, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevRsi = rsi;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		// RSI cross above 50 + SAR below price -> buy
		if (_prevRsi <= 50 && rsi > 50 && sar < close)
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
				BuyMarket();
		}
		// RSI cross below 50 + SAR above price -> sell
		else if (_prevRsi >= 50 && rsi < 50 && sar > close)
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
				SellMarket();
		}

		_prevRsi = rsi;
	}
}