在 GitHub 上查看

ZMFX Stolid 5a EA 策略

多时间框趋势策略,通过RSI和随机指标确认回调入场。 系统依据4小时随机指标和1小时平滑均线确定主要趋势。 在RSI超买/超卖的K线反转处开仓,并在相反信号出现时平仓。

细节

  • 入场条件
    • 多头:UpTrend && PreviousBarDown && PrevRSI < 30 && (RSI15 < 30 => double volume)
    • 空头:DownTrend && PreviousBarUp && PrevRSI > 70 && (RSI15 > 70 => double volume)
  • 多空方向:双向
  • 止损:无固定止损;根据指标条件出场
  • 默认参数
    • Volume = 1m
    • CandleType = TimeSpan.FromMinutes(5).TimeFrame()
  • 过滤器
    • 类别:趋势
    • 方向:双向
    • 指标:RSI, Stochastic, Smoothed Moving Average
    • 止损:否
    • 复杂度:中等
    • 时间框架:多时间框
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// ZMFX Stolid strategy - trades pullbacks within the main trend.
/// Uses RSI for oversold/overbought, EMA crossover for trend direction.
/// </summary>
public class ZmfxStolid5aEaStrategy : Strategy
{
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<int> _fastEmaLength;
	private readonly StrategyParam<int> _slowEmaLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevRsi;
	private bool _hasPrevRsi;

	public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
	public int FastEmaLength { get => _fastEmaLength.Value; set => _fastEmaLength.Value = value; }
	public int SlowEmaLength { get => _slowEmaLength.Value; set => _slowEmaLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public ZmfxStolid5aEaStrategy()
	{
		_rsiLength = Param(nameof(RsiLength), 11)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI period", "Indicators");

		_fastEmaLength = Param(nameof(FastEmaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");

		_slowEmaLength = Param(nameof(SlowEmaLength), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");

		_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();
		_prevRsi = 0;
		_hasPrevRsi = false;
	}

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

		var rsi = new RelativeStrengthIndex { Length = RsiLength };
		var fastEma = new ExponentialMovingAverage { Length = FastEmaLength };
		var slowEma = new ExponentialMovingAverage { Length = SlowEmaLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(rsi, fastEma, slowEma, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal rsi, decimal fastEma, decimal slowEma)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_hasPrevRsi)
		{
			_prevRsi = rsi;
			_hasPrevRsi = true;
			return;
		}

		var upTrend = fastEma > slowEma;
		var downTrend = fastEma < slowEma;

		// Buy pullback: uptrend, RSI was oversold, now crossing up
		if (upTrend && _prevRsi < 35 && rsi >= 35 && Position <= 0)
		{
			if (Position < 0)
				BuyMarket();
			BuyMarket();
		}
		// Sell pullback: downtrend, RSI was overbought, now crossing down
		else if (downTrend && _prevRsi > 65 && rsi <= 65 && Position >= 0)
		{
			if (Position > 0)
				SellMarket();
			SellMarket();
		}

		// Exit long on RSI overbought or trend reversal
		if (Position > 0 && (rsi > 75 || fastEma < slowEma))
		{
			SellMarket();
		}
		// Exit short on RSI oversold or trend reversal
		else if (Position < 0 && (rsi < 25 || fastEma > slowEma))
		{
			BuyMarket();
		}

		_prevRsi = rsi;
	}
}