Hurst Exponent Trend 策略
本策略使用赫斯特指数判断市场是否处于趋势状态。指数高于阈值表示趋势持续,低于阈值则可能是震荡或均值回归。移动平均线提供方向确认。
测试表明年均收益约为 40%,该策略在加密市场表现最佳。
当赫斯特指数高于阈值且收盘价站在均线上方时做多;当赫斯特指数高且价格收于均线下方时做空。若赫斯特指数跌破阈值,则平掉现有仓位以避免在震荡市中交易。
这种方法适合希望在入场前获得客观趋势确认的交易者。趋势过滤和止损结合有助于管理假信号带来的风险。
细节
- 入场条件:
- 多头:
Hurst > Threshold && Close > MA - 空头:
Hurst > Threshold && Close < MA
- 多头:
- 多/空: 双向
- 离场条件:
- 多头: 收盘价跌破MA或Hurst < Threshold
- 空头: 收盘价升破MA或Hurst < Threshold
- 止损: 百分比止损
- 默认值:
HurstPeriod= 100MaPeriod= 20HurstThreshold= 0.55StopLossPercent= 2 MA为MaPeriod周期简单移动平均。止损为入场价的固定StopLossPercent百分比,在K线之间同样监控;设为0则关闭。与持仓方向相反的入场信号会反转持仓。CandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 类别: Trend
- 方向: 双向
- 指标: Hurst Exponent, MA
- 止损: 是
- 复杂度: 中等
- 时间框架: 日内
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
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>
/// Hurst Exponent Trend strategy.
/// A HurstPeriod Hurst exponent above HurstThreshold marks a trending market. Then a close above the MaPeriod simple moving average goes long
/// and a close below it goes short, reversing an opposite position. A long closes once the close is below the average or the exponent
/// drops below the threshold, a short mirrors it, and a percent stop limits the loss.
/// </summary>
public class HurstExponentTrendStrategy : Strategy
{
private readonly StrategyParam<int> _hurstPeriod;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<decimal> _hurstThreshold;
private readonly StrategyParam<decimal> _stopLossPercent;
private readonly StrategyParam<DataType> _candleType;
/// <summary>
/// Period of the Hurst exponent.
/// </summary>
public int HurstPeriod
{
get => _hurstPeriod.Value;
set => _hurstPeriod.Value = value;
}
/// <summary>
/// Period of the simple moving average.
/// </summary>
public int MaPeriod
{
get => _maPeriod.Value;
set => _maPeriod.Value = value;
}
/// <summary>
/// Hurst exponent level of a trending market.
/// </summary>
public decimal HurstThreshold
{
get => _hurstThreshold.Value;
set => _hurstThreshold.Value = value;
}
/// <summary>
/// Stop loss percentage from entry price.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Candle type.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Constructor.
/// </summary>
public HurstExponentTrendStrategy()
{
_hurstPeriod = Param(nameof(HurstPeriod), 100)
.SetGreaterThanZero()
.SetDisplay("Hurst Period", "Period of the Hurst exponent", "Indicators");
_maPeriod = Param(nameof(MaPeriod), 20)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Period of the simple moving average", "Indicators");
_hurstThreshold = Param(nameof(HurstThreshold), 0.55m)
.SetDisplay("Hurst Threshold", "Hurst exponent level of a trending market", "Indicators");
_stopLossPercent = Param(nameof(StopLossPercent), 2m)
.SetNotNegative()
.SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var hurst = new HurstExponent { Length = HurstPeriod };
var sma = new SimpleMovingAverage { Length = MaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(hurst, sma, ProcessCandle)
.Start();
StartProtection(new Unit(), new Unit(StopLossPercent, UnitTypes.Percent), useMarketOrders: true, isLocalStop: true);
// The stop has to see prices between candles, not only at their close.
foreach (var field in new[] { Level1Fields.BestBidPrice, Level1Fields.BestAskPrice })
{
var quotes = new Subscription(DataType.Level1, Security);
quotes.MarketData.BuildField = field;
SubscribeLevel1(quotes).Bind(ObserveProtectionQuote).Start();
}
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawOwnTrades(area);
var oscillators = CreateChartArea();
if (oscillators != null)
{
DrawIndicator(oscillators, hurst);
}
}
}
private void ObserveProtectionQuote(Level1ChangeMessage quote)
{
// The high-level handler activates native protection before this callback, also between signal bars.
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue hurstValue, IIndicatorValue smaValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!hurstValue.IsFormed || !smaValue.IsFormed)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var hurst = hurstValue.GetValue<decimal>();
var sma = smaValue.GetValue<decimal>();
var close = candle.ClosePrice;
var trending = hurst > HurstThreshold;
if (trending && close > sma && Position <= 0)
BuyMarket(Volume + Math.Abs(Position));
else if (trending && close < sma && Position >= 0)
SellMarket(Volume + Math.Abs(Position));
else if (Position > 0 && (close < sma || hurst < HurstThreshold))
SellMarket(Position);
else if (Position < 0 && (close > sma || hurst < HurstThreshold))
BuyMarket(-Position);
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
clr.AddReference("StockSharp.BusinessEntities")
from System import TimeSpan, Decimal
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes, Level1Fields
from StockSharp.BusinessEntities import Subscription
from StockSharp.Algo.Indicators import HurstExponent, SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class hurst_exponent_trend_strategy(Strategy):
"""
Hurst Exponent Trend strategy.
A HurstPeriod Hurst exponent above HurstThreshold marks a trending market. Then a close above the MaPeriod simple moving average goes long
and a close below it goes short, reversing an opposite position. A long closes once the close is below the average or the exponent
drops below the threshold, a short mirrors it, and a percent stop limits the loss.
"""
def __init__(self):
super(hurst_exponent_trend_strategy, self).__init__()
self._hurst_period = self.Param("HurstPeriod", 100).SetGreaterThanZero().SetDisplay("Hurst Period", "Period of the Hurst exponent", "Indicators")
self._ma_period = self.Param("MaPeriod", 20).SetGreaterThanZero().SetDisplay("MA Period", "Period of the simple moving average", "Indicators")
self._hurst_threshold = self.Param("HurstThreshold", 0.55).SetDisplay("Hurst Threshold", "Hurst exponent level of a trending market", "Indicators")
self._stop_loss_percent = self.Param("StopLossPercent", 2.0).SetNotNegative().SetDisplay("Stop Loss %", "Stop loss percentage from entry price", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Type of candles to use", "General")
@property
def candle_type(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(hurst_exponent_trend_strategy, self).OnStarted2(time)
hurst = HurstExponent()
hurst.Length = self._hurst_period.Value
sma = SimpleMovingAverage()
sma.Length = self._ma_period.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(hurst, sma, self._process_candle).Start()
self.StartProtection(Unit(), Unit(Decimal(self._stop_loss_percent.Value), UnitTypes.Percent), useMarketOrders=True, isLocalStop=True)
# The stop has to see prices between candles, not only at their close.
for field in (Level1Fields.BestBidPrice, Level1Fields.BestAskPrice):
quotes = Subscription(DataType.Level1, self.Security)
quotes.MarketData.BuildField = field
self.SubscribeLevel1(quotes).Bind(self._observe_protection_quote).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
oscillators = self.CreateChartArea()
if oscillators is not None:
self.DrawIndicator(oscillators, hurst)
def _observe_protection_quote(self, quote):
# The high-level handler activates native protection before the callback, including between bars.
pass
def _process_candle(self, candle, hurst_value, sma_value):
if candle.State != CandleStates.Finished:
return
if not hurst_value.IsFormed or not sma_value.IsFormed:
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
hurst = hurst_value.GetValue[Decimal](None)
sma = sma_value.GetValue[Decimal](None)
close = candle.ClosePrice
threshold = Decimal(self._hurst_threshold.Value)
trending = hurst > threshold
if trending and close > sma and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
elif trending and close < sma and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
elif self.Position > 0 and (close < sma or hurst < threshold):
self.SellMarket(self.Position)
elif self.Position < 0 and (close > sma or hurst < threshold):
self.BuyMarket(-self.Position)
def CreateClone(self):
return hurst_exponent_trend_strategy()