在 GitHub 上查看

Color Zero Lag MA 策略

该策略使用零滞后移动平均线(Zero Lag Moving Average)来捕捉趋势反转。当 ZLMA 曲线从下降转为上升时开多仓,从上升转为下降时开空仓。若指标斜率反向,则平掉当前仓位。

参数

  • Length:零滞后均线的周期。
  • Candle Type:策略使用的 K 线周期。
  • Open Buy:允许开多仓。
  • Open Sell:允许开空仓。
  • Close Buy:当 ZLMA 向下转折时平多仓。
  • Close Sell:当 ZLMA 向上转折时平空仓。

逻辑

  1. 订阅指定周期的 K 线。
  2. 计算零滞后移动平均值。
  3. 跟踪最近两次 ZLMA 的数值以判断斜率方向。
  4. 斜率从下行转为上行时,平空并开多。
  5. 斜率从上行转为下行时,平多并开空。

该方法利用指标颜色的变化来捕捉可能的趋势反转。

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>
/// Strategy that follows ZLEMA direction changes for trend signals.
/// </summary>
public class ColorZeroLagMaStrategy : Strategy
{
	private readonly StrategyParam<int> _length;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevZlma;
	private decimal _prevPrevZlma;
	private int _count;

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

	public ColorZeroLagMaStrategy()
	{
		_length = Param(nameof(Length), 12)
			.SetGreaterThanZero()
			.SetDisplay("Length", "ZLEMA length", "Indicator");

		_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();
		_prevZlma = 0;
		_prevPrevZlma = 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 zlmaValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		_count++;

		if (_count < 3)
		{
			_prevPrevZlma = _prevZlma;
			_prevZlma = zlmaValue;
			return;
		}

		// Buy when ZLEMA turns up
		var turnUp = _prevZlma < _prevPrevZlma && zlmaValue > _prevZlma;
		// Sell when ZLEMA turns down
		var turnDown = _prevZlma > _prevPrevZlma && zlmaValue < _prevZlma;

		if (turnUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (turnDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevPrevZlma = _prevZlma;
		_prevZlma = zlmaValue;
	}
}