在 GitHub 上查看

EURUSD Session Breakout 策略

该策略复刻了经典的欧/美盘突破思路:利用欧洲早盘的窄幅波动,为美盘提供突破信号。系统使用 24 根滚动的 K 线窗口(默认 15 分钟),计算美盘前的区间,过滤掉波动超出可配置点数阈值的交易日,然后只在价格完全 突破该区间时入场。每天最多允许一次做多尝试和一次做空尝试。

工作流程

  1. 区间锁定:在设定的美盘开始小时,策略会锁定最近 24 根已完成 K 线(不含当前 K 线)的最高价和最低价。 对于 3/5 位小数的外汇报价,会自动换算成标准点值。
  2. 区间过滤:只有当锁定的欧盘区间小于 Small EU Session (pips) 参数时,才允许继续寻找信号。
  3. 突破确认:在允许的美盘交易时间内,并且只在 (EU start hour + 5)(EU start hour + 10) 之间,策略 会检查整根 K 线是否完全突破了区间,同时需要额外的 points 缓冲。
  4. 下单逻辑:当整根 K 线的最低价高于区间上沿加缓冲时,市价买入;当最高价低于区间下沿减缓冲时,市价卖出。 做多和做空标记互不影响,因此每天两个方向各自只会触发一次。
  5. 风险控制:止损和止盈以点数表示,自动换算成绝对价格距离,并在每根完成的 K 线上根据最高/最低价进行检查。

参数说明

  • EU Session Start / US Session Start / US Session End:指定欧盘监控开始时间以及美盘可交易时间窗口(0–23 时)。
  • Small EU Session (pips):允许交易的欧盘区间最大点数。
  • Trade On Monday:是否允许周一交易,周末始终被屏蔽。
  • Stop Loss (pips):进场到止损的点数距离,会根据报价的最小跳动值自动缩放。
  • Take Profit (pips):进场到止盈的点数距离,计算方式与止损一致。
  • Breakout Buffer (points):突破判断时额外要求的价格跳动数,确保整根 K 线完全脱离区间。
  • Candle Type:订阅的 K 线类型,默认使用 15 分钟,因为原始脚本针对 M15 图表。

其它说明

  • 策略基于净仓模式实现,触发保护条件时使用市价单直接平掉全部仓位。
  • 每天午夜都会重置内部状态,避免区间或交易标记跨日遗留;若有持仓,会保留相应的止损和止盈价格。
  • 止损和止盈是通过已完成 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;

/// <summary>
/// Breakout strategy that trades after a tight consolidation range.
/// Captures the range from a "quiet" session, then trades breakouts in the following session.
/// </summary>
public class EurUsdSessionBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _euSessionLengthBars;
	private readonly StrategyParam<int> _startHourRangeSession;
	private readonly StrategyParam<int> _startHourTradeSession;
	private readonly StrategyParam<int> _endHourTradeSession;
	private readonly StrategyParam<decimal> _smallSessionThreshold;
	private readonly StrategyParam<decimal> _stopLossDistance;
	private readonly StrategyParam<decimal> _takeProfitDistance;
	private readonly StrategyParam<decimal> _breakoutBuffer;
	private readonly StrategyParam<DataType> _candleType;

	private Highest _highest = null!;
	private Lowest _lowest = null!;
	private decimal _currentHighest;
	private decimal _currentLowest;
	private decimal _entryPrice;
	private decimal _stopPrice;
	private decimal _takePrice;
	private DateTime _currentDate;

	public EurUsdSessionBreakoutStrategy()
	{
		_startHourRangeSession = Param(nameof(StartHourRangeSession), 0)
			.SetDisplay("Range Session Start", "Start hour of the consolidation range session", "Schedule");

		_startHourTradeSession = Param(nameof(StartHourTradeSession), 8)
			.SetDisplay("Trade Session Start", "Start hour of the trading session", "Schedule");

		_endHourTradeSession = Param(nameof(EndHourTradeSession), 20)
			.SetDisplay("Trade Session End", "End hour of the trading session", "Schedule");

		_smallSessionThreshold = Param(nameof(SmallSessionThreshold), 200m)
			.SetDisplay("Small Session Threshold", "Maximum range session price range to trigger trading", "Risk");

		_stopLossDistance = Param(nameof(StopLossDistance), 5m)
			.SetDisplay("Stop Loss Distance", "Stop loss distance in price units", "Risk");

		_takeProfitDistance = Param(nameof(TakeProfitDistance), 8m)
			.SetDisplay("Take Profit Distance", "Take profit distance in price units", "Risk");

		_breakoutBuffer = Param(nameof(BreakoutBuffer), 0m)
			.SetDisplay("Breakout Buffer", "Extra price buffer added to breakout trigger", "Entries");

		_euSessionLengthBars = Param(nameof(EuSessionLengthBars), 10)
			.SetRange(1, 72)
			.SetDisplay("Range Session Length (bars)", "Number of bars representing the range session", "Schedule");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type used for calculations", "General");
	}

	public int StartHourRangeSession
	{
		get => _startHourRangeSession.Value;
		set => _startHourRangeSession.Value = value;
	}

	public int StartHourTradeSession
	{
		get => _startHourTradeSession.Value;
		set => _startHourTradeSession.Value = value;
	}

	public int EndHourTradeSession
	{
		get => _endHourTradeSession.Value;
		set => _endHourTradeSession.Value = value;
	}

	public decimal SmallSessionThreshold
	{
		get => _smallSessionThreshold.Value;
		set => _smallSessionThreshold.Value = value;
	}

	public decimal StopLossDistance
	{
		get => _stopLossDistance.Value;
		set => _stopLossDistance.Value = value;
	}

	public decimal TakeProfitDistance
	{
		get => _takeProfitDistance.Value;
		set => _takeProfitDistance.Value = value;
	}

	public decimal BreakoutBuffer
	{
		get => _breakoutBuffer.Value;
		set => _breakoutBuffer.Value = value;
	}

	public int EuSessionLengthBars
	{
		get => _euSessionLengthBars.Value;
		set => _euSessionLengthBars.Value = value;
	}

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_highest = null!;
		_lowest = null!;
		_currentHighest = 0;
		_currentLowest = 0;
		_entryPrice = 0;
		_stopPrice = 0;
		_takePrice = 0;
		_currentDate = default;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_highest = new Highest { Length = EuSessionLengthBars };
		_lowest = new Lowest { Length = EuSessionLengthBars };

		_currentDate = time.Date;

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

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
		}
	}

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

		// Manage protective exits first
		ManageActivePosition(candle);

		// Update rolling highest/lowest
		var previousHighest = _currentHighest;
		var previousLowest = _currentLowest;

		_currentHighest = _highest.Process(candle).ToDecimal();
		_currentLowest = _lowest.Process(candle).ToDecimal();

		if (!_highest.IsFormed || !_lowest.IsFormed)
			return;

		if (previousHighest <= 0 || previousLowest <= 0)
			return;

		if (Position != 0)
			return;

		// Breakout above previous rolling highest
		if (candle.ClosePrice > previousHighest + BreakoutBuffer)
		{
			BuyMarket();
			SetLongTargets(candle.ClosePrice);
		}
		// Breakout below previous rolling lowest
		else if (candle.ClosePrice < previousLowest - BreakoutBuffer)
		{
			SellMarket();
			SetShortTargets(candle.ClosePrice);
		}
	}

	private void ManageActivePosition(ICandleMessage candle)
	{
		if (Position == 0)
			return;

		if (Position > 0)
		{
			var exitByStop = StopLossDistance > 0m && candle.LowPrice <= _stopPrice;
			var exitByTake = TakeProfitDistance > 0m && candle.HighPrice >= _takePrice;

			if (exitByStop || exitByTake)
			{
				SellMarket();
				ClearTargets();
			}
		}
		else if (Position < 0)
		{
			var exitByStop = StopLossDistance > 0m && candle.HighPrice >= _stopPrice;
			var exitByTake = TakeProfitDistance > 0m && candle.LowPrice <= _takePrice;

			if (exitByStop || exitByTake)
			{
				BuyMarket();
				ClearTargets();
			}
		}
	}

	private void SetLongTargets(decimal entryPrice)
	{
		_entryPrice = entryPrice;
		_stopPrice = entryPrice - StopLossDistance;
		_takePrice = entryPrice + TakeProfitDistance;
	}

	private void SetShortTargets(decimal entryPrice)
	{
		_entryPrice = entryPrice;
		_stopPrice = entryPrice + StopLossDistance;
		_takePrice = entryPrice - TakeProfitDistance;
	}

	private void ClearTargets()
	{
		_entryPrice = 0m;
		_stopPrice = 0m;
		_takePrice = 0m;
	}

	private void ResetDailyState(DateTime date)
	{
		_currentDate = date;

		if (Position == 0)
			ClearTargets();
	}
}