在 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>
/// Trades ZLEMA slope reversals with independent long/short entry/exit switches.
/// </summary>
public class ColorZerolagX10MaStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<bool> _openBuy;
	private readonly StrategyParam<bool> _openSell;
	private readonly StrategyParam<bool> _closeBuy;
	private readonly StrategyParam<bool> _closeSell;

	private decimal? _previous;
	private decimal? _beforePrevious;
	private Order _pendingOrder;

	public int Length { get => _length.Value; set => _length.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public bool BuyPosOpen { get => _openBuy.Value; set => _openBuy.Value = value; }
	public bool SellPosOpen { get => _openSell.Value; set => _openSell.Value = value; }
	public bool BuyPosClose { get => _closeBuy.Value; set => _closeBuy.Value = value; }
	public bool SellPosClose { get => _closeSell.Value; set => _closeSell.Value = value; }

	public ColorZerolagX10MaStrategy()
	{
		_length = Param(nameof(Length), 20).SetGreaterThanZero()
			.SetDisplay("Length", "ZLEMA length", "Indicator");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
		_openBuy = Param(nameof(BuyPosOpen), true).SetDisplay("Open Buy", "Allow long entries", "Trading");
		_openSell = Param(nameof(SellPosOpen), true).SetDisplay("Open Sell", "Allow short entries", "Trading");
		_closeBuy = Param(nameof(BuyPosClose), true).SetDisplay("Close Buy", "Allow long exits", "Trading");
		_closeSell = Param(nameof(SellPosClose), true).SetDisplay("Close Sell", "Allow short exits", "Trading");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_previous = _beforePrevious = null;
		_pendingOrder = null;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_previous = _beforePrevious = null;
		_pendingOrder = null;
		var zlma = new ZeroLagExponentialMovingAverage { Length = Length };
		SubscribeCandles(CandleType).Bind(zlma, ProcessCandle).Start();
	}

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

		var turnUp = _beforePrevious is decimal older && _previous is decimal previous
			&& previous < older && current > previous;
		var turnDown = _beforePrevious is decimal olderDown && _previous is decimal previousDown
			&& previousDown > olderDown && current < previousDown;
		_beforePrevious = _previous;
		_previous = current;

		if (!IsFormedAndOnlineAndAllowTrading()
			|| (_pendingOrder is not null && _pendingOrder.State is not (OrderStates.Done or OrderStates.Failed)))
			return;

		if (turnUp)
		{
			if (Position < 0m && SellPosClose)
				_pendingOrder = BuyMarket(-Position + (BuyPosOpen ? Volume : 0m));
			else if (Position == 0m && BuyPosOpen)
				_pendingOrder = BuyMarket(Volume);
		}
		else if (turnDown)
		{
			if (Position > 0m && BuyPosClose)
				_pendingOrder = SellMarket(Position + (SellPosOpen ? Volume : 0m));
			else if (Position == 0m && SellPosOpen)
				_pendingOrder = SellMarket(Volume);
		}
	}
}