支撑阻力突破策略
概述
该策略复刻了 MetaTrader 上的 “SupportResistTrade” 专家顾问,通过 Donchian 通道确定最近的支撑与阻力,并结合长期 EMA 趋势过滤器来筛选方向。只有当价格突破区间边界并且 K 线开盘价位于 EMA 的同侧时才会开仓。策略使用即时保护性止损以及三阶逐步移动止损(+10/+20/+30 点)来管理风险。
数据与指标
- 主要数据源: 单一 K 线订阅(默认 1 分钟,可通过
CandleType参数调整)。 - 支撑/阻力:
DonchianChannels,长度为RangeLength(默认 55),用于追踪最近区间的最高价与最低价。 - 趋势过滤:
ExponentialMovingAverage,周期为EmaPeriod(默认 500),基于 K 线开盘价计算。只有当开盘价位于 EMA 上方/下方时才允许做多/做空。
交易逻辑
- 市场分析: 每根已完成 K 线都会更新 Donchian 区间与 EMA。上轨视为阻力,下轨视为支撑。
- 入场条件:
- 做多: K 线收盘价突破阻力,且开盘价高于 EMA。若持有空单则先平仓,再市价开多。
- 做空: K 线收盘价跌破支撑,且开盘价低于 EMA。若持有多单则先平仓,再市价开空。
- 初始止损: 成交后立即在最新支撑(多单)或最新阻力(空单)放置止损单,对应原 EA 的
OrderSend参数。 - 离场规则:
- 当持仓已有盈利且收盘价回到更新后的支撑/阻力另一侧时,立即市价平仓,与原策略的
OrderClose条件一致。 - 保护性止损始终挂单生效,以防突然的反向波动。
- 当持仓已有盈利且收盘价回到更新后的支撑/阻力另一侧时,立即市价平仓,与原策略的
逐步移动止损
策略严格按照 EA 的三次 OrderModify 调整止损:
| 盈利阈值(点) | 新的止损位置(相对开仓点数) | 说明 |
| --- | --- | --- |
| >= 20 | 10 | 多单止损抬高到开仓价 +10 点;空单止损下移到开仓价 −10 点。 |
| >= 40 | 20 | 止损进一步移动到开仓价 ±20 点。 |
| >= 60 | 30 | 最后一步锁定 30 点利润。 |
止损从不反向移动:多单止损只会上移,空单止损只会下移,确保盈利被逐步锁定。
风险控制
- 保护性止损通过原生
SellStop/BuyStop订单实现,即使策略暂时离线也能由券商执行。 - 策略按净头寸运行,每次新信号都会先平掉反向仓位,再建立新的方向。
参数
| 参数 | 默认值 | 说明 |
|---|---|---|
RangeLength |
55 |
计算支撑(最低价)与阻力(最高价)所用的 K 线数量。 |
EmaPeriod |
500 |
基于开盘价计算的 EMA 趋势过滤周期。 |
CandleType |
1 分钟 |
所有指标使用的 K 线类型,可自由切换。 |
说明
- 实现完全基于 StockSharp 高级 API,通过指标绑定与 K 线订阅完成全部计算。
- 当前仅提供 C# 版本,实现位于
CS文件夹;未创建 Python 版本或相应目录。
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>
/// Support and resistance breakout strategy with EMA trend filter.
/// Buys above resistance during bullish trends and sells below support during bearish trends.
/// Manually tracks highest high and lowest low over N candles for support/resistance.
/// </summary>
public class SupportResistanceBreakoutStrategy : Strategy
{
private readonly StrategyParam<int> _rangeLength;
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<decimal> _stopLossPoints;
private readonly StrategyParam<decimal> _takeProfitPoints;
private readonly StrategyParam<DataType> _candleType;
private ExponentialMovingAverage _ema;
private readonly List<decimal> _highs = new();
private readonly List<decimal> _lows = new();
private decimal _support;
private decimal _resistance;
private decimal? _entryPrice;
/// <summary>
/// Number of candles used to compute support and resistance.
/// </summary>
public int RangeLength
{
get => _rangeLength.Value;
set => _rangeLength.Value = value;
}
/// <summary>
/// EMA length used as the trend filter.
/// </summary>
public int EmaPeriod
{
get => _emaPeriod.Value;
set => _emaPeriod.Value = value;
}
/// <summary>
/// Stop loss in absolute points.
/// </summary>
public decimal StopLossPoints
{
get => _stopLossPoints.Value;
set => _stopLossPoints.Value = value;
}
/// <summary>
/// Take profit in absolute points.
/// </summary>
public decimal TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>
/// Candle type used for calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public SupportResistanceBreakoutStrategy()
{
_rangeLength = Param(nameof(RangeLength), 55)
.SetGreaterThanZero()
.SetDisplay("Range Length", "Candles used to form support/resistance", "General")
.SetOptimize(20, 100, 5);
_emaPeriod = Param(nameof(EmaPeriod), 50)
.SetGreaterThanZero()
.SetDisplay("EMA Period", "Length of the EMA trend filter", "General")
.SetOptimize(20, 200, 10);
_stopLossPoints = Param(nameof(StopLossPoints), 500m)
.SetNotNegative()
.SetDisplay("Stop Loss", "Stop loss in absolute points", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 500m)
.SetNotNegative()
.SetDisplay("Take Profit", "Take profit in absolute points", "Risk");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Primary candle series", "General");
Volume = 1;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_ema = null;
_highs.Clear();
_lows.Clear();
_support = 0m;
_resistance = 0m;
_entryPrice = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
_ema = new ExponentialMovingAverage { Length = EmaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_ema, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _ema);
DrawOwnTrades(area);
}
var tp = TakeProfitPoints > 0 ? new Unit(TakeProfitPoints, UnitTypes.Absolute) : null;
var sl = StopLossPoints > 0 ? new Unit(StopLossPoints, UnitTypes.Absolute) : null;
if (tp != null || sl != null)
StartProtection(tp, sl);
base.OnStarted2(time);
}
/// <inheritdoc />
protected override void OnOwnTradeReceived(MyTrade trade)
{
base.OnOwnTradeReceived(trade);
if (Position != 0 && _entryPrice == null)
_entryPrice = trade.Trade.Price;
if (Position == 0)
_entryPrice = null;
}
private void ProcessCandle(ICandleMessage candle, decimal emaValue)
{
if (candle.State != CandleStates.Finished)
return;
// Track highs and lows manually
_highs.Add(candle.HighPrice);
_lows.Add(candle.LowPrice);
if (_highs.Count > RangeLength)
_highs.RemoveAt(0);
if (_lows.Count > RangeLength)
_lows.RemoveAt(0);
if (_highs.Count < RangeLength)
return;
// Compute support/resistance from previous bars (exclude current)
var prevResistance = _resistance;
var prevSupport = _support;
decimal maxHigh = decimal.MinValue;
decimal minLow = decimal.MaxValue;
// Use bars 0..N-2 (exclude last which is current)
for (int i = 0; i < _highs.Count - 1; i++)
{
if (_highs[i] > maxHigh) maxHigh = _highs[i];
if (_lows[i] < minLow) minLow = _lows[i];
}
_resistance = maxHigh;
_support = minLow;
// Determine trend from EMA
var isBullish = candle.ClosePrice > emaValue;
var isBearish = candle.ClosePrice < emaValue;
// Exit: close longs if price falls back below support while in profit
if (Position > 0 && _entryPrice is decimal entryLong)
{
if (candle.ClosePrice - entryLong > 0 && candle.ClosePrice < _support)
{
SellMarket(Position);
return;
}
}
else if (Position < 0 && _entryPrice is decimal entryShort)
{
if (entryShort - candle.ClosePrice > 0 && candle.ClosePrice > _resistance)
{
BuyMarket(Math.Abs(Position));
return;
}
}
if (!IsFormedAndOnlineAndAllowTrading())
return;
// Entry: breakout above resistance in bullish trend
if (isBullish && Position <= 0 && candle.ClosePrice > _resistance && _resistance > 0)
{
if (Position < 0)
BuyMarket(Math.Abs(Position));
BuyMarket(Volume);
}
// Entry: breakdown below support in bearish trend
else if (isBearish && Position >= 0 && candle.ClosePrice < _support && _support > 0)
{
if (Position > 0)
SellMarket(Position);
SellMarket(Volume);
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates, UnitTypes, Unit
from StockSharp.Algo.Strategies import Strategy
from StockSharp.Algo.Indicators import ExponentialMovingAverage
class support_resistance_breakout_strategy(Strategy):
def __init__(self):
super(support_resistance_breakout_strategy, self).__init__()
self._range_length = self.Param("RangeLength", 55) \
.SetDisplay("Range Length", "Candles used to form support/resistance", "General")
self._ema_period = self.Param("EmaPeriod", 50) \
.SetDisplay("EMA Period", "Length of the EMA trend filter", "General")
self._stop_loss_points = self.Param("StopLossPoints", 500.0) \
.SetDisplay("Stop Loss", "Stop loss in absolute points", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 500.0) \
.SetDisplay("Take Profit", "Take profit in absolute points", "Risk")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Primary candle series", "General")
self.Volume = 1
self._highs = []
self._lows = []
self._support = 0.0
self._resistance = 0.0
self._entry_price = None
@property
def RangeLength(self):
return self._range_length.Value
@property
def EmaPeriod(self):
return self._ema_period.Value
@property
def StopLossPoints(self):
return self._stop_loss_points.Value
@property
def TakeProfitPoints(self):
return self._take_profit_points.Value
@property
def CandleType(self):
return self._candle_type.Value
def OnStarted2(self, time):
ema = ExponentialMovingAverage()
ema.Length = self.EmaPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(ema, self.ProcessCandle).Start()
tp = float(self.TakeProfitPoints)
sl = float(self.StopLossPoints)
tp_unit = Unit(tp, UnitTypes.Absolute) if tp > 0 else None
sl_unit = Unit(sl, UnitTypes.Absolute) if sl > 0 else None
if tp_unit is not None or sl_unit is not None:
self.StartProtection(tp_unit, sl_unit)
super(support_resistance_breakout_strategy, self).OnStarted2(time)
def ProcessCandle(self, candle, ema_value):
if candle.State != CandleStates.Finished:
return
ema_value = float(ema_value)
high = float(candle.HighPrice)
low = float(candle.LowPrice)
close = float(candle.ClosePrice)
self._highs.append(high)
self._lows.append(low)
rl = int(self.RangeLength)
if len(self._highs) > rl:
self._highs.pop(0)
if len(self._lows) > rl:
self._lows.pop(0)
if len(self._highs) < rl:
return
max_high = max(self._highs[:-1])
min_low = min(self._lows[:-1])
self._resistance = max_high
self._support = min_low
is_bullish = close > ema_value
is_bearish = close < ema_value
pos = float(self.Position)
if pos > 0 and self._entry_price is not None:
if close - self._entry_price > 0 and close < self._support:
self.SellMarket(self.Position)
return
elif pos < 0 and self._entry_price is not None:
if self._entry_price - close > 0 and close > self._resistance:
self.BuyMarket(Math.Abs(self.Position))
return
if not self.IsFormedAndOnlineAndAllowTrading():
return
if is_bullish and pos <= 0 and close > self._resistance and self._resistance > 0:
if pos < 0:
self.BuyMarket(Math.Abs(self.Position))
self.BuyMarket(self.Volume)
elif is_bearish and pos >= 0 and close < self._support and self._support > 0:
if pos > 0:
self.SellMarket(self.Position)
self.SellMarket(self.Volume)
def OnOwnTradeReceived(self, trade):
super(support_resistance_breakout_strategy, self).OnOwnTradeReceived(trade)
if self.Position != 0 and self._entry_price is None:
self._entry_price = float(trade.Trade.Price)
if self.Position == 0:
self._entry_price = None
def OnReseted(self):
super(support_resistance_breakout_strategy, self).OnReseted()
self._highs = []
self._lows = []
self._support = 0.0
self._resistance = 0.0
self._entry_price = None
def CreateClone(self):
return support_resistance_breakout_strategy()