在 GitHub 上查看

Three Breaky 策略

Three Breaky 策略是 MetaTrader 4 专家顾问 ThreeBreaky_v1.mq4 的完整移植版本。StockSharp 实现保留了原始的三个突破子系统,将蜡烛图驱动的逻辑迁移到高级 API,并为每个模块维护独立的持仓状态。策略仅依赖一个可配置的时间框架,可以根据需要单独启用或关闭任意子系统。

交易子系统

  1. System 1 – ATR 扩张突破

    • 只读取上一根完成的蜡烛。
    • 当上一根蜡烛收阳且高低价差大于 72 周期 ATR 的四倍时做多。
    • 当上一根蜡烛收阴且满足同样的价差条件时做空。
  2. System 2 – Ichimoku 云突破

    • 使用默认周期 9/26/52 的 Senkou Span A 和 Senkou Span B。
    • 如果前两根蜡烛收盘价均低于两条云带,而上一根蜡烛收盘价重新站上两条云带,则触发做多信号。
    • 如果前两根蜡烛收盘价均高于云带,而上一根蜡烛跌破两条云带,则触发做空信号。
  3. System 3 – 特大实体突破

    • 维护最近 20 根完成蜡烛的实体长度。
    • 当上一根蜡烛收阳且实体长度超过历史最大实体的三倍时做多。
    • 当上一根蜡烛收阴且实体长度超过三倍阈值时做空。

每个子系统都拥有独立的虚拟仓位。系统会记录最后一次买入或卖出的时间戳,以确保在同一根蜡烛内只开仓一次,这一点与原版的 buyTag/sellTag 控制逻辑一致。

离场规则

  • Parabolic SAR 反向:三个子系统共用参数为 0.005/0.2 的抛物线 SAR。当价格在最近两根蜡烛之间穿越 SAR 时,相应仓位立即平仓。
  • 风险管理:可选的止损和止盈(以点为单位)在每根完成蜡烛上检查,一旦触发立即平仓。

使用的指标

  • 72 周期的 Average True Range,用于衡量平均波动。
  • Ichimoku Kinko Hyo(9、26、52),用于云层翻转判断。
  • Parabolic SAR(0.005, 0.2),作为共用的退出和跟踪指标。
  • 20 根蜡烛的实体长度缓冲区,用来复制原策略中的最大实体比较。

参数

参数 说明
UseSystem1 是否启用 ATR 扩张突破模块。
UseSystem2 是否启用 Ichimoku 云翻转模块。
UseSystem3 是否启用特大实体突破模块。
OrderVolume 每次下单使用的交易量。
StopLossPips 止损距离(点)。设为 0 表示关闭。
TakeProfitPips 止盈距离(点)。设为 0 表示关闭。
CandleType 工作蜡烛的时间框架(默认 1 小时)。

工作流程

  1. 订阅所选时间框架的蜡烛,仅处理收盘完成的蜡烛。
  2. 更新 ATR、Ichimoku、Parabolic SAR 指标以及实体长度历史。
  3. 检查并关闭触发止损、止盈或 SAR 反向的仓位。
  4. 当允许交易时,分别评估三个子系统的条件,并在条件满足时发送市价单。
  5. 保存最新的指标值,为下一根蜡烛提供与原始 MQL 策略一致的历史数据。

其他说明

  • 策略根据品种的最小报价步长计算点值,对五位和三位小数报价分别归一化到四位和两位小数。
  • 各子系统可以同时持仓,每个子系统都维护自身的方向、开仓价和最近信号时间,对应原策略中的不同 MagicNumber
  • 通过比较蜡烛的开盘时间与上一次下单时间,策略保证每个子系统在同一根蜡烛中只开仓一次。
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 ThreeBreakyStrategy : 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 ThreeBreakyStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 15).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 60).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;
	}
}