Break the Range Bound 策略
该策略寻找三条移动平均线在狭窄区间内收敛的盘整期。当价格向上或向下突破该区间时,策略按突破方向开仓,尝试捕捉新的趋势。
系统观察快、中、慢三条 SMA 之间的最大差值。如果在指定的 RangeLength 根K线上,该差值始终低于 ShakeThreshold,则认为市场处于整理区间。此期间的最高价和最低价定义了突破水平。
当价格收盘突破这些水平时进场。如果价格回到区间内,或盈利达到区间宽度的四倍,仓位将被平仓。
详情
- 入场条件:
- 做多:在满足整理区间条件后,价格收盘高于区间最高价。
- 做空:在满足整理区间条件后,价格收盘低于区间最低价。
- 多空方向:双向。
- 退出条件:
- 多单:价格跌回区间下沿或盈利超过
4 * (高-低)。 - 空单:价格升回区间上沿或盈利超过
4 * (高-低)。
- 多单:价格跌回区间下沿或盈利超过
- 止损:基于区间边界和盈利倍数的条件退出。
- 默认值:
FastSma= 38MidSma= 140SlowSma= 210ShakeThreshold= 250RangeLength= 200CandleType= 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;
}
}
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 ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class break_the_range_bound_strategy(Strategy):
def __init__(self):
super(break_the_range_bound_strategy, self).__init__()
self._fast_sma = self.Param("FastSma", 10) \
.SetDisplay("Fast SMA", "Fast moving average period", "Parameters")
self._slow_sma = self.Param("SlowSma", 50) \
.SetDisplay("Slow SMA", "Slow moving average period", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._prev_close = 0.0
self._has_prev = False
@property
def fast_sma(self):
return self._fast_sma.Value
@property
def slow_sma(self):
return self._slow_sma.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(break_the_range_bound_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._prev_close = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(break_the_range_bound_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_sma
slow = ExponentialMovingAverage()
slow.Length = self.slow_sma
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(fast, slow, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
close = candle.ClosePrice
if not self._has_prev:
self._prev_fast = fast_value
self._prev_slow = slow_value
self._prev_close = close
self._has_prev = True
return
# Cross above slow SMA => buy breakout
if self._prev_close <= self._prev_slow and close > slow_value and fast_value > slow_value and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
# Cross below slow SMA => sell breakout
elif self._prev_close >= self._prev_slow and close < slow_value and fast_value < slow_value and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_fast = fast_value
self._prev_slow = slow_value
self._prev_close = close
def CreateClone(self):
return break_the_range_bound_strategy()