Internal Bar Strength IBS Mean Reversion 策略
该策略仅做空,利用内部柱强度(IBS)进行均值回归。当 IBS 较高且价格突破前高时卖出,IBS 下降到下限时平仓。
细节
- 入场条件: IBS >= 上限且收盘价高于前高
- 多空方向: 做空
- 出场条件: IBS <= 下限
- 止损: 无
- 默认值:
UpperThreshold= 0.9LowerThreshold= 0.3
- 过滤器:
- 分类: Mean Reversion
- 方向: 做空
- 指标: IBS
- 止损: 无
- 复杂度: 基础
- 时间框架: 日内
- 季节性: 否
- 神经网络: 否
- 背离: 否
- 风险等级: 中等
using System;
using System.Linq;
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>
/// Short strategy using internal bar strength for mean reversion.
/// </summary>
public class InternalBarStrengthIbsMeanReversionStrategy : Strategy
{
private readonly StrategyParam<decimal> _upperThreshold;
private readonly StrategyParam<decimal> _lowerThreshold;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevHigh;
private bool _isReady;
public decimal UpperThreshold { get => _upperThreshold.Value; set => _upperThreshold.Value = value; }
public decimal LowerThreshold { get => _lowerThreshold.Value; set => _lowerThreshold.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public InternalBarStrengthIbsMeanReversionStrategy()
{
_upperThreshold = Param(nameof(UpperThreshold), 0.9m)
.SetDisplay("Upper Threshold", "IBS value to trigger entry", "Parameters");
_lowerThreshold = Param(nameof(LowerThreshold), 0.3m)
.SetDisplay("Lower Threshold", "IBS value to exit", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevHigh = 0;
_isReady = false;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = 10 };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(sma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal _dummy)
{
if (candle.State != CandleStates.Finished)
return;
var range = candle.HighPrice - candle.LowPrice;
if (range == 0)
{
_prevHigh = candle.HighPrice;
_isReady = true;
return;
}
if (!_isReady)
{
_prevHigh = candle.HighPrice;
_isReady = true;
return;
}
var ibs = (candle.ClosePrice - candle.LowPrice) / range;
// Short when close above previous high and IBS is high (near candle top)
if (candle.ClosePrice > _prevHigh && ibs >= UpperThreshold && Position >= 0)
SellMarket();
else if (Position < 0 && ibs <= LowerThreshold)
BuyMarket();
_prevHigh = candle.HighPrice;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class internal_bar_strength_ibs_mean_reversion_strategy(Strategy):
def __init__(self):
super(internal_bar_strength_ibs_mean_reversion_strategy, self).__init__()
self._upper_threshold = self.Param("UpperThreshold", 0.9) \
.SetDisplay("Upper Threshold", "IBS value to trigger entry", "Parameters")
self._lower_threshold = self.Param("LowerThreshold", 0.3) \
.SetDisplay("Lower Threshold", "IBS value to exit", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._prev_high = 0.0
self._is_ready = False
@property
def upper_threshold(self):
return self._upper_threshold.Value
@property
def lower_threshold(self):
return self._lower_threshold.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(internal_bar_strength_ibs_mean_reversion_strategy, self).OnReseted()
self._prev_high = 0.0
self._is_ready = False
def OnStarted2(self, time):
super(internal_bar_strength_ibs_mean_reversion_strategy, self).OnStarted2(time)
sma = SimpleMovingAverage()
sma.Length = 10
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def on_process(self, candle, _dummy):
if candle.State != CandleStates.Finished:
return
rng = candle.HighPrice - candle.LowPrice
if rng == 0:
self._prev_high = candle.HighPrice
self._is_ready = True
return
if not self._is_ready:
self._prev_high = candle.HighPrice
self._is_ready = True
return
ibs = (candle.ClosePrice - candle.LowPrice) / rng
# Short when close above previous high and IBS is high (near candle top)
if candle.ClosePrice > self._prev_high and ibs >= self.upper_threshold and self.Position >= 0:
self.SellMarket()
elif self.Position < 0 and ibs <= self.lower_threshold:
self.BuyMarket()
self._prev_high = candle.HighPrice
def CreateClone(self):
return internal_bar_strength_ibs_mean_reversion_strategy()