在 GitHub 上查看

Break the Range Bound 策略

该策略寻找三条移动平均线在狭窄区间内收敛的盘整期。当价格向上或向下突破该区间时,策略按突破方向开仓,尝试捕捉新的趋势。

系统观察快、中、慢三条 SMA 之间的最大差值。如果在指定的 RangeLength 根K线上,该差值始终低于 ShakeThreshold,则认为市场处于整理区间。此期间的最高价和最低价定义了突破水平。

当价格收盘突破这些水平时进场。如果价格回到区间内,或盈利达到区间宽度的四倍,仓位将被平仓。

详情

  • 入场条件
    • 做多:在满足整理区间条件后,价格收盘高于区间最高价。
    • 做空:在满足整理区间条件后,价格收盘低于区间最低价。
  • 多空方向:双向。
  • 退出条件
    • 多单:价格跌回区间下沿或盈利超过 4 * (高-低)
    • 空单:价格升回区间上沿或盈利超过 4 * (高-低)
  • 止损:基于区间边界和盈利倍数的条件退出。
  • 默认值
    • FastSma = 38
    • MidSma = 140
    • SlowSma = 210
    • ShakeThreshold = 250
    • RangeLength = 200
    • CandleType = TimeSpan.FromMinutes(1)
  • 过滤器
    • 分类:突破
    • 方向:双向
    • 指标:SMA、Highest、Lowest
    • 止损:有
    • 复杂度:基础
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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 waits for converging SMAs and trades on breakout.
/// </summary>
public class BreakTheRangeBoundStrategy : Strategy
{
	private readonly StrategyParam<int> _fastSma;
	private readonly StrategyParam<int> _slowSma;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _prevClose;
	private bool _hasPrev;

	public int FastSma { get => _fastSma.Value; set => _fastSma.Value = value; }
	public int SlowSma { get => _slowSma.Value; set => _slowSma.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BreakTheRangeBoundStrategy()
	{
		_fastSma = Param(nameof(FastSma), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast SMA", "Fast moving average period", "Parameters");

		_slowSma = Param(nameof(SlowSma), 50)
			.SetGreaterThanZero()
			.SetDisplay("Slow SMA", "Slow moving average period", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_prevClose = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var fast = new ExponentialMovingAverage { Length = FastSma };
		var slow = new ExponentialMovingAverage { Length = SlowSma };

		SubscribeCandles(CandleType).Bind(fast, slow, ProcessCandle).Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;

		var close = candle.ClosePrice;

		if (!_hasPrev)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_prevClose = close;
			_hasPrev = true;
			return;
		}

		// Cross above slow SMA => buy breakout
		if (_prevClose <= _prevSlow && close > slowValue && fastValue > slowValue && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		// Cross below slow SMA => sell breakout
		else if (_prevClose >= _prevSlow && close < slowValue && fastValue < slowValue && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
		_prevClose = close;
	}
}