Ver no GitHub

Estratégia de Negociação Automática com RSI

Esta estratégia calcula a média dos últimos valores de RSI para gerar sinais de trading. Calcula um Índice de Força Relativa (RSI) padrão sobre um período configurável e depois aplica uma média móvel simples ao próprio RSI. As operações são abertas quando o RSI médio cruza limites predefinidos e fechadas quando o limiar oposto é atingido.

Lógica de trading

  1. Cálculo do RSI
    • O indicador usa RsiPeriod para calcular o RSI baseado nos preços de fechamento das velas.
  2. Média do RSI
    • Os últimos AveragePeriod valores de RSI são suavizados por uma média móvel simples.
  3. Regras de entrada
    • Se BuyEnabled for true e nenhuma posição estiver aberta, uma ordem de compra é enviada quando o RSI médio excede BuyThreshold (padrão 55).
    • Se SellEnabled for true e nenhuma posição estiver aberta, uma ordem de venda é enviada quando o RSI médio cai abaixo de SellThreshold (padrão 45).
  4. Regras de saída
    • Quando CloseBySignal for true, posições abertas são fechadas em sinais opostos:
      • Posições compradas fecham quando o RSI médio cai abaixo de CloseBuyThreshold (padrão 47).
      • Posições vendidas fecham quando o RSI médio sobe acima de CloseSellThreshold (padrão 52).

Parâmetros

  • BuyEnabled – habilitar ou desabilitar entradas compradas.
  • SellEnabled – habilitar ou desabilitar entradas vendidas.
  • CloseBySignal – permitir saídas em sinais RSI opostos.
  • RsiPeriod – comprimento do cálculo do RSI.
  • AveragePeriod – número de valores de RSI usados para a média.
  • BuyThreshold – valor do RSI médio acima do qual uma posição comprada é aberta.
  • SellThreshold – valor do RSI médio abaixo do qual uma posição vendida é aberta.
  • CloseBuyThreshold – valor do RSI médio abaixo do qual uma posição comprada é fechada.
  • CloseSellThreshold – valor do RSI médio acima do qual uma posição vendida é fechada.
  • CandleType – tipo de vela para assinaturas.

Notas

Esta estratégia demonstra como valores de indicadores podem ser combinados por vinculação na API de alto nível do StockSharp. Funções de trailing stop e gerenciamento de dinheiro da versão MQL original são omitidas por simplicidade.

using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy that trades based on averaged RSI values.
/// Uses RSI smoothed by SMA to generate signals.
/// </summary>
public class AutoTradeWithRsiStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _averagePeriod;
	private readonly StrategyParam<decimal> _buyThreshold;
	private readonly StrategyParam<decimal> _sellThreshold;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _rsiAvg;

	public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
	public int AveragePeriod { get => _averagePeriod.Value; set => _averagePeriod.Value = value; }
	public decimal BuyThreshold { get => _buyThreshold.Value; set => _buyThreshold.Value = value; }
	public decimal SellThreshold { get => _sellThreshold.Value; set => _sellThreshold.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public AutoTradeWithRsiStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "RSI calculation period", "Indicator");

		_averagePeriod = Param(nameof(AveragePeriod), 21)
			.SetGreaterThanZero()
			.SetDisplay("Average Period", "SMA period to smooth RSI", "Indicator");

		_buyThreshold = Param(nameof(BuyThreshold), 55m)
			.SetDisplay("Buy Threshold", "Averaged RSI above which to buy", "Rules");

		_sellThreshold = Param(nameof(SellThreshold), 45m)
			.SetDisplay("Sell Threshold", "Averaged RSI below which to sell", "Rules");

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

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_rsiAvg = null;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		_rsiAvg = new ExponentialMovingAverage { Length = AveragePeriod };
		Indicators.Add(_rsiAvg);

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

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

			var area2 = CreateChartArea();
			if (area2 != null)
				DrawIndicator(area2, rsi);
		}
	}

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

		if (!rsiValue.IsFormed)
			return;

		var avgResult = _rsiAvg.Process(rsiValue);
		if (!avgResult.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var avgRsi = avgResult.GetValue<decimal>();

		if (avgRsi > BuyThreshold && Position <= 0)
			BuyMarket();
		else if (avgRsi < SellThreshold && Position >= 0)
			SellMarket();
	}
}