Ver en GitHub

RSI Experto

Descripción general

La estrategia RSI Experto opera usando el Índice de Fuerza Relativa (RSI). Espera a que el valor del RSI cruce niveles predefinidos de sobrecompra o sobreventa y entra en posiciones en la dirección del cruce.

Lógica

  • Calcular el RSI para cada vela.
  • Cuando el RSI cruza por encima del nivel de sobreventa, se abre una posición larga.
  • Cuando el RSI cruza por debajo del nivel de sobrecompra, se abre una posición corta.
  • Antes de entrar en una nueva posición se cierra la opuesta.
  • Se pueden activar protecciones opcionales de take‑profit, stop‑loss y trailing stop.

La estrategia procesa solo velas terminadas y usa la API de alto nivel de StockSharp con vinculación de indicadores.

Parámetros

Nombre Descripción Por defecto
RsiPeriod Período de cálculo del RSI. 14
LevelUp Nivel de sobrecompra para activar cortos. 70
LevelDown Nivel de sobreventa para activar largos. 30
TakeProfitPercent Porcentaje de take profit. 0 desactiva. 0
StopLossPercent Porcentaje de stop loss. 0 desactiva. 0
TrailingStopPercent Porcentaje de trailing stop. 0 desactiva. 0
CandleType Marco temporal de las velas para cálculos. 1 minuto

Notas

El trailing stop usa el mecanismo integrado StartProtection. Cuando TrailingStopPercent es mayor que cero reemplaza al stop loss regular y sigue automáticamente al precio.

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 based strategy with overbought and oversold level cross signals.
/// Opens long when RSI crosses above oversold level, short when crosses below overbought level.
/// </summary>
public class RsiExpertStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<decimal> _levelUp;
	private readonly StrategyParam<decimal> _levelDown;
	private readonly StrategyParam<decimal> _takeProfitPercent;
	private readonly StrategyParam<decimal> _stopLossPercent;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public decimal LevelUp { get => _levelUp.Value; set => _levelUp.Value = value; }
	public decimal LevelDown { get => _levelDown.Value; set => _levelDown.Value = value; }
	public decimal TakeProfitPercent { get => _takeProfitPercent.Value; set => _takeProfitPercent.Value = value; }
	public decimal StopLossPercent { get => _stopLossPercent.Value; set => _stopLossPercent.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public RsiExpertStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Length of the RSI indicator", "Indicators");

		_levelUp = Param(nameof(LevelUp), 70m)
			.SetDisplay("RSI Overbought", "Upper RSI level triggering a short", "Indicators");

		_levelDown = Param(nameof(LevelDown), 30m)
			.SetDisplay("RSI Oversold", "Lower RSI level triggering a long", "Indicators");

		_takeProfitPercent = Param(nameof(TakeProfitPercent), 3m)
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPercent = Param(nameof(StopLossPercent), 2m)
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

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

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

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

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };

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

		StartProtection(
			takeProfit: new Unit(TakeProfitPercent, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPercent, UnitTypes.Percent),
			useMarketOrders: true);

		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 (_prevRsi == 0m)
		{
			_prevRsi = rsiValue;
			return;
		}

		var crossUp = _prevRsi < LevelDown && rsiValue > LevelDown;
		var crossDown = _prevRsi > LevelUp && rsiValue < LevelUp;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevRsi = rsiValue;
	}
}