在 GitHub 上查看

RSI RFTL 策略

该策略将 MetaTrader 5 的 RSI RFTL EA 迁移到 StockSharp 的高级 API。核心思想仍然是利用 RSI 的摆动高低点绘制趋势线,并借助 Recursive Filter Trend Line(RFTL)做方向过滤。实现过程中保留了原始智能交易系统的逐棒判断,同时采用 StrategyParam、蜡烛订阅和指标绑定等 StockSharp 原生组件。

工作机制

  1. RSI 摆动识别 – 扫描最近 500 个 RSI 值以寻找局部高点和低点。高点必须先后突破 40 和 60,低点必须跌破 60 和 40,与原始 MQL 判定一致。
  2. 趋势线投射 – 找到两处有效的高点或低点后,通过它们构建 RSI 趋势线,并将该直线外推到当前和上一根柱子。若中间出现突破 40/60 的摆动,趋势线立即失效。
  3. RFTL 过滤 – 计算出的 RFTL(使用原始系数表)上一根柱子的取值需高于上一根收盘价才能做空,或低于收盘价才能做多,从而保证顺势进入。
  4. 入场门槛 – RSI 还必须保持在合适的区间:做空需要 RSI 维持在 47/50 以上,做多要求 RSI 处于 55/50 以下。
  5. 风险控制 – 止损、止盈和追踪止损均以点(pip)为单位,并在每根收盘柱上更新,完全复刻 MQL 中的修改逻辑。额外的保护在 RSI 突破 70(平多)或跌破 30(平空)时触发。

入场条件

  • 做空信号
    • 两个 RSI 低点低于 60/40,形成的上升趋势线在当前柱被向下突破(RSI[1] < 线RSI[2] > 线(上一柱))。
    • RFTL 的前值高于上一根收盘价,确认下行压力。
    • RSI 保持在多头区间(RSI[2] > 50RSI[0] > 47),且检测到的顶部比底部更早出现(pos₂ > pos₄)。
  • 做多信号
    • 两个 RSI 高点高于 40/60,形成的下降趋势线被向上突破(RSI[1] > 线RSI[2] < 线(上一柱))。
    • RFTL 的前值低于上一根收盘价。
    • RSI 位于空头区域(RSI[2] < 50RSI[0] < 55),并且最近的底部比顶部更近(pos₄ > pos₂)。

只有在所有指标形成且积累到足够历史数据之后才会评估这些信号,避免基于不完整信息下单。

风险管理

  • 止损 / 止盈 – 以点数配置。当当前蜡烛触及相应价格时立即平仓,并重置追踪状态。
  • 追踪止损 – 可选功能。当浮盈超过 TrailingStopPips + TrailingStepPips 时,追踪止损开始跟随收盘价,并在每次价格至少额外推进 TrailingStepPips 点后才再次收紧。
  • RSI 紧急退出 – RSI 超过 70 时平掉多单,低于 30 时平掉空单,保持与原版 EA 一致。

参数

参数 默认值 说明
CandleType 1 小时 计算 RSI 与 RFTL 所用的周期。
TradeVolume 1 每次开仓的下单手数。
RsiPeriod 30 RSI 的回溯长度。
StopLossPips 50 止损距离(点),0 表示禁用。
TakeProfitPips 50 止盈距离(点),0 表示禁用。
TrailingStopPips 5 追踪止损的基准距离,0 表示关闭。
TrailingStepPips 5 每次收紧追踪止损所需的额外点数。

所有距离都会乘以品种的 PriceStep,从而与 MQL 中的点值处理保持一致。

使用步骤

  1. 将策略连接到目标标的,并把 CandleType 设置为与你在 MetaTrader 中测试的周期一致。
  2. 调整风险参数(止损、止盈、追踪止损),填入对应的点数;设置为 0 可关闭该保护。
  3. 启动策略,系统会自动订阅蜡烛、计算 RSI 与 RFTL,并在积累足够历史后开始评估信号。
  4. 关注图表输出:价格区域会绘制蜡烛与 RFTL 线,第二个窗口展示 RSI 振荡器。

注意事项与差异

  • RFTL 指标直接用 C# 实现,并使用原始系数,无需额外文件。
  • 策略仅维护单一头寸,会在多头、空头与空仓之间切换,与原始 EA 的单魔术号逻辑一致。
  • 由于 StockSharp 不会自动执行 MT5 的服务器端止损,本策略在内部模拟保护性退出,并在该柱上跳过重新入场,以获得更安全的近似行为。
  • 历史缓存限制为 600 条记录,与源代码中的 500 元素数组规模接近,同时避免内存无限增长。
  • 所有代码注释均为英文,并符合 StockSharp 高级 API 的风格指南。
namespace StockSharp.Samples.Strategies;

using System;
using System.Collections.Generic;

using Ecng.Common;

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

/// <summary>
/// RSI-based trend strategy with a simple recursive filter (EMA) as trend confirmation.
/// Buys when RSI crosses above oversold level and EMA confirms uptrend.
/// Sells when RSI crosses below overbought level and EMA confirms downtrend.
/// </summary>
public class RsiRftlStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiPeriod;
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<decimal> _overbought;
	private readonly StrategyParam<decimal> _oversold;

	private RelativeStrengthIndex _rsi;
	private ExponentialMovingAverage _ema;

	private decimal _prevRsi;
	private decimal _entryPrice;
	private int _cooldown;

	/// <summary>
	/// RSI lookback period.
	/// </summary>
	public int RsiPeriod
	{
		get => _rsiPeriod.Value;
		set => _rsiPeriod.Value = value;
	}

	/// <summary>
	/// EMA period for trend filter.
	/// </summary>
	public int EmaPeriod
	{
		get => _emaPeriod.Value;
		set => _emaPeriod.Value = value;
	}

	/// <summary>
	/// Overbought RSI level.
	/// </summary>
	public decimal Overbought
	{
		get => _overbought.Value;
		set => _overbought.Value = value;
	}

	/// <summary>
	/// Oversold RSI level.
	/// </summary>
	public decimal Oversold
	{
		get => _oversold.Value;
		set => _oversold.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the strategy.
	/// </summary>
	public RsiRftlStrategy()
	{
		_rsiPeriod = Param(nameof(RsiPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("RSI Period", "Length of the RSI oscillator", "Indicator");

		_emaPeriod = Param(nameof(EmaPeriod), 44)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Length of the trend EMA filter", "Indicator");

		_overbought = Param(nameof(Overbought), 75m)
			.SetDisplay("Overbought", "RSI overbought level", "Levels");

		_oversold = Param(nameof(Oversold), 25m)
			.SetDisplay("Oversold", "RSI oversold level", "Levels");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_rsi = null;
		_ema = null;
		_prevRsi = 0;
		_entryPrice = 0;
		_cooldown = 0;
	}

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

		_rsi = new RelativeStrengthIndex { Length = RsiPeriod };
		_ema = new ExponentialMovingAverage { Length = EmaPeriod };

		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_rsi, _ema, OnProcess);
		subscription.Start();
	}

	private void OnProcess(ICandleMessage candle, decimal rsiValue, decimal emaValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_rsi.IsFormed || !_ema.IsFormed)
		{
			_prevRsi = rsiValue;
			return;
		}

		if (_cooldown > 0)
		{
			_cooldown--;
			_prevRsi = rsiValue;
			return;
		}

		var close = candle.ClosePrice;
		var trendUp = close > emaValue;
		var trendDown = close < emaValue;

		// Buy: RSI crosses above oversold + uptrend
		if (_prevRsi < Oversold && rsiValue >= Oversold && trendUp && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();

			BuyMarket();
			_entryPrice = close;
			_cooldown = 10;
		}
		// Sell: RSI crosses below overbought + downtrend
		else if (_prevRsi > Overbought && rsiValue <= Overbought && trendDown && Position >= 0)
		{
			if (Position > 0)
				SellMarket();

			SellMarket();
			_entryPrice = close;
			_cooldown = 10;
		}

		// Exit long on overbought
		if (Position > 0 && rsiValue > 80m)
		{
			SellMarket();
			_entryPrice = 0;
			_cooldown = 10;
		}
		// Exit short on oversold
		else if (Position < 0 && rsiValue < 20m)
		{
			BuyMarket();
			_entryPrice = 0;
			_cooldown = 10;
		}

		_prevRsi = rsiValue;
	}
}