伦敦突破经典策略
该策略利用亚洲时段的高低点构建区间,在伦敦时段突破时入场。00:00 至 06:55 UTC 的最高价和最低价形成箱体。07:00 UTC 之后,价格突破上沿则做多,突破下沿则做空。止损设在箱体中点,止盈按可调的风险回报比计算。
细节
- 入场条件:
- 多头:价格突破亚洲时段高点。
- 空头:价格跌破亚洲时段低点。
- 出场条件:
- 止损或止盈。
- 交易窗口结束。
- 止损: 有。
- 默认值:
- 亚洲时段:00:00–06:55 UTC。
- 交易时段:07:00–16:00 UTC。
- CRV = 1。
- 过滤器:
- 类型:突破
- 方向:双向
- 指标:无
- 止损:有
- 复杂度:中等
- 时间框架:日内
- 季节性:无
- 神经网络:无
- 背离:无
- 风险等级:中等
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// London session breakout strategy using Highest/Lowest channels.
/// </summary>
public class LondonBreakOutClassicStrategy : Strategy
{
private readonly StrategyParam<int> _channelLength;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _cooldownBars;
private Highest _highest;
private Lowest _lowest;
private decimal _prevHigh;
private decimal _prevLow;
private int _barsSinceSignal;
public int ChannelLength { get => _channelLength.Value; set => _channelLength.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }
public LondonBreakOutClassicStrategy()
{
_channelLength = Param(nameof(ChannelLength), 20)
.SetDisplay("Channel Length", "Period for breakout channel", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Candles", "General");
_cooldownBars = Param(nameof(CooldownBars), 20)
.SetDisplay("Cooldown Bars", "Min bars between signals", "General");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_highest = null;
_lowest = null;
_prevHigh = 0;
_prevLow = 0;
_barsSinceSignal = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_highest = new Highest { Length = ChannelLength };
_lowest = new Lowest { Length = ChannelLength };
_prevHigh = 0;
_prevLow = 0;
_barsSinceSignal = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_highest, _lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal highVal, decimal lowVal)
{
if (candle.State != CandleStates.Finished)
return;
_barsSinceSignal++;
if (!_highest.IsFormed || !_lowest.IsFormed)
{
_prevHigh = highVal;
_prevLow = lowVal;
return;
}
if (_barsSinceSignal < CooldownBars)
{
_prevHigh = highVal;
_prevLow = lowVal;
return;
}
// Breakout above channel high
if (candle.ClosePrice > _prevHigh && Position <= 0)
{
BuyMarket(Volume + Math.Abs(Position));
_barsSinceSignal = 0;
}
// Breakout below channel low
else if (candle.ClosePrice < _prevLow && Position >= 0)
{
SellMarket(Volume + Math.Abs(Position));
_barsSinceSignal = 0;
}
_prevHigh = highVal;
_prevLow = lowVal;
}
}
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
from StockSharp.Algo.Strategies import Strategy
class london_breakout_classic_strategy(Strategy):
def __init__(self):
super(london_breakout_classic_strategy, self).__init__()
self._channel_length = self.Param("ChannelLength", 20) \
.SetDisplay("Channel Length", "Period for breakout channel", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Candles", "General")
self._cooldown_bars = self.Param("CooldownBars", 20) \
.SetDisplay("Cooldown Bars", "Min bars between signals", "General")
self._prev_high = 0.0
self._prev_low = 0.0
self._bars_since_signal = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(london_breakout_classic_strategy, self).OnReseted()
self._prev_high = 0.0
self._prev_low = 0.0
self._bars_since_signal = 0
def OnStarted2(self, time):
super(london_breakout_classic_strategy, self).OnStarted2(time)
self._prev_high = 0.0
self._prev_low = 0.0
self._bars_since_signal = 0
self._highest = Highest()
self._highest.Length = self._channel_length.Value
self._lowest = Lowest()
self._lowest.Length = self._channel_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._highest, self._lowest, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def OnProcess(self, candle, high_val, low_val):
if candle.State != CandleStates.Finished:
return
self._bars_since_signal += 1
hv = float(high_val)
lv = float(low_val)
if not self._highest.IsFormed or not self._lowest.IsFormed:
self._prev_high = hv
self._prev_low = lv
return
if self._bars_since_signal < self._cooldown_bars.Value:
self._prev_high = hv
self._prev_low = lv
return
close = float(candle.ClosePrice)
if close > self._prev_high and self.Position <= 0:
self.BuyMarket(self.Volume + abs(self.Position))
self._bars_since_signal = 0
elif close < self._prev_low and self.Position >= 0:
self.SellMarket(self.Volume + abs(self.Position))
self._bars_since_signal = 0
self._prev_high = hv
self._prev_low = lv
def CreateClone(self):
return london_breakout_classic_strategy()