在 GitHub 上查看

Color Zerolag X10 MA 策略

该策略是 MetaTrader 示例 Exp_ColorZerolagX10MA.mq5 的简化移植版本。它利用零滞后指数移动平均线 (Zero Lag EMA) 来检测斜率变化。当均线在连续两根柱子下降后转而向上时,策略会开多或反手做多;反之,当均线在上升后转而向下时,策略会开空。

原始系统依赖由十条平滑均线构成的复杂指标。此处使用 StockSharp 内置的 ZeroLagExponentialMovingAverage 代替,使实现更加简洁。策略在选定的K线时间框架上运行,并可通过参数启用或禁用各个动作(开/平多空)。

细节

  • 入场条件
    • 多头ZLEMA[t-2] > ZLEMA[t-1]ZLEMA[t] > ZLEMA[t-1]
    • 空头ZLEMA[t-2] < ZLEMA[t-1]ZLEMA[t] < ZLEMA[t-1]
  • 多空方向:双向。
  • 出场条件
    • 当出现空头信号且启用 BuyPosClose 时平多。
    • 当出现多头信号且启用 SellPosClose 时平空。
  • 止损:默认无,依靠反向信号退出。
  • 默认值
    • Length = 20。
    • CandleType = 4 小时时间框架。
    • 所有动作标志 (BuyPosOpen, SellPosOpen, BuyPosClose, SellPosClose) 启用。
  • 过滤器
    • 类别:趋势跟随
    • 方向:双向
    • 指标:单一
    • 止损:否
    • 复杂度:简单
    • 时间框架:中期
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中等
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>
/// Trend detection strategy based on the slope of a zero lag moving average.
/// </summary>
public class ColorZerolagX10MaStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prev1;
	private decimal _prev2;
	private int _count;

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

	public ColorZerolagX10MaStrategy()
	{
		_length = Param(nameof(Length), 20)
			.SetDisplay("Length", "MA length", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prev1 = 0;
		_prev2 = 0;
		_count = 0;
	}

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

		var zlma = new ZeroLagExponentialMovingAverage { Length = Length };

		SubscribeCandles(CandleType)
			.Bind(zlma, ProcessCandle)
			.Start();
	}

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

		_count++;
		if (_count < 3)
		{
			_prev2 = _prev1;
			_prev1 = ma;
			return;
		}

		// Detect trend turn via slope direction change
		var trendUp = _prev1 < _prev2 && ma > _prev1;
		var trendDown = _prev1 > _prev2 && ma < _prev1;

		if (trendUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (trendDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prev2 = _prev1;
		_prev1 = ma;
	}
}