View on GitHub

RSI Trend Strategy

The RSI Trend Strategy uses the Relative Strength Index (RSI) to detect trend reversals and manages positions with an ATR-based trailing stop. The system opens a long position when the RSI crosses above an overbought threshold and enters a short position when the RSI falls below an oversold threshold. Risk is controlled using a trailing stop derived from the Average True Range (ATR), allowing the stop level to adapt to current volatility.

This implementation is designed for educational purposes and demonstrates how to build a high-level StockSharp strategy using indicator bindings. The strategy trades on completed candles only and does not reference previous indicator values directly, aligning with StockSharp best practices.

Details

  • Entry Criteria:
    • Long: RSI(t) > BuyLevel and RSI(t-1) <= BuyLevel.
    • Short: RSI(t) < SellLevel and RSI(t-1) >= SellLevel.
  • Long/Short: Both directions.
  • Exit Criteria:
    • Trailing stop based on ATR multiple.
  • Stops: Yes, dynamic trailing stop.
  • Default Values:
    • RSI Period = 14.
    • BuyLevel = 73.
    • SellLevel = 27.
    • ATR Period = 100.
    • ATR Multiple = 3.
  • Filters:
    • Category: Trend following.
    • Direction: Both.
    • Indicators: RSI, ATR.
    • Stops: Yes.
    • Complexity: Medium.
    • Timeframe: Any (default 1 minute candles).
    • Seasonality: No.
    • Neural networks: No.
    • Divergence: No.
    • Risk level: Moderate.
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 strategy with StdDev trailing stop.
/// </summary>
public class RsiTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _rsiBuyLevel;
	private readonly StrategyParam<decimal> _rsiSellLevel;
	private readonly StrategyParam<int> _stdevPeriod;
	private readonly StrategyParam<decimal> _stdevMultiple;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _previousRsi;
	private bool _isRsiInitialized;
	private decimal _stopPrice;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal RsiBuyLevel { get => _rsiBuyLevel.Value; set => _rsiBuyLevel.Value = value; }
	public decimal RsiSellLevel { get => _rsiSellLevel.Value; set => _rsiSellLevel.Value = value; }
	public int StdevPeriod { get => _stdevPeriod.Value; set => _stdevPeriod.Value = value; }
	public decimal StdevMultiple { get => _stdevMultiple.Value; set => _stdevMultiple.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public RsiTrendStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Period for RSI calculation", "RSI Settings");

		_rsiBuyLevel = Param(nameof(RsiBuyLevel), 60m)
			.SetDisplay("RSI Buy Level", "Upper RSI barrier for long entries", "RSI Settings");

		_rsiSellLevel = Param(nameof(RsiSellLevel), 40m)
			.SetDisplay("RSI Sell Level", "Lower RSI barrier for short entries", "RSI Settings");

		_stdevPeriod = Param(nameof(StdevPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Period", "StdDev period for trailing stop", "Settings");

		_stdevMultiple = Param(nameof(StdevMultiple), 2m)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Multiple", "StdDev multiplier for trailing stop", "Settings");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_previousRsi = 0;
		_isRsiInitialized = false;
		_stopPrice = 0;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		var stdev = new StandardDeviation { Length = StdevPeriod };

		SubscribeCandles(CandleType)
			.Bind(rsi, stdev, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal rsiValue, decimal stdevValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (stdevValue <= 0) return;

		if (!_isRsiInitialized)
		{
			_previousRsi = rsiValue;
			_isRsiInitialized = true;
			return;
		}

		var bullish = rsiValue > RsiBuyLevel && _previousRsi <= RsiBuyLevel;
		var bearish = rsiValue < RsiSellLevel && _previousRsi >= RsiSellLevel;

		if (bullish && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
			_stopPrice = candle.ClosePrice - stdevValue * StdevMultiple;
		}
		else if (bearish && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
			_stopPrice = candle.ClosePrice + stdevValue * StdevMultiple;
		}

		if (Position > 0)
		{
			var newStop = candle.ClosePrice - stdevValue * StdevMultiple;
			if (newStop > _stopPrice) _stopPrice = newStop;
			if (candle.ClosePrice <= _stopPrice) SellMarket();
		}
		else if (Position < 0)
		{
			var newStop = candle.ClosePrice + stdevValue * StdevMultiple;
			if (newStop < _stopPrice) _stopPrice = newStop;
			if (candle.ClosePrice >= _stopPrice) BuyMarket();
		}

		_previousRsi = rsiValue;
	}
}