Ver no GitHub

Forex Fire EMA MA RSI Strategy

Multi-timeframe trend strategy using EMA, MA and RSI confirmation. Uses 4h candles for confluence and 15m candles for entries.

Details

  • Entry Criteria:
    • Long: Short EMA above long EMA, price above MA, fast RSI above slow RSI and >50, volume increasing with higher timeframe confirmation.
    • Short: Opposite conditions.
  • Long/Short: Both.
  • Exit Criteria:
    • EMA cross or RSI reaching thresholds.
    • Optional stop loss, take profit, trailing stop and ATR-based exit.
  • Stops: Yes, configurable.
  • Default Values:
    • EmaShortLength = 13
    • EmaLongLength = 62
    • MaLength = 200
    • MaType = MovingAverageTypeEnum.Simple
    • RsiSlowLength = 28
    • RsiFastLength = 7
    • RsiOverbought = 70
    • RsiOversold = 30
    • UseStopLoss = true
    • StopLossPercent = 2
    • UseTakeProfit = true
    • TakeProfitPercent = 4
    • UseTrailingStop = true
    • TrailingPercent = 1.5
    • UseAtrExits = true
    • AtrMultiplier = 2
    • AtrLength = 14
    • EntryCandleType = TimeSpan.FromMinutes(15).TimeFrame()
    • ConfluenceCandleType = TimeSpan.FromHours(4).TimeFrame()
  • Filters:
    • Category: Trend
    • Direction: Both
    • Indicators: EMA, MA, RSI, ATR
    • Stops: Yes
    • Complexity: Medium
    • Timeframe: Multi-timeframe
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: Medium
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;

public class ForexFireEmaMaRsiStrategy : Strategy
{
	private readonly StrategyParam<int> _fastEmaPeriod;
	private readonly StrategyParam<int> _slowEmaPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private decimal _prevFastEma;
	private decimal _prevSlowEma;

	public int FastEmaPeriod { get => _fastEmaPeriod.Value; set => _fastEmaPeriod.Value = value; }
	public int SlowEmaPeriod { get => _slowEmaPeriod.Value; set => _slowEmaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ForexFireEmaMaRsiStrategy()
	{
		_fastEmaPeriod = Param(nameof(FastEmaPeriod), 120)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
		_slowEmaPeriod = Param(nameof(SlowEmaPeriod), 450)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFastEma = 0m;
		_prevSlowEma = 0m;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		var fastEma = new ExponentialMovingAverage { Length = FastEmaPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowEmaPeriod };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fastEma, slowEma, ProcessCandle).Start();
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastEma);
			DrawIndicator(area, slowEma);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastEmaValue, decimal slowEmaValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (_prevFastEma == 0m || _prevSlowEma == 0m)
		{
			_prevFastEma = fastEmaValue;
			_prevSlowEma = slowEmaValue;
			return;
		}
		if (_prevFastEma <= _prevSlowEma && fastEmaValue > slowEmaValue && Position <= 0)
			BuyMarket();
		else if (_prevFastEma >= _prevSlowEma && fastEmaValue < slowEmaValue && Position >= 0)
			SellMarket();
		_prevFastEma = fastEmaValue;
		_prevSlowEma = slowEmaValue;
	}
}