在 GitHub 上查看

MH Hull Moving Average Based Trading

基于Hull移动平均的突破策略。

策略比较开盘价与Hull移动平均生成的动态水平。当价格突破上方水平时做多,跌破下方水平时做空。相反方向的突破将平掉当前仓位。

详情

  • 入场条件:价格相对于Hull MA水平。
  • 多/空:双向。
  • 出场条件:相反突破。
  • 止损:无。
  • 默认值
    • HullPeriod = 210
    • CandleType = TimeSpan.FromMinutes(5)
  • 筛选
    • 类别:趋势
    • 方向:双向
    • 指标:MA
    • 止损:无
    • 复杂度:基础
    • 时间框架:日内 (5m)
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// HMA cross strategy with threshold and cooldown to reduce signal noise.
/// </summary>
public class MhHullMovingAverageBasedTradingStrategy : Strategy
{
	private readonly StrategyParam<int> _hullPeriod;
	private readonly StrategyParam<decimal> _signalThresholdPercent;
	private readonly StrategyParam<int> _signalCooldownBars;
	private readonly StrategyParam<DataType> _candleType;

	private HullMovingAverage _hma;
	private decimal _prevDiffPercent;
	private bool _hasPrevDiff;
	private int _barsFromSignal;

	/// <summary>
	/// Period for Hull Moving Average.
	/// </summary>
	public int HullPeriod
	{
		get => _hullPeriod.Value;
		set => _hullPeriod.Value = value;
	}

	/// <summary>
	/// Minimum price to HMA distance in percent required for a signal.
	/// </summary>
	public decimal SignalThresholdPercent
	{
		get => _signalThresholdPercent.Value;
		set => _signalThresholdPercent.Value = value;
	}

	/// <summary>
	/// Minimum bars between entries.
	/// </summary>
	public int SignalCooldownBars
	{
		get => _signalCooldownBars.Value;
		set => _signalCooldownBars.Value = value;
	}

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

	/// <summary>
	/// Initialize the strategy.
	/// </summary>
	public MhHullMovingAverageBasedTradingStrategy()
	{
		_hullPeriod = Param(nameof(HullPeriod), 120)
			.SetGreaterThanZero()
			.SetDisplay("Hull Period", "Period for Hull Moving Average", "Indicators");

		_signalThresholdPercent = Param(nameof(SignalThresholdPercent), 0.15m)
			.SetGreaterThanZero()
			.SetDisplay("Signal Threshold %", "Minimum distance from HMA", "Indicators");

		_signalCooldownBars = Param(nameof(SignalCooldownBars), 10)
			.SetGreaterThanZero()
			.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).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 OnReseted()
	{
		base.OnReseted();
		_hma = null;
		_prevDiffPercent = 0m;
		_hasPrevDiff = false;
		_barsFromSignal = 0;
	}

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

		_hma = new HullMovingAverage { Length = HullPeriod };
		_prevDiffPercent = 0m;
		_hasPrevDiff = false;
		_barsFromSignal = SignalCooldownBars;

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(_hma, ProcessCandle).Start();
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (!_hma.IsFormed)
			return;

		var price = candle.ClosePrice;
		if (price <= 0m)
			return;

		var diffPercent = (price - hmaValue) / price * 100m;
		var threshold = SignalThresholdPercent;
		var crossedUp = _hasPrevDiff && _prevDiffPercent <= threshold && diffPercent > threshold;
		var crossedDown = _hasPrevDiff && _prevDiffPercent >= -threshold && diffPercent < -threshold;

		_prevDiffPercent = diffPercent;
		_hasPrevDiff = true;

		_barsFromSignal++;
		if (_barsFromSignal < SignalCooldownBars)
			return;

		if (crossedUp && Position <= 0)
		{
			var volume = Volume + Math.Abs(Position);
			BuyMarket(volume);
			_barsFromSignal = 0;
		}
		else if (crossedDown && Position >= 0)
		{
			var volume = Volume + Math.Abs(Position);
			SellMarket(volume);
			_barsFromSignal = 0;
		}
	}
}