View on GitHub

cm RSI Strategy

Overview

This strategy is a direct port of the MetaTrader 4 expert "cm_RSI". It uses the Relative Strength Index (RSI) indicator to catch momentum reversals.

The algorithm monitors RSI values calculated from candle open prices. A long position is opened when RSI rises above a configurable buy level after being below it. A short position is opened when RSI falls below a configurable sell level after being above it. Each trade is protected by fixed take profit and stop loss values expressed in price points.

Strategy Logic

  1. Calculate the RSI with a user defined period using candle open prices.
  2. If the previous RSI value was below the buy level and the current value crosses above it, open a long market position.
  3. If the previous RSI value was above the sell level and the current value crosses below it, open a short market position.
  4. Each trade uses the same configurable volume and is protected by both stop loss and take profit orders.

Parameters

Name Description
RsiPeriod RSI calculation period.
BuyLevel RSI level used to trigger long entries.
SellLevel RSI level used to trigger short entries.
TakeProfit Take profit in absolute price points.
StopLoss Stop loss in absolute price points.
OrderVolume Volume applied to every trade.
CandleType Type of candles used for calculations.

Notes

  • The strategy processes only finished candles.
  • It holds a single open position at any time.
  • StartProtection is used to automatically manage stop loss and take profit orders.
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>
/// Strategy based on RSI cross signals from the original cm_RSI expert.
/// </summary>
public class CmRsiStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _buyLevel;
	private readonly StrategyParam<decimal> _sellLevel;
	private readonly StrategyParam<int> _takeProfit;
	private readonly StrategyParam<int> _stopLoss;
	private readonly StrategyParam<decimal> _orderVolume;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _isFirst = true;

	/// <summary>
	/// RSI calculation period.
	/// </summary>
	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }

	/// <summary>
	/// RSI level to trigger long entries.
	/// </summary>
	public decimal BuyLevel { get => _buyLevel.Value; set => _buyLevel.Value = value; }

	/// <summary>
	/// RSI level to trigger short entries.
	/// </summary>
	public decimal SellLevel { get => _sellLevel.Value; set => _sellLevel.Value = value; }

	/// <summary>
	/// Take profit in price points.
	/// </summary>
	public int TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }

	/// <summary>
	/// Stop loss in price points.
	/// </summary>
	public int StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }

	/// <summary>
	/// Volume applied to each trade.
	/// </summary>
	public decimal OrderVolume { get => _orderVolume.Value; set => _orderVolume.Value = value; }

	/// <summary>
	/// Candle type for calculations.
	/// </summary>
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	/// <summary>
	/// Initialize <see cref="CmRsiStrategy"/>.
	/// </summary>
	public CmRsiStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation period", "Indicators")
			
			.SetOptimize(7, 21, 1);

		_buyLevel = Param(nameof(BuyLevel), 30m)
			.SetDisplay("Buy Level", "RSI level to enter long", "Indicators")
			
			.SetOptimize(10m, 40m, 5m);

		_sellLevel = Param(nameof(SellLevel), 70m)
			.SetDisplay("Sell Level", "RSI level to enter short", "Indicators")
			
			.SetOptimize(60m, 90m, 5m);

		_takeProfit = Param(nameof(TakeProfit), 200)
			.SetDisplay("Take Profit", "Take profit in price points", "Risk Management")
			
			.SetOptimize(100, 400, 50);

		_stopLoss = Param(nameof(StopLoss), 100)
			.SetDisplay("Stop Loss", "Stop loss in price points", "Risk Management")
			
			.SetOptimize(50, 200, 50);

		_orderVolume = Param(nameof(OrderVolume), 0.1m)
			.SetGreaterThanZero()
			.SetDisplay("Order Volume", "Volume of each trade", "General")
			
			.SetOptimize(0.1m, 1m, 0.1m);

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevRsi = 0;
		_isFirst = true;
	}

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

		_isFirst = true;
		_prevRsi = 0;

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevRsi = rsiValue;
			_isFirst = false;
			return;
		}

		if (_isFirst)
		{
			_prevRsi = rsiValue;
			_isFirst = false;
			return;
		}

		// Open long when RSI crosses above buy level
		if (_prevRsi < BuyLevel && rsiValue > BuyLevel && Position <= 0)
			BuyMarket();

		// Open short when RSI crosses below sell level
		if (_prevRsi > SellLevel && rsiValue < SellLevel && Position >= 0)
			SellMarket();

		_prevRsi = rsiValue;
	}
}