这是一个基于RSI和CCI双重技术指标的自适应交易策略。策略通过监控不同时间周期下的RSI和CCI指标的交叉状态,结合EMA均线趋势,构建了一个完整的交易系统。该策略具有自适应性强、信号稳定等特点,能够有效地捕捉市场超买超卖机会。
策略的核心逻辑包括以下几个方面: 1. 时间周期自适应:根据不同的时间周期(1分钟到4小时)动态调整RSI和CCI的参数设置。 2. 双重指标确认:使用RSI(相对强弱指标)和CCI(顺势指标)的组合来过滤交易信号。当RSI和CCI同时满足特定条件时才会产生交易信号。 3. 信号持续性验证:通过设置最小持续时间(stayTimeFrames)来确保信号的稳定性。 4. 动态止盈止损:基于入场时的RSI和CCI水平设置动态的止盈止损点位。 5. 趋势确认:使用200周期EMA作为趋势参考。
该策略通过结合RSI和CCI指标的优势,构建了一个稳健的交易系统。策略的自适应特性和完善的风险控制机制使其具有良好的实用性。通过持续优化和完善,该策略有望在实际交易中取得更好的表现。建议交易者在实盘使用前进行充分的回测和参数优化。
/*backtest
start: 2025-01-19 00:00:00
end: 2025-02-18 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("RSI & CCI Strategy with Alerts", overlay=true)
// Detect current chart timeframe
tf = timeframe.period
// Define settings for different timeframes
rsiLength = tf == "1" ? 30 : tf == "5" ? 30 : tf == "15" ? 30 : tf == "30" ? 30 : 30 // Default
cciLength = tf == "1" ? 15 : tf == "5" ? 20 : tf == "15" ? 20 : tf == "30" ? 20 : 20 // Default
cciBuyThreshold = tf == "1" ? -100 : tf == "5" ? -100 : tf == "15" ? -100 : tf == "30" ? -100 : -100
cciSellThreshold = tf == "1" ? 100 : tf == "5" ? 100 : tf == "15" ? 100 : tf == "30" ? 100 : 100 // Default
stayTimeFrames = tf == "1" ? 1 : tf == "5" ? 1 : tf == "15" ? 1 : tf == "30" ? 1 : tf == "240" ? 1 : 2 // Default
stayTimeFramesOver =tf == "1" ? 1 : tf == "5" ? 2 : tf == "15" ? 2 : tf == "30" ? 3 : 2 // Default
// Calculate RSI & CCI
rsi = ta.rsi(close, rsiLength)
rsiOver = ta.rsi(close, 14)
cci = ta.cci(close, cciLength)
// EMA 50
ema200 = ta.ema(close, 200)
plot(ema200, color=color.rgb(255, 255, 255), linewidth=2, title="EMA 200")
// CCI candle threshold tracking
var int cciEntryTimeLong = na
var int cciEntryTimeShort = na
// Store entry time when CCI enters the zone
if (cci < cciBuyThreshold)
if na(cciEntryTimeLong)
cciEntryTimeLong := bar_index
else
cciEntryTimeLong := na
if (cci > cciSellThreshold)
if na(cciEntryTimeShort)
cciEntryTimeShort := bar_index
else
cciEntryTimeShort := na
// Confirming CCI has stayed in the threshold for required bars
cciStayedBelowNeg100 = not na(cciEntryTimeLong) and (bar_index - cciEntryTimeLong >= stayTimeFrames) and rsi >= 53
cciStayedAbove100 = not na(cciEntryTimeShort) and (bar_index - cciEntryTimeShort >= stayTimeFrames) and rsi <= 47
// CCI & RSI candle threshold tracking for Buy Over and Sell Over signals
var int buyOverEntryTime = na
var int sellOverEntryTime = na
// Track entry time when RSI and CCI conditions are met
if (rsiOver <= 31 and cci <= -120)
if na(buyOverEntryTime)
buyOverEntryTime := bar_index
else
buyOverEntryTime := na
if (rsiOver >= 69 and cci >= 120)
if na(sellOverEntryTime)
sellOverEntryTime := bar_index
else
sellOverEntryTime := na
// Confirm that conditions are met for the required stayTimeFrames
buyOverCondition = not na(buyOverEntryTime) and (bar_index - buyOverEntryTime >= stayTimeFramesOver)
sellOverCondition = not na(sellOverEntryTime) and (bar_index - sellOverEntryTime <= stayTimeFramesOver)
//Buy and sell for over bought or sell
conditionOverBuy = buyOverCondition
conditionOverSell = sellOverCondition
// Buy and sell conditions
buyCondition = cciStayedBelowNeg100
sellCondition = cciStayedAbove100
// // Track open positions
var bool isLongOpen = false
var bool isShortOpen = false
// // Strategy logic for backtesting
// if (buyCondition and not isLongOpen)
// strategy.entry("Long", strategy.long)
// isLongOpen := true
// isShortOpen := false
// if (sellCondition and not isShortOpen)
// strategy.entry("Short", strategy.short)
// isShortOpen := true
// isLongOpen := false
// // Close positions based on EMA 50
// if (isLongOpen and exitLongCondition)
// strategy.close("Long")
// isLongOpen := false
// if (isShortOpen and exitShortCondition)
// strategy.close("Short")
// isShortOpen := false
// Track RSI at position entry
var float entryRSILong = na
var float entryRSIShort = na
// Track CCI at position entry
var float entryCCILong = na
var float entryCCIShort = na
if (buyOverCondition and not isLongOpen)
strategy.entry("Long", strategy.long)
entryRSILong := rsi // Store RSI at entry
entryCCILong := cci
isLongOpen := true
isShortOpen := false
if (sellOverCondition and not isShortOpen)
strategy.entry("Short", strategy.short)
entryRSIShort := rsi // Store RSI at entry
entryCCIShort := cci // Stpre CCI at entry
isShortOpen := true
isLongOpen := false
exitLongRSICondition = isLongOpen and not na(entryRSILong) and rsi >= (entryRSILong + 12) or rsi <= (entryRSILong -8)
exitShortRSICondition = isShortOpen and not na(entryRSIShort) and rsi <= (entryRSIShort - 12) or rsi >= (entryRSIShort +8)
exitLongCCICondition = isLongOpen and not na(entryCCILong) and cci <= (entryCCILong -100)
exitShortCCICondition = isShortOpen and not na(entryCCIShort) and cci >= (entryCCIShort +100)
// Close positions based on EMA 50 or RSI change
if (isLongOpen and (exitLongRSICondition) or (exitLongCCICondition))
strategy.close("Long")
isLongOpen := false
entryRSILong := na
entryCCILong := na
isLongOpen := false
if (isShortOpen and (exitShortRSICondition) or (exitShortCCICondition))
strategy.close("Short")
isShortOpen := false
entryRSIShort := na
entryCCIShort := na
isShortOpen := false
// Plot buy and sell signals
plotshape(buyCondition, style=shape.labelup, location=location.belowbar, color=color.green, size=size.large, title="Buy Signal", text="BUY")
plotshape(sellCondition, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.large, title="Sell Signal", text="SELL")
//Plot buy and sell OverBought
plotshape(conditionOverBuy, style=shape.labelup, location=location.belowbar, color=color.rgb(255, 238, 0), size=size.large, title="OverBuy Signal", text="Over Sell")
plotshape(conditionOverSell, style=shape.labeldown, location=location.abovebar, color=color.rgb(186, 40, 223), size=size.large, title="OverSell Signal", text="Over Buy")
// Alerts
alertcondition(buyCondition, title="Buy Alert", message="Buy Signal Triggered")
alertcondition(sellCondition, title="Sell Alert", message="Sell Signal Triggered")