在 GitHub 上查看

支撑阻力突破策略

概述

该策略复刻了 MetaTrader 上的 “SupportResistTrade” 专家顾问,通过 Donchian 通道确定最近的支撑与阻力,并结合长期 EMA 趋势过滤器来筛选方向。只有当价格突破区间边界并且 K 线开盘价位于 EMA 的同侧时才会开仓。策略使用即时保护性止损以及三阶逐步移动止损(+10/+20/+30 点)来管理风险。

数据与指标

  • 主要数据源: 单一 K 线订阅(默认 1 分钟,可通过 CandleType 参数调整)。
  • 支撑/阻力: DonchianChannels,长度为 RangeLength(默认 55),用于追踪最近区间的最高价与最低价。
  • 趋势过滤: ExponentialMovingAverage,周期为 EmaPeriod(默认 500),基于 K 线开盘价计算。只有当开盘价位于 EMA 上方/下方时才允许做多/做空。

交易逻辑

  1. 市场分析: 每根已完成 K 线都会更新 Donchian 区间与 EMA。上轨视为阻力,下轨视为支撑。
  2. 入场条件:
    • 做多: K 线收盘价突破阻力,且开盘价高于 EMA。若持有空单则先平仓,再市价开多。
    • 做空: K 线收盘价跌破支撑,且开盘价低于 EMA。若持有多单则先平仓,再市价开空。
  3. 初始止损: 成交后立即在最新支撑(多单)或最新阻力(空单)放置止损单,对应原 EA 的 OrderSend 参数。
  4. 离场规则:
    • 当持仓已有盈利且收盘价回到更新后的支撑/阻力另一侧时,立即市价平仓,与原策略的 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);
		}
	}
}