ChannelEA2 策略
概述
ChannelEA2 策略在 StockSharp 中复刻 MetaTrader 专家顾问 “ChannelEA2”。策略会在配置的会话开始与结束时间之间构建日内价格通道。当会话结束时,它会在通道上沿上方和下沿下方挂入场止损单,每个止损单的保护止损价都设置为通道的另一侧边界。此方法旨在捕捉会话期间盘整后的突破行情。
交易逻辑
- 当第一根完成的 K 线开盘时间跨越
BeginHour时,策略会重置当日会话:- 使用市价单平掉所有持仓。
- 取消所有活动订单(包括上一轮的入场止损单以及保护止损单)。
- 使用会话内第一根 K 线初始化通道的最高价和最低价。
- 在会话运行期间(从
BeginHour到EndHour),每根完成的 K 线都会更新通道的最高价和最低价。 - 在会话结束后的第一根 K 线开盘时(
EndHour之后),策略会:- 在会话最高价之上(可选价差缓冲)挂出买入止损单;
- 在会话最低价之下(同样的缓冲)挂出卖出止损单;
- 买入订单的保护止损设为会话最低价,卖出订单的保护止损设为会话最高价。
- 一旦有方向被触发形成持仓,策略会取消相反方向的入场订单,并根据记录的止损价注册保护止损单。
- 这些订单会一直保留到下一次会话开始时,届时所有仓位和订单都会被重新清理。
参数
| 名称 | 描述 | 默认值 |
|---|---|---|
BeginHour |
会话重置并开始收集数据的小时(0-23)。 | 1 |
EndHour |
安排挂入止损单的小时(0-23),当 BeginHour > EndHour 时可跨夜。 |
10 |
TradeVolume |
每笔入场订单使用的成交量。 | 1 |
CandleType |
构建通道所用的 K 线类型(默认 1 小时)。 | 1 小时 |
StopBufferMultiplier |
以价格最小变动为单位的缓冲倍数,用于入场触发价和保护止损。 | 2 |
风险控制
- 策略会自动调用
StartProtection(),让 StockSharp 处理意外持仓。 - 保护止损单会在出现持仓后立即提交,当仓位归零时自动取消。
- 止损价格会按照
StopBufferMultiplier * PriceStep偏移,避免触犯交易所的最小止损距离限制。
其他说明
- 一旦生成入场止损单,通道高低值会被冻结,直到下一次会话开始才重新计算。
- 如果标的没有定义
PriceStep,则缓冲忽略,止损单直接挂在通道价格。 TradeVolume支持小数,方便在允许的市场中使用分数手或手数。- 策略会在图表区域绘制 K 线和成交记录,便于可视化跟踪。
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Breakout strategy that places stop orders at the extremes of the intraday channel.
/// </summary>
public class ChannelEa2Strategy : Strategy
{
private readonly StrategyParam<int> _beginHour;
private readonly StrategyParam<int> _endHour;
private readonly StrategyParam<decimal> _tradeVolume;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<decimal> _stopBufferMultiplier;
private decimal? _sessionHigh;
private decimal? _sessionLow;
private bool _channelReady;
private decimal? _entryPrice;
private decimal? _stopLossPrice;
/// <summary>
/// Trading session start hour.
/// </summary>
public int BeginHour
{
get => _beginHour.Value;
set => _beginHour.Value = value;
}
/// <summary>
/// Trading session end hour.
/// </summary>
public int EndHour
{
get => _endHour.Value;
set => _endHour.Value = value;
}
/// <summary>
/// Order volume.
/// </summary>
public decimal TradeVolume
{
get => _tradeVolume.Value;
set => _tradeVolume.Value = value;
}
/// <summary>
/// Candle type used for channel detection.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Number of price steps added as a buffer to entry and protective orders.
/// </summary>
public decimal StopBufferMultiplier
{
get => _stopBufferMultiplier.Value;
set => _stopBufferMultiplier.Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="ChannelEa2Strategy"/> class.
/// </summary>
public ChannelEa2Strategy()
{
_beginHour = Param(nameof(BeginHour), 1)
.SetDisplay("Begin Hour", "Hour when the session resets", "Trading")
.SetOptimize(0, 23, 1);
_endHour = Param(nameof(EndHour), 10)
.SetDisplay("End Hour", "Hour when breakout orders are scheduled", "Trading")
.SetOptimize(0, 23, 1);
_tradeVolume = Param(nameof(TradeVolume), 1m)
.SetDisplay("Volume", "Order volume", "Trading")
.SetGreaterThanZero();
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Time frame used for the channel", "General");
_stopBufferMultiplier = Param(nameof(StopBufferMultiplier), 2m)
.SetDisplay("Stop Buffer", "Price step multiplier for safety offsets", "Risk")
.SetNotNegative();
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_sessionHigh = null;
_sessionLow = null;
_channelReady = false;
_entryPrice = null;
_stopLossPrice = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
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;
var hour = candle.OpenTime.Hour;
// During channel-building hours, accumulate the range.
if (hour >= BeginHour && hour < EndHour)
{
if (_sessionHigh is null || candle.HighPrice > _sessionHigh)
_sessionHigh = candle.HighPrice;
if (_sessionLow is null || candle.LowPrice < _sessionLow)
_sessionLow = candle.LowPrice;
_channelReady = true;
return;
}
// Outside the channel window, attempt breakout entries.
if (!_channelReady || _sessionHigh is not decimal high || _sessionLow is not decimal low || high <= low)
return;
var buffer = GetPriceBuffer();
// Manage existing positions.
if (Position > 0)
{
if (_stopLossPrice.HasValue && candle.LowPrice <= _stopLossPrice.Value)
{
SellMarket(Position);
_entryPrice = null;
_stopLossPrice = null;
}
return;
}
else if (Position < 0)
{
if (_stopLossPrice.HasValue && candle.HighPrice >= _stopLossPrice.Value)
{
BuyMarket(Math.Abs(Position));
_entryPrice = null;
_stopLossPrice = null;
}
return;
}
// Long breakout: price exceeds channel high.
if (candle.HighPrice > high + buffer)
{
BuyMarket(TradeVolume > 0 ? TradeVolume : Volume);
_entryPrice = candle.ClosePrice;
_stopLossPrice = low - buffer;
// Reset channel for next session.
_sessionHigh = null;
_sessionLow = null;
_channelReady = false;
return;
}
// Short breakout: price drops below channel low.
if (candle.LowPrice < low - buffer)
{
SellMarket(TradeVolume > 0 ? TradeVolume : Volume);
_entryPrice = candle.ClosePrice;
_stopLossPrice = high + buffer;
_sessionHigh = null;
_sessionLow = null;
_channelReady = false;
}
}
private decimal GetPriceBuffer()
{
var step = Security?.PriceStep ?? 0m;
if (step <= 0m || StopBufferMultiplier <= 0m)
return 0m;
return step * StopBufferMultiplier;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from StockSharp.Algo.Strategies import Strategy
from StockSharp.Messages import DataType, CandleStates
from System import TimeSpan, Math
class channel_ea2_strategy(Strategy):
def __init__(self):
super(channel_ea2_strategy, self).__init__()
self._begin_hour = self.Param("BeginHour", 1)
self._end_hour = self.Param("EndHour", 10)
self._trade_volume = self.Param("TradeVolume", 1.0)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._stop_buffer_multiplier = self.Param("StopBufferMultiplier", 2.0)
self._session_high = None
self._session_low = None
self._channel_ready = False
self._entry_price = None
self._stop_loss_price = None
@property
def CandleType(self):
return self._candle_type.Value
def OnStarted2(self, time):
super(channel_ea2_strategy, self).OnStarted2(time)
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(self._process_candle).Start()
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
hour = candle.OpenTime.Hour
if hour >= self._begin_hour.Value and hour < self._end_hour.Value:
h = float(candle.HighPrice)
lo = float(candle.LowPrice)
if self._session_high is None or h > self._session_high:
self._session_high = h
if self._session_low is None or lo < self._session_low:
self._session_low = lo
self._channel_ready = True
return
if not self._channel_ready or self._session_high is None or self._session_low is None:
return
high = self._session_high
low = self._session_low
if high <= low:
return
buffer = self._get_price_buffer()
if self.Position > 0:
if self._stop_loss_price is not None and float(candle.LowPrice) <= self._stop_loss_price:
self.SellMarket(self.Position)
self._entry_price = None
self._stop_loss_price = None
return
elif self.Position < 0:
if self._stop_loss_price is not None and float(candle.HighPrice) >= self._stop_loss_price:
self.BuyMarket(abs(self.Position))
self._entry_price = None
self._stop_loss_price = None
return
if float(candle.HighPrice) > high + buffer:
vol = self._trade_volume.Value if self._trade_volume.Value > 0 else float(self.Volume)
self.BuyMarket(vol)
self._entry_price = float(candle.ClosePrice)
self._stop_loss_price = low - buffer
self._session_high = None
self._session_low = None
self._channel_ready = False
return
if float(candle.LowPrice) < low - buffer:
vol = self._trade_volume.Value if self._trade_volume.Value > 0 else float(self.Volume)
self.SellMarket(vol)
self._entry_price = float(candle.ClosePrice)
self._stop_loss_price = high + buffer
self._session_high = None
self._session_low = None
self._channel_ready = False
def _get_price_buffer(self):
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 0.0
if step <= 0 or self._stop_buffer_multiplier.Value <= 0:
return 0.0
return step * self._stop_buffer_multiplier.Value
def OnReseted(self):
super(channel_ea2_strategy, self).OnReseted()
self._session_high = None
self._session_low = None
self._channel_ready = False
self._entry_price = None
self._stop_loss_price = None
def CreateClone(self):
return channel_ea2_strategy()