在 GitHub 上查看

Hoop Master 策略

概述

Hoop Master 策略是一种挂单突破系统,会在当前价格上下方同时保持买入止损和卖出止损。原始的 MetaTrader 5 专家顾问在市场上方放置买入止损、在市场下方放置卖出止损,当任一方向被触发时,另一侧订单会被取消,并立即按更大的手数重新布置。StockSharp 版本沿用了相同的思想,在同一个策略类中管理挂单以及可选的马丁加仓逻辑。

策略还可以为当前持仓附加止损和止盈保护单,并在价格沿持仓方向移动时通过追踪止损模块逐步抬高(或下移)保护单。

交易逻辑

  1. 每当一根 K 线收盘,策略都会重新计算突破挂单的价格。
  2. 若当前没有持仓,会根据设定的点差,在买卖价附近同时放置买入止损与卖出止损。
  3. 当任一挂单成交时,会取消对侧挂单,并立刻以基础手数的两倍重新提交双向突破挂单。
  4. 成交后会根据参数创建独立的止损与止盈保护单。若启用追踪止损模块,保护止损会在市场朝有利方向推进时跟随移动。
  5. 持仓平仓后,所有保护单都会被取消,并在下一次信号出现时按照基础手数重新初始化突破挂单。

参数

参数 说明
Candle Type 用于驱动逻辑的 K 线类型。
Order Volume 每个突破挂单的基础手数。马丁加仓阶段会使用该手数的两倍。
Stop Loss (pips) 入场价与止损保护单之间的点数距离,设为 0 表示不使用。
Take Profit (pips) 入场价与止盈目标单之间的点数距离,设为 0 表示不使用。
Trailing Stop (pips) 追踪止损时使用的点数距离,设为 0 表示关闭追踪。
Trailing Step (pips) 每次更新追踪止损所需的最小点数改善。
Indent (pips) 在计算突破挂单时相对买价、卖价的偏移量(点数)。

挂单管理细节

  • 策略持续跟踪买一/卖一报价,当报价缺失时,会回退到最新成交价或最近的收盘价。
  • 所有价格都会对齐到交易品种的最小报价步长,避免无效价格。
  • 保护性的止损、止盈会在检测到新持仓时立即重建。
  • 只有当追踪距离和追踪步长都大于零时才会启用追踪止损,当价格改善达到设定步长时才会移动保护单。

注意事项

  • 请确认所连接的券商或仿真环境支持止损和限价订单。
  • 马丁加仓会迅速放大仓位风险,请合理设置基础手数。
  • 策略需要同时接收 Level1 行情数据(买卖价)与 K 线数据,以便精确计算突破价格。
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;

public class HoopMasterStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public HoopMasterStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}