在 GitHub 上查看

ColorMETRO XRSX 策略

该策略是对原始 MQL5 专家顾问 “Exp_ColorMETRO_XRSX” 的 StockSharp 实现。它使用两条平滑移动平均线来识别趋势变化。当快线向上穿越慢线时开多仓,当快线向下穿越慢线时开空仓。

参数

  • Fast Period – 快速移动平均线的周期。
  • Slow Period – 慢速移动平均线的周期。
  • Candle Type – 用于计算的K线时间框架。

运行机制

  1. 策略订阅选定的K线序列。
  2. 在收盘价上计算两个不同周期的 Sma 指标。
  3. 当快速 SMA 上穿慢速 SMA 时,关闭所有空头并开多头。
  4. 当快速 SMA 下穿慢速 SMA 时,关闭所有多头并开空头。
  5. 保存前一根K线的平均值以避免重复触发信号。

注意事项

  • 策略使用高层 API,并通过 Bind 处理指标。
  • 启用了 StartProtection 以管理保护机制。
  • 这是一个简化的翻译版本,并未使用原始的自定义指标。
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 the cross of two smoothed lines inspired by the ColorMETRO XRSX indicator.
/// Opens a long position when the fast line crosses above the slow line and
/// opens a short position when the fast line crosses below the slow line.
/// </summary>
public class ColorMetroXrsxStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	/// <summary>
	/// Fast moving average period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow moving average period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="ColorMetroXrsxStrategy"/> class.
	/// </summary>
	public ColorMetroXrsxStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast moving average length", "Parameters")
			;

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow moving average length", "Parameters")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to process", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = _prevSlow = null;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();

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

	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (_prevFast is decimal prevFast && _prevSlow is decimal prevSlow)
		{
			if (prevFast <= prevSlow && fast > slow && Position <= 0)
				BuyMarket();

			if (prevFast >= prevSlow && fast < slow && Position >= 0)
				SellMarket();
		}

		_prevFast = fast;
		_prevSlow = slow;
	}
}