区间突破策略
该策略在最近 RangePeriod 根K线上统计最高价和最低价。当K线收盘价向上或向下突破该区间,且区间宽度(以点数计)小于 MaxRangePoints 时,策略会在突破方向开仓。
入场规则
- 多头:收盘价 >= 观察区间内的最高价,并且区间宽度(点数) <=
MaxRangePoints,同时当前无持仓。 - 空头:收盘价 <= 观察区间内的最低价,并且区间宽度(点数) <=
MaxRangePoints,同时当前无持仓。
离场规则
- 开仓后立即设置止损和止盈保护。
- 无额外的离场条件,仓位保持到保护触发为止。
参数
RangePeriod– 计算最高/最低价所使用的K线数量。MaxRangePoints– 允许交易的最大区间宽度(点数)。CandleType– 用于分析和交易的K线周期。Volume– 市价单交易量。StopLossPoints– 止损距离(点数)。TakeProfitPoints– 止盈距离(点数)。
指标
- Highest
- Lowest
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Range Breakout strategy: Highest/Lowest channel breakout.
/// Buys when close >= highest. Sells when close <= lowest.
/// </summary>
public class RangeBreakoutStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _channelPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int ChannelPeriod
{
get => _channelPeriod.Value;
set => _channelPeriod.Value = value;
}
public RangeBreakoutStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_channelPeriod = Param(nameof(ChannelPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Lookback period", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var high = new Highest { Length = ChannelPeriod };
var low = new Lowest { Length = ChannelPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(high, low, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (candle.ClosePrice >= high && Position <= 0)
BuyMarket();
else if (candle.ClosePrice <= low && Position >= 0)
SellMarket();
}
}
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 Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class range_breakout_strategy(Strategy):
def __init__(self):
super(range_breakout_strategy, self).__init__()
self._channel_period = self.Param("ChannelPeriod", 14) \
.SetDisplay("Channel Period", "Highest/Lowest lookback", "Indicators")
self._highest = None
self._lowest = None
@property
def channel_period(self):
return self._channel_period.Value
def OnReseted(self):
super(range_breakout_strategy, self).OnReseted()
self._highest = None
self._lowest = None
def OnStarted2(self, time):
super(range_breakout_strategy, self).OnStarted2(time)
self._highest = Highest()
self._highest.Length = self.channel_period
self._lowest = Lowest()
self._lowest.Length = self.channel_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._highest, self._lowest, self._process_candle)
subscription.Start()
def _process_candle(self, candle, high_val, low_val):
if candle.State != CandleStates.Finished:
return
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
close = float(candle.ClosePrice)
h = float(high_val)
l = float(low_val)
if close >= h and self.Position <= 0:
self.BuyMarket()
elif close <= l and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return range_breakout_strategy()