GitHub で見る

XDerivative戦略

XDerivative戦略は、スムーズ化された変化率を使用して価格モメンタムの変化を追跡します。元のMQLエキスパートはRate of Change計算とJurikスムーシングを組み合わせてターニングポイントを検出します。StockSharp版は同じコンセプトを実装するために組み込みインジケーターを再利用します。

戦略はRocPeriodバーにわたる変化率を計算し、長さMaLengthのJurik Moving Averageでスムーシングします。スムーシングされた派生値が谷を形成すると(前の値がその前の値より低く、現在の値が前の値より上昇する)、戦略はロングポジションに入るか切り替えます。ピークが形成されると(前の値がその前の値より高く、現在の値がそれ以下に落ちる)、戦略はショートポジションに入るか切り替えます。保護ストップがエグジットを管理します。

詳細

  • エントリー条件:
    • ロング: スムーシングされた派生値がローカル最小値の後に上向きに転じる。
    • ショート: スムーシングされた派生値がローカル最大値の後に下向きに転じる。
  • ロング/ショート: 両方向。
  • エグジット条件: 逆方向の派生値の転換または保護ストップ。
  • ストップ: はい、パーセンテージのテイクプロフィットとストップロス。
  • デフォルト値:
    • RocPeriod = 34
    • MaLength = 7
    • TakeProfitPercent = 2
    • StopLossPercent = 1
    • CandleType = TimeSpan.FromHours(4)
  • フィルター:
    • カテゴリ: トレンドフォロー
    • 方向: 両方
    • インジケーター: RateOfChange, JurikMovingAverage
    • ストップ: はい
    • 複雑さ: 基本
    • 時間軸: 4H
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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);
		}
	}
}