Ver no GitHub

Estratégia de Scalping 15m EMA MACD RSI ATR

Estratégia de scalping que combina um filtro de tendência EMA de 50 períodos, o momentum do histograma MACD e os níveis do RSI. A gestão de risco utiliza stop loss e take profit baseados em ATR.

A estratégia compra quando o preço está acima da EMA, o histograma MACD é positivo e o RSI está entre 50 e o nível de sobrecompra. Posições vendidas ocorrem quando o preço está abaixo da EMA, o histograma é negativo e o RSI está entre o nível de sobrevenda e 50. Stops e alvos seguem o fechamento por múltiplos do ATR.

Detalhes

  • Critérios de entrada: Preço relativo à EMA, sinal do histograma MACD, nível do RSI.
  • Comprado/Vendido: Ambas as direções.
  • Critérios de saída: Stop loss ou take profit baseados em ATR.
  • Stops: Sim.
  • Valores padrão:
    • EmaPeriod = 50
    • MacdFast = 12
    • MacdSlow = 26
    • MacdSignal = 9
    • RsiPeriod = 14
    • RsiOverbought = 70
    • RsiOversold = 30
    • AtrPeriod = 14
    • SlAtrMultiplier = 1m
    • TpAtrMultiplier = 2m
    • CandleType = TimeSpan.FromMinutes(15)
  • Filtros:
    • Categoria: Scalping
    • Direção: Ambos
    • Indicadores: EMA, MACD, RSI, ATR
    • Stops: Sim
    • Complexidade: Básico
    • Período: Intradiário (15m)
    • Sazonalidade: Não
    • Redes neurais: Não
    • Divergência: Não
    • Nível de risco: Médio
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>
/// Scalping 15min EMA MACD RSI ATR strategy using EMA crossover.
/// </summary>
public class Scalping15minEmaMacdRsiAtrStrategy : Strategy
{
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

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

	public Scalping15minEmaMacdRsiAtrStrategy()
	{
		_slowLength = Param(nameof(SlowLength), 40)
			.SetGreaterThanZero()
			.SetDisplay("Slow Length", "Slow EMA period", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		var fast = new ExponentialMovingAverage { Length = 14 };
		var slow = new ExponentialMovingAverage { Length = SlowLength };
		var prevF = 0m; var prevS = 0m; var init = false;
		var lastSignal = DateTimeOffset.MinValue;
		var cooldown = TimeSpan.FromMinutes(360);
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fast, slow, (candle, f, s) =>
		{
			if (candle.State != CandleStates.Finished) return;
			if (!fast.IsFormed || !slow.IsFormed) return;
			if (!init) { prevF = f; prevS = s; init = true; return; }
			if (candle.OpenTime - lastSignal >= cooldown)
			{
				if (prevF <= prevS && f > s && Position <= 0) { BuyMarket(); lastSignal = candle.OpenTime; }
				else if (prevF >= prevS && f < s && Position >= 0) { SellMarket(); lastSignal = candle.OpenTime; }
			}
			prevF = f; prevS = s;
		}).Start();
		var area = CreateChartArea();
		if (area != null) { DrawCandles(area, subscription); DrawIndicator(area, fast); DrawIndicator(area, slow); DrawOwnTrades(area); }
	}
}