GitHub で見る

Color Zerolag X10 MA Strategy

This strategy is a simplified port of the MetaTrader example Exp_ColorZerolagX10MA.mq5. It uses a zero lag exponential moving average to detect slope changes. When the moving average turns upward after decreasing for two bars, the strategy opens or reverses to a long position. Conversely, when the moving average turns downward after increasing, it opens or reverses to a short position.

The logic mimics the original idea where a combined set of ten smoothed moving averages produces a single color-coded line. Here we replace that complex indicator with StockSharp's built-in ZeroLagExponentialMovingAverage to keep the implementation compact and reusable. The system works on the selected candle timeframe and can enable or disable individual actions (open/close long/short) via parameters.

Details

  • Entry Criteria:
    • Long: ZLEMA[t-2] > ZLEMA[t-1] and ZLEMA[t] > ZLEMA[t-1].
    • Short: ZLEMA[t-2] < ZLEMA[t-1] and ZLEMA[t] < ZLEMA[t-1].
  • Long/Short: Both directions supported.
  • Exit Criteria:
    • Long positions are closed when a short signal appears and BuyPosClose is enabled.
    • Short positions are closed when a long signal appears and SellPosClose is enabled.
  • Stops: None by default; exits rely on opposite signals.
  • Default Values:
    • Length = 20.
    • CandleType = 4-hour timeframe.
    • All action flags (BuyPosOpen, SellPosOpen, BuyPosClose, SellPosClose) enabled.
  • Filters:
    • Category: Trend following
    • Direction: Both
    • Indicators: Single
    • Stops: No
    • Complexity: Simple
    • Timeframe: Medium-term
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
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;
	}
}