在 GitHub 上查看

New FSCEA 策略

概览

New FSCEA 策略源自 MetaTrader 4 专家顾问 new_fscea.mq4,属于典型的 MACD 趋势追踪系统。策略将 MACD 信号线交叉、带位移的 EMA 斜率过滤、固定点数止盈以及跟踪止损结合在一起,仅在市场中保持一笔持仓。

交易逻辑

做多入场

  • MACD 主线位于零轴下方,并在当前已收盘 K 线向上穿越信号线。
  • 前一根 K 线仍然是主线在信号线下方(确认交叉)。
  • MACD 主线的绝对值大于 OpenLevelPoints(通过价格最小变动单位换算)。
  • 位移后的 EMA 斜率为正(EMA_shifted_now > EMA_shifted_previous)。
  • 当前没有持仓。

做空入场

  • MACD 主线位于零轴上方,并在当前已收盘 K 线向下穿越信号线。
  • 前一根 K 线仍然是主线在信号线上方。
  • MACD 主线大于 OpenLevelPoints(通过价格最小变动单位换算)。
  • 位移后的 EMA 斜率为负(EMA_shifted_now < EMA_shifted_previous)。
  • 当前没有持仓。

做多离场

  • 当 MACD 主线在零轴上方重新跌破信号线,且数值大于 CloseLevelPoints 时离场。
  • 或者当蜡烛最高价触及虚拟止盈价格(entry + TakeProfitPoints * priceStep)。
  • 或者当蜡烛最低价触及跟踪止损价格(随着盈利扩大而上移)。

做空离场

  • 当 MACD 主线在零轴下方重新上穿信号线,且绝对值大于 CloseLevelPoints 时离场。
  • 或者当蜡烛最低价触及虚拟止盈价格(entry - TakeProfitPoints * priceStep)。
  • 或者当蜡烛最高价触及跟踪止损价格(随着盈利扩大而下移)。

风险管理

  • 止盈以点数形式设置,通过 Security.PriceStep 转换成价格。
  • 跟踪止损同样使用点数,当浮动盈利超过设定距离后开始锁定利润。
  • 同一时间只有一笔持仓,完全复现原始 MT4 EA 的行为。
  • 通过 StartProtection() 启用默认的仓位保护。

指标

  • MACD (12, 26, 9):核心趋势过滤器,交叉方向和幅度共同决定开平仓条件。
  • EMA (TrendPeriod):基于收盘价计算。通过 TrendShift 参数将 EMA 输出向前平移若干根,用于比较斜率方向。

参数说明

参数 默认值 说明
TakeProfitPoints 300 止盈距离,单位为点。会乘以价格步长得到价格差。
TrailingStopPoints 20 跟踪止损距离,单位为点。只有当浮盈超过该距离才会启用。
OpenLevelPoints 3 允许入场的最小 MACD 幅度(点)。
CloseLevelPoints 2 触发离场的 MACD 幅度(点)。
TrendPeriod 10 EMA 趋势过滤长度。
TrendShift 2 EMA 输出的水平位移(K 线数量)。值越大,趋势确认越滞后。
TradeVolume 0.1 市价单的默认下单手数。
CandleType 1 小时时间框架 指标计算使用的蜡烛类型,可根据需求修改。

实现细节

  • 仅处理收盘完成的蜡烛,确保与 MT4 版本保持一致。
  • 通过内部缓冲模拟 MT4 中的 ma_shift,比较相隔 TrendShift 根的 EMA 值。
  • 止盈与跟踪止损以虚拟方式实现(不发送实际的限价/止损订单),完全遵循高阶 API 的最佳实践。
  • 使用 SubscribeCandles().BindEx(...) 完成数据绑定,满足仓库对高阶 API 的要求。
using System;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// NewFSCEA: EMA trend with RSI momentum and ATR stops.
/// </summary>
public class NewFsceaStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _emaLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<int> _atrLength;

	private decimal _prevClose;
	private decimal _entryPrice;

	public NewFsceaStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe.", "General");

		_emaLength = Param(nameof(EmaLength), 20)
			.SetDisplay("EMA Length", "Trend filter.", "Indicators");

		_rsiLength = Param(nameof(RsiLength), 14)
			.SetDisplay("RSI Length", "RSI period.", "Indicators");

		_atrLength = Param(nameof(AtrLength), 14)
			.SetDisplay("ATR Length", "ATR period.", "Indicators");
	}

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

	public int EmaLength
	{
		get => _emaLength.Value;
		set => _emaLength.Value = value;
	}

	public int RsiLength
	{
		get => _rsiLength.Value;
		set => _rsiLength.Value = value;
	}

	public int AtrLength
	{
		get => _atrLength.Value;
		set => _atrLength.Value = value;
	}

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

		_prevClose = 0;
		_entryPrice = 0;
	}

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

		_prevClose = 0;
		_entryPrice = 0;

		var ema = new ExponentialMovingAverage { Length = EmaLength };
		var rsi = new RelativeStrengthIndex { Length = RsiLength };
		var atr = new AverageTrueRange { Length = AtrLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, rsi, atr, ProcessCandle)
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, decimal emaVal, decimal rsiVal, decimal atrVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var close = candle.ClosePrice;

		if (_prevClose == 0 || atrVal <= 0)
		{
			_prevClose = close;
			return;
		}

		if (Position > 0)
		{
			if (close >= _entryPrice + atrVal * 2.5m || close <= _entryPrice - atrVal * 1.5m || rsiVal > 75)
			{
				SellMarket();
				_entryPrice = 0;
			}
		}
		else if (Position < 0)
		{
			if (close <= _entryPrice - atrVal * 2.5m || close >= _entryPrice + atrVal * 1.5m || rsiVal < 25)
			{
				BuyMarket();
				_entryPrice = 0;
			}
		}

		if (Position == 0)
		{
			if (close > emaVal && _prevClose <= emaVal && rsiVal > 50)
			{
				_entryPrice = close;
				BuyMarket();
			}
			else if (close < emaVal && _prevClose >= emaVal && rsiVal < 50)
			{
				_entryPrice = close;
				SellMarket();
			}
		}

		_prevClose = close;
	}
}