波动捕捉 RSI-Bollinger
该策略利用动态布林带并可选用 RSI 过滤器以捕捉波动性。
详情
- 入场条件:价格穿越自适应布林带,可选择 RSI 确认。
- 多空方向:通过
Direction参数配置。 - 退出条件:价格穿越相反方向的跟踪带。
- 止损:无。
- 默认值:
BollingerLength= 50Multiplier= 2.7183mUseRsi= trueRsiPeriod= 10RsiSmaPeriod= 5BoughtRangeLevel= 55mSoldRangeLevel= 50mDirection= TradeDirection.BothCandleType= TimeSpan.FromMinutes(5)
- 过滤器:
- 类型:波动性
- 方向:可配置
- 指标:Bollinger、RSI
- 止损:无
- 复杂度:基础
- 时间框架:日内 (5m)
- 季节性:无
- 神经网络:无
- 背离:无
- 风险等级:中
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>
/// Volatility capture strategy using RSI and Bollinger band logic.
/// Buys when price crosses above lower band with RSI confirmation.
/// Sells when price crosses below upper band.
/// </summary>
public class VolatilityCaptureRsiBollingerStrategy : Strategy
{
private readonly StrategyParam<int> _smaLength;
private readonly StrategyParam<decimal> _bbWidth;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiBuy;
private readonly StrategyParam<decimal> _rsiSell;
private readonly StrategyParam<DataType> _candleType;
private int _cooldown;
public int SmaLength { get => _smaLength.Value; set => _smaLength.Value = value; }
public decimal BbWidth { get => _bbWidth.Value; set => _bbWidth.Value = value; }
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public decimal RsiBuy { get => _rsiBuy.Value; set => _rsiBuy.Value = value; }
public decimal RsiSell { get => _rsiSell.Value; set => _rsiSell.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public VolatilityCaptureRsiBollingerStrategy()
{
_smaLength = Param(nameof(SmaLength), 50)
.SetGreaterThanZero()
.SetDisplay("SMA Length", "Bollinger SMA period", "Indicators");
_bbWidth = Param(nameof(BbWidth), 2.7m)
.SetGreaterThanZero()
.SetDisplay("BB Width", "Bollinger band width", "Indicators");
_rsiLength = Param(nameof(RsiLength), 10)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_rsiBuy = Param(nameof(RsiBuy), 55m)
.SetDisplay("RSI Buy", "RSI above for buy signal", "Levels");
_rsiSell = Param(nameof(RsiSell), 50m)
.SetDisplay("RSI Sell", "RSI below for sell signal", "Levels");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SimpleMovingAverage { Length = SmaLength };
var stdDev = new StandardDeviation { Length = SmaLength };
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(sma, stdDev, rsi, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, sma);
DrawIndicator(area, rsi);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal smaVal, decimal stdVal, decimal rsiVal)
{
if (candle.State != CandleStates.Finished)
return;
if (_cooldown > 0)
{
_cooldown--;
return;
}
var lower = smaVal - BbWidth * stdVal;
var upper = smaVal + BbWidth * stdVal;
// Buy when price near lower band (oversold)
if (candle.ClosePrice <= lower && rsiVal < RsiSell && Position <= 0)
{
BuyMarket();
_cooldown = 50;
}
// Sell when price near upper band (overbought)
else if (candle.ClosePrice >= upper && rsiVal > RsiBuy && Position >= 0)
{
SellMarket();
_cooldown = 50;
}
}
}
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 RelativeStrengthIndex, SimpleMovingAverage, StandardDeviation
from StockSharp.Algo.Strategies import Strategy
class volatility_capture_rsi_bollinger_strategy(Strategy):
def __init__(self):
super(volatility_capture_rsi_bollinger_strategy, self).__init__()
self._sma_length = self.Param("SmaLength", 50) \
.SetDisplay("SMA Length", "Bollinger SMA period", "Indicators")
self._bb_width = self.Param("BbWidth", 2.7) \
.SetDisplay("BB Width", "Bollinger band width", "Indicators")
self._rsi_length = self.Param("RsiLength", 10) \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._rsi_buy = self.Param("RsiBuy", 55) \
.SetDisplay("RSI Buy", "RSI above for buy signal", "Levels")
self._rsi_sell = self.Param("RsiSell", 50.0) \
.SetDisplay("RSI Sell", "RSI below for sell signal", "Levels")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._cooldown = 0
@property
def sma_length(self):
return self._sma_length.Value
@property
def bb_width(self):
return self._bb_width.Value
@property
def rsi_length(self):
return self._rsi_length.Value
@property
def rsi_buy(self):
return self._rsi_buy.Value
@property
def rsi_sell(self):
return self._rsi_sell.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(volatility_capture_rsi_bollinger_strategy, self).OnReseted()
self._cooldown = 0
def OnStarted2(self, time):
super(volatility_capture_rsi_bollinger_strategy, self).OnStarted2(time)
sma = SimpleMovingAverage()
sma.Length = self.sma_length
std_dev = StandardDeviation()
std_dev.Length = self.sma_length
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, std_dev, rsi, self.on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawIndicator(area, rsi)
self.DrawOwnTrades(area)
def on_process(self, candle, sma_val, std_val, rsi_val):
if candle.State != CandleStates.Finished:
return
if self._cooldown > 0:
self._cooldown -= 1
return
lower = sma_val - self.bb_width * std_val
upper = sma_val + self.bb_width * std_val
# Buy when price near lower band (oversold)
if candle.ClosePrice <= lower and rsi_val < self.rsi_sell and self.Position <= 0:
self.BuyMarket()
self._cooldown = 50
# Sell when price near upper band (overbought)
elif candle.ClosePrice >= upper and rsi_val > self.rsi_buy and self.Position >= 0:
self.SellMarket()
self._cooldown = 50
def CreateClone(self):
return volatility_capture_rsi_bollinger_strategy()