Auf GitHub ansehen

XDerivative Strategy

XDerivative Strategy tracks shifts in price momentum using a smoothed rate of change. The original MQL expert combines a rate-of-change calculation with Jurik smoothing to detect turning points. The StockSharp version reuses built-in indicators to implement the same concept.

The strategy computes the rate of change over RocPeriod bars and smooths it with a Jurik Moving Average of length MaLength. When the smoothed derivative forms a trough (previous value is lower than its predecessor and the current value rises above the previous) the strategy enters or flips to a long position. When a peak forms (previous value is higher than its predecessor and the current value falls below it) the strategy enters or flips to a short position. Protective stops manage exits.

Details

  • Entry Criteria:
    • Long: Smoothed derivative turns up after a local minimum.
    • Short: Smoothed derivative turns down after a local maximum.
  • Long/Short: Both directions.
  • Exit Criteria: Opposite derivative turn or protective stop.
  • Stops: Yes, percentage take-profit and stop-loss.
  • Default Values:
    • RocPeriod = 34
    • MaLength = 7
    • TakeProfitPercent = 2
    • StopLossPercent = 1
    • CandleType = TimeSpan.FromHours(4)
  • Filters:
    • Category: Trend following
    • Direction: Both
    • Indicators: RateOfChange, JurikMovingAverage
    • Stops: Yes
    • Complexity: Basic
    • Timeframe: 4H
    • 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>
/// XDerivative strategy based on smoothed rate of change.
/// Enters long when the smoothed derivative forms a trough and short when it forms a peak.
/// </summary>
public class XDerivativeStrategy : Strategy
{
	private readonly StrategyParam<int> _rocPeriod;
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<decimal> _takeProfitPct;
	private readonly StrategyParam<decimal> _stopLossPct;
	private readonly StrategyParam<DataType> _candleType;

	private JurikMovingAverage _jma;
	private decimal? _prevValue;
	private decimal? _prevPrevValue;

	public int RocPeriod
	{
		get => _rocPeriod.Value;
		set => _rocPeriod.Value = value;
	}

	public int MaLength
	{
		get => _maLength.Value;
		set => _maLength.Value = value;
	}

	public decimal TakeProfitPct
	{
		get => _takeProfitPct.Value;
		set => _takeProfitPct.Value = value;
	}

	public decimal StopLossPct
	{
		get => _stopLossPct.Value;
		set => _stopLossPct.Value = value;
	}

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

	public XDerivativeStrategy()
	{
		_rocPeriod = Param(nameof(RocPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ROC Period", "Period for rate of change", "Parameters");

		_maLength = Param(nameof(MaLength), 7)
			.SetGreaterThanZero()
			.SetDisplay("JMA Length", "Period for Jurik MA smoothing", "Parameters");

		_takeProfitPct = Param(nameof(TakeProfitPct), 3m)
			.SetGreaterThanZero()
			.SetDisplay("Take Profit %", "Take profit percentage", "Risk");

		_stopLossPct = Param(nameof(StopLossPct), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Stop Loss %", "Stop loss percentage", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "Parameters");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_jma = default;
		_prevValue = null;
		_prevPrevValue = null;
	}

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

		_jma = new JurikMovingAverage { Length = MaLength };
		var roc = new RateOfChange { Length = RocPeriod };

		Indicators.Add(_jma);

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(roc, (candle, rocValue) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				var jmaResult = _jma.Process(rocValue, candle.OpenTime, true);
				if (!jmaResult.IsFormed)
					return;

				var value = jmaResult.ToDecimal();

				if (_prevValue is decimal prev && _prevPrevValue is decimal prev2)
				{
					var turnUp = prev < prev2 && value > prev;
					var turnDown = prev > prev2 && value < prev;

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

				_prevPrevValue = _prevValue;
				_prevValue = value;
			})
			.Start();

		StartProtection(
			takeProfit: new Unit(TakeProfitPct, UnitTypes.Percent),
			stopLoss: new Unit(StopLossPct, UnitTypes.Percent),
			useMarketOrders: true);

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, roc);
			DrawOwnTrades(area);
		}
	}
}