GitHub で見る

Hurst Exponent トレンド戦略

このシステムはHurst Exponentを使用して、市場がトレンド行動を示しているかどうかを判断します。閾値を超える値は持続性を示し、閾値を下回る値はノイズまたは平均回帰を示唆します。移動平均が追加の方向確認を提供します。

テストによると、平均年間リターンは約40%です。暗号通貨市場で最も高いパフォーマンスを発揮します。

Hurst Exponentが閾値より大きく、価格が移動平均を上回って終値をつけると、戦略は買います。Hurst Exponentが高く、価格が平均を下回って終値をつけると、空売りします。Hurst Exponentが閾値を下回ると、不安定な市場での取引を避けるために既存のポジションが決済されます。

このアプローチは、エントリー前にトレンドが存在するという客観的な確認を求めるトレーダーに機能します。トレンドフィルターとストップロスの組み合わせにより、偽シグナルのリスクを管理するのに役立ちます。

詳細

  • エントリー条件:
    • ロング: Hurst > 閾値 && 終値 > MA
    • ショート: Hurst > 閾値 && 終値 < MA
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 終値 < MA または Hurst < 閾値 の時に決済
    • ショート: 終値 > MA または Hurst < 閾値 の時に決済
  • ストップ: あり、パーセンテージストップロス。
  • デフォルト値:
    • HurstPeriod = 100
    • MaPeriod = 20
    • HurstThreshold = 0.55m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: トレンド
    • 方向: 両方
    • インジケーター: Hurst Exponent, MA
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Hurst Exponent Trend strategy.
/// Uses Hurst exponent to identify trending markets.
/// </summary>
public class HurstExponentTrendStrategy : Strategy
{
	private readonly StrategyParam<int> _hurstPeriodParam;
	private readonly StrategyParam<int> _maPeriodParam;
	private readonly StrategyParam<decimal> _hurstThresholdParam;
	private readonly StrategyParam<DataType> _candleTypeParam;

	private HurstExponent _hurst;
	private SimpleMovingAverage _sma;

	/// <summary>
	/// Hurst exponent calculation period.
	/// </summary>
	public int HurstPeriod
	{
		get => _hurstPeriodParam.Value;
		set => _hurstPeriodParam.Value = value;
	}

	/// <summary>
	/// Moving average period.
	/// </summary>
	public int MaPeriod
	{
		get => _maPeriodParam.Value;
		set => _maPeriodParam.Value = value;
	}

	/// <summary>
	/// Hurst exponent threshold for trend identification.
	/// </summary>
	public decimal HurstThreshold
	{
		get => _hurstThresholdParam.Value;
		set => _hurstThresholdParam.Value = value;
	}

	/// <summary>
	/// Candle type for strategy.
	/// </summary>
	public DataType CandleType
	{
		get => _candleTypeParam.Value;
		set => _candleTypeParam.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public HurstExponentTrendStrategy()
	{
		_hurstPeriodParam = Param(nameof(HurstPeriod), 100)
			.SetGreaterThanZero()
			.SetDisplay("Hurst Period", "Period for Hurst exponent calculation", "Parameters")
			
			.SetOptimize(50, 150, 25);

		_maPeriodParam = Param(nameof(MaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Period for Moving Average", "Parameters")
			
			.SetOptimize(10, 50, 10);

		_hurstThresholdParam = Param(nameof(HurstThreshold), 0.55m)
			.SetRange(0.1m, 0.9m)
			.SetDisplay("Hurst Threshold", "Threshold value for trend identification", "Parameters")
			
			.SetOptimize(0.5m, 0.6m, 0.05m);

		_candleTypeParam = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for strategy", "Common");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_hurst = null;
		_sma = null;
	}

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

		// Create indicators
		_hurst = new HurstExponent { Length = HurstPeriod };
		_sma = new SMA { Length = MaPeriod };

		// Create subscription and bind indicators
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_hurst, _sma, ProcessCandle)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _sma);
			DrawOwnTrades(area);
		}
		
		// Enable position protection
		StartProtection(
			takeProfit: new Unit(0, UnitTypes.Absolute), // No take profit
			stopLoss: new Unit(2, UnitTypes.Percent) // 2% stop loss
		);
	}

	private void ProcessCandle(ICandleMessage candle, decimal hurstValue, decimal smaValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;
		
		// Check if market is trending (Hurst > 0.5 indicates trending market)
		bool isTrending = hurstValue > HurstThreshold;
		
		if (isTrending)
		{
			// In trending markets, use price relative to MA to determine direction
			
			// Long setup - trending market with price above MA
			if (candle.ClosePrice > smaValue && Position <= 0)
			{
				// Buy signal - trending market with price above MA
				BuyMarket(Volume + Math.Abs(Position));
			}
			// Short setup - trending market with price below MA
			else if (candle.ClosePrice < smaValue && Position >= 0)
			{
				// Sell signal - trending market with price below MA
				SellMarket(Volume + Math.Abs(Position));
			}
		}
		else
		{
			// In non-trending markets, exit positions
			if (Position > 0)
			{
				SellMarket(Position);
			}
			else if (Position < 0)
			{
				BuyMarket(Math.Abs(Position));
			}
		}
	}
}