在 GitHub 上查看

Stalin 指标策略

该策略复刻 MQL5 中的 “Stalin” 指标逻辑。 它使用两条指数移动平均线 (EMA) 以及可选的 RSI 过滤。 当快 EMA 从下向上穿越慢 EMA 且 RSI 高于 50 时产生做多信号。 当快 EMA 从上向下穿越慢 EMA 且 RSI 低于 50 时产生做空信号。

信号可以通过价格必须移动 Confirm 点以及与上一个信号间距 Flat 点来进行确认和过滤。 策略使用市价单开仓,并在出现反向信号时反手。

详情

  • 入场条件:
    • 多头: FastEMA(t-1) < SlowEMA(t-1) && FastEMA(t) > SlowEMA(t) && RSI(t) > 50
    • 空头: FastEMA(t-1) > SlowEMA(t-1) && FastEMA(t) < SlowEMA(t) && RSI(t) < 50
  • 确认: 价格从突破点移动 Confirm 点后才入场。
  • Flat 过滤: 如果距离上次信号不到 Flat 点则忽略新信号。
  • 多空方向: 双向。
  • 退出条件: 反向信号。
  • 止损: 无。
  • 默认值:
    • FastLength = 14。
    • SlowLength = 21。
    • RsiLength = 17。
    • Confirm = 0 点(禁用)。
    • Flat = 0 点(禁用)。
    • CandleType = 1 小时 K 线。
  • 过滤器:
    • 类别: 趋势跟随。
    • 方向: 双向。
    • 指标: 多个。
    • 止损: 无。
    • 复杂度: 中等。
    • 时间框架: 中期。
    • 季节性: 无。
    • 神经网络: 无。
    • 背离: 无。
    • 风险水平: 中等。
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy based on "Stalin" indicator.
/// Uses fast and slow EMAs with optional RSI filter.
/// </summary>
public class StalinStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<int> _rsiLength;
	private readonly StrategyParam<DataType> _candleType;

	private ExponentialMovingAverage _fastMa;
	private ExponentialMovingAverage _slowMa;
	private RelativeStrengthIndex _rsi;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _initialized;

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

	public StalinStrategy()
	{
		_fastLength = Param(nameof(FastLength), 14)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicator")
			.SetOptimize(5, 30, 1);

		_slowLength = Param(nameof(SlowLength), 21)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicator")
			.SetOptimize(20, 50, 1);

		_rsiLength = Param(nameof(RsiLength), 17)
			.SetGreaterThanZero()
			.SetDisplay("RSI Length", "RSI period", "Indicator")
			.SetOptimize(10, 30, 5);

		_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();
		_fastMa = null;
		_slowMa = null;
		_rsi = null;
		_prevFast = 0;
		_prevSlow = 0;
		_initialized = false;
	}

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

		_fastMa = new ExponentialMovingAverage { Length = FastLength };
		_slowMa = new ExponentialMovingAverage { Length = SlowLength };
		_rsi = new RelativeStrengthIndex { Length = RsiLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_fastMa, _slowMa, _rsi, ProcessCandle)
			.Start();

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

	private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow, decimal rsiValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_initialized)
		{
			_prevFast = fast;
			_prevSlow = slow;
			_initialized = true;
			return;
		}

		var buySignal = _prevFast <= _prevSlow && fast > slow && rsiValue > 50m;
		var sellSignal = _prevFast >= _prevSlow && fast < slow && rsiValue < 50m;

		if (IsFormedAndOnlineAndAllowTrading())
		{
			if (buySignal && Position <= 0)
				BuyMarket();
			else if (sellSignal && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}