Auf GitHub ansehen

Color Zero Lag MA Strategy

This strategy uses a zero lag moving average (ZLMA) to detect trend reversals. It opens long positions when the ZLMA turns upward and opens short positions when the ZLMA turns downward. Existing positions are closed when the indicator slope reverses.

Parameters

  • Length: Period of the zero lag moving average.
  • Candle Type: Timeframe for candles used by the strategy.
  • Open Buy: Enable opening long positions.
  • Open Sell: Enable opening short positions.
  • Close Buy: Close long positions when ZLMA turns down.
  • Close Sell: Close short positions when ZLMA turns up.

Logic

  1. Subscribe to candles of the selected timeframe.
  2. Calculate the zero lag moving average.
  3. Track the last two ZLMA values to determine slope direction.
  4. If the slope changes from down to up, close short positions and open a long position.
  5. If the slope changes from up to down, close long positions and open a short position.

This simple approach follows the color change of the zero lag moving average to capture potential trend reversals.

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;
	}
}