Ver no GitHub

Estratégia do Indicador Stalin

Esta estratégia replica a lógica do indicador "Stalin" do MQL5. Usa um par de médias móveis exponenciais (EMAs) e um filtro RSI opcional. Um sinal de compra aparece quando a EMA rápida cruza acima da EMA lenta e o RSI está acima de 50. Um sinal de venda aparece quando a EMA rápida cruza abaixo da EMA lenta e o RSI está abaixo de 50.

Os sinais podem ser confirmados por um movimento de preço necessário e filtrados pela distância do último sinal. As posições são abertas com ordens a mercado e invertidas em sinais opostos.

Detalhes

  • Critérios de entrada:
    • Comprado: FastEMA(t-1) < SlowEMA(t-1) && FastEMA(t) > SlowEMA(t) && RSI(t) > 50.
    • Vendido: FastEMA(t-1) > SlowEMA(t-1) && FastEMA(t) < SlowEMA(t) && RSI(t) < 50.
  • Confirmar: A operação é adiada até o preço se mover Confirm pontos a partir do nível de rompimento.
  • Filtro Flat: Novos sinais são ignorados se estiverem mais próximos que Flat pontos do preço do sinal anterior.
  • Comprado/Vendido: Ambos.
  • Critérios de saída: Sinal oposto.
  • Stops: Nenhum.
  • Valores padrão:
    • FastLength = 14.
    • SlowLength = 21.
    • RsiLength = 17.
    • Confirm = 0 pontos (desabilitado).
    • Flat = 0 pontos (desabilitado).
    • CandleType = velas de 1 hora.
  • Filtros:
    • Categoria: Seguidor de tendência.
    • Direção: Ambos.
    • Indicadores: Múltiplos.
    • Stops: Não.
    • Complexidade: Moderado.
    • Período: Médio prazo.
    • Sazonalidade: Não.
    • Redes neurais: Não.
    • Divergência: Não.
    • Nível de risco: Médio.
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 based on "Stalin" indicator.
/// Uses fast and slow EMAs with optional RSI filter.
/// </summary>
public class StalinStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _fastMa;
	private ExponentialMovingAverage _slowMa;
	private RelativeStrengthIndex _rsi;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _initialized;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public StalinStrategy()
	{
		_fastLength = Param(nameof(FastLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicator")
			.SetOptimize(5, 30, 1);

		_slowLength = Param(nameof(SlowLength), 21)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicator")
			.SetOptimize(20, 50, 1);

		_rsiLength = Param(nameof(RsiLength), 17)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI period", "Indicator")
			.SetOptimize(10, 30, 5);

		_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();
		_fastMa = null;
		_slowMa = null;
		_rsi = null;
		_prevFast = 0;
		_prevSlow = 0;
		_initialized = false;
	}

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

		_fastMa = new ExponentialMovingAverage { Length = FastLength };
		_slowMa = new ExponentialMovingAverage { Length = SlowLength };
		_rsi = new RelativeStrengthIndex { Length = RsiLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_fastMa, _slowMa, _rsi, ProcessCandle)
			.Start();

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

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

		if (!_initialized)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_initialized = true;
			return;
		}

		var buySignal = _prevFast <= _prevSlow && fast > slow && rsiValue > 50m;
		var sellSignal = _prevFast >= _prevSlow && fast < slow && rsiValue < 50m;

		if (IsFormedAndOnlineAndAllowTrading())
		{
			if (buySignal && Position <= 0)
				BuyMarket();
			else if (sellSignal && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}