EURUSD Session Breakout 策略
该策略复刻了经典的欧/美盘突破思路:利用欧洲早盘的窄幅波动,为美盘提供突破信号。系统使用 24 根滚动的 K 线窗口(默认 15 分钟),计算美盘前的区间,过滤掉波动超出可配置点数阈值的交易日,然后只在价格完全 突破该区间时入场。每天最多允许一次做多尝试和一次做空尝试。
工作流程
- 区间锁定:在设定的美盘开始小时,策略会锁定最近 24 根已完成 K 线(不含当前 K 线)的最高价和最低价。 对于 3/5 位小数的外汇报价,会自动换算成标准点值。
- 区间过滤:只有当锁定的欧盘区间小于 Small EU Session (pips) 参数时,才允许继续寻找信号。
- 突破确认:在允许的美盘交易时间内,并且只在
(EU start hour + 5)到(EU start hour + 10)之间,策略 会检查整根 K 线是否完全突破了区间,同时需要额外的 points 缓冲。 - 下单逻辑:当整根 K 线的最低价高于区间上沿加缓冲时,市价买入;当最高价低于区间下沿减缓冲时,市价卖出。 做多和做空标记互不影响,因此每天两个方向各自只会触发一次。
- 风险控制:止损和止盈以点数表示,自动换算成绝对价格距离,并在每根完成的 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();
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import Highest, Lowest, CandleIndicatorValue
from StockSharp.Algo.Strategies import Strategy
class eur_usd_session_breakout_strategy(Strategy):
"""
Session breakout strategy using rolling highest/lowest channels.
Buys on breakout above rolling highest, sells on breakout below rolling lowest.
Manual SL/TP management.
"""
def __init__(self):
super(eur_usd_session_breakout_strategy, self).__init__()
self._eu_session_length = self.Param("EuSessionLengthBars", 10) \
.SetDisplay("Range Session Length", "Number of bars for range", "Schedule")
self._stop_loss_dist = self.Param("StopLossDistance", 5.0) \
.SetDisplay("Stop Loss Distance", "Stop loss in price units", "Risk")
self._take_profit_dist = self.Param("TakeProfitDistance", 8.0) \
.SetDisplay("Take Profit Distance", "Take profit in price units", "Risk")
self._breakout_buffer = self.Param("BreakoutBuffer", 0.0) \
.SetDisplay("Breakout Buffer", "Extra buffer for breakout trigger", "Entries")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle type", "General")
self._highest = None
self._lowest = None
self._current_highest = 0.0
self._current_lowest = 0.0
self._entry_price = 0.0
self._stop_price = 0.0
self._take_price = 0.0
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(eur_usd_session_breakout_strategy, self).OnReseted()
self._highest = None
self._lowest = None
self._current_highest = 0.0
self._current_lowest = 0.0
self._entry_price = 0.0
self._stop_price = 0.0
self._take_price = 0.0
def OnStarted2(self, time):
super(eur_usd_session_breakout_strategy, self).OnStarted2(time)
self._highest = Highest()
self._highest.Length = self._eu_session_length.Value
self._lowest = Lowest()
self._lowest.Length = self._eu_session_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
self._manage_position(candle)
prev_highest = self._current_highest
prev_lowest = self._current_lowest
h_result = self._highest.Process(CandleIndicatorValue(self._highest, candle))
l_result = self._lowest.Process(CandleIndicatorValue(self._lowest, candle))
self._current_highest = float(h_result) if not h_result.IsEmpty else self._current_highest
self._current_lowest = float(l_result) if not l_result.IsEmpty else self._current_lowest
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
if prev_highest <= 0 or prev_lowest <= 0:
return
if self.Position != 0:
return
close = float(candle.ClosePrice)
buf = float(self._breakout_buffer.Value)
if close > prev_highest + buf:
self.BuyMarket()
self._set_long_targets(close)
elif close < prev_lowest - buf:
self.SellMarket()
self._set_short_targets(close)
def _manage_position(self, candle):
if self.Position == 0:
return
low = float(candle.LowPrice)
high = float(candle.HighPrice)
sl_dist = float(self._stop_loss_dist.Value)
tp_dist = float(self._take_profit_dist.Value)
if self.Position > 0:
exit_stop = sl_dist > 0 and low <= self._stop_price
exit_take = tp_dist > 0 and high >= self._take_price
if exit_stop or exit_take:
self.SellMarket()
self._clear_targets()
elif self.Position < 0:
exit_stop = sl_dist > 0 and high >= self._stop_price
exit_take = tp_dist > 0 and low <= self._take_price
if exit_stop or exit_take:
self.BuyMarket()
self._clear_targets()
def _set_long_targets(self, price):
self._entry_price = price
self._stop_price = price - float(self._stop_loss_dist.Value)
self._take_price = price + float(self._take_profit_dist.Value)
def _set_short_targets(self, price):
self._entry_price = price
self._stop_price = price + float(self._stop_loss_dist.Value)
self._take_price = price - float(self._take_profit_dist.Value)
def _clear_targets(self):
self._entry_price = 0.0
self._stop_price = 0.0
self._take_price = 0.0
def CreateClone(self):
return eur_usd_session_breakout_strategy()