在 GitHub 上查看

Hoop Master 突破策略

概述

  • 改编自 MetaTrader 5 专家顾问 “Hoop master 2”(作者 Vladimir Karputov)。
  • 每当出现新的完整 K 线时,在价格附近同时挂出买入止损和卖出止损单。
  • 继承原策略的资金管理:亏损后将下一个循环的手数乘以系数,盈利后恢复基准手数。

交易逻辑

  1. 订阅所选的蜡烛序列,只在收到完结的蜡烛后执行逻辑。
  2. 当前无持仓时:
    • 在最新收盘价上方 IndentPips 点处挂出 买入止损单
    • 在最新收盘价下方 IndentPips 点处挂出 卖出止损单
    • 将 MetaTrader 的“点”转换为绝对价格:先取 PriceStep,若品种报价保留 3 或 5 位小数,则额外乘以 10(与 MQL 中的 m_adjusted_point 一致)。
  3. 每个挂单都会记录自己的止损与止盈。挂单成交后立即撤掉相反方向的挂单,并使用 SellStop/SellLimit(多头)或 BuyStop/BuyLimit(空头)重建保护单。
  4. 如果保护单平掉仓位,另一张附属保护单会被撤销,避免重复出场。
  5. 若启用追踪止损,当浮动收益超过 TrailingStopPips,且改进幅度高于 TrailingStepPips 时,止损会沿着盈利方向移动。
  6. 每完成一次“平仓→持仓→再平仓”的完整周期后,统计已实现盈亏:亏损则将工作手数乘以 LossMultiplier,盈利则把手数重置为基础 Volume

参数

参数 说明 默认值 备注
Volume 新建挂单使用的基础手数。 策略 Volume 属性 亏损时乘以 LossMultiplier
StopLossPips 止损距离(MT5 点)。 25 0 表示不设置止损。
TakeProfitPips 止盈距离(MT5 点)。 70 0 表示不设置止盈。
TrailingStopPips 追踪止损的固定距离。 0 设置为 0 表示关闭追踪止损。
TrailingStepPips 调整追踪止损所需的最小改进。 5 仅在启用追踪止损时使用。
IndentPips 挂单相对于价格的偏移。 15 有助于避免在噪声区间被触发。
LossMultiplier 亏损后下一循环的手数乘数。 2 复现原顾问的“翻倍加仓”逻辑。
CandleType 触发重新布置挂单的蜡烛类型/周期。 1 小时 可根据测试周期调整。

风控与保护

  • 成交后立即通过交易所原生订单重建止损和止盈,即便策略暂时离线也能获得保护。
  • 启动时调用 StartProtection(),用于清理之前可能遗留的仓位。
  • 追踪止损通过替换已有的 stop 单来实现,行为与 MT5 中修改订单的逻辑一致。

实现要点

  • 全面使用 StockSharp 高层 API:烛线订阅、BuyStop/SellStop 进场,以及 BuyLimit/SellLimit 出场。
  • 代码中的注释全部使用英文,详细说明放在多语言 README 文档中。
  • 点值换算完全遵循原始 EA:若报价为 3 或 5 位小数,将 PriceStep 乘以 10,以保持与 MetaTrader 定义一致。
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 HoopMasterBreakoutStrategy : 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 HoopMasterBreakoutStrategy()
	{
		_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;
	}
}