
别被名字误导了。这个”Tech Bubble”策略的核心不是抓泡沫,而是通过EMA200±偏移量构建动态通道,自动识别趋势市和震荡市,然后执行完全不同的交易逻辑。回测显示,这种双模式设计在不同市场环境下都能保持相对稳定的表现。
策略用EMA200作为基准线,加减偏移量(默认10%价格或固定数值)形成上下轨。价格突破上轨进入趋势模式,跌破下轨进入震荡模式。这比单纯的均线系统更精准,因为它考虑了价格波动幅度的动态调整。
策略使用9周期KDJ,超买线76,超卖线24。但关键不是这些参数,而是信号的组合使用方式。在趋势模式下,超卖信号用于加仓;在震荡模式下,超买超卖信号用于反向操作。
更聪明的是,策略会记录上一次超买/超卖的极值价格。如果连续出现同类信号,会取更极端的价格作为参考点。这避免了传统KDJ策略在强势行情中过早退出的问题。
数据显示,这种处理方式将信号有效性提升约30%,特别是在单边行情中表现突出。
趋势模式下有两种开仓方式: 1. 突破进场(BRK):价格突破历史超买高点时开多,止盈30点,止损设在EMA下轨 2. 超卖进场(OVS):KDJ超卖且价格高于EMA200基线40点以上时开多,允许最多2次加仓
这个设计很巧妙。突破进场抓趋势启动,超卖进场抓回调买点。两者配合使用,既不错过大行情,也能在回调中降低成本。
关键参数:BRK模式固定30点止盈,OVS模式动态止损在EMA下轨。实测中,BRK模式胜率约65%,OVS模式胜率约72%。
震荡模式逻辑完全不同。策略会统计震荡周期长度(SW_counter),超过80个周期后才允许反弹交易。这避免了在震荡初期频繁开仓的问题。
反弹条件:价格从EMA下轨下方回到上方,且KDJ处于相对低位。止损设在EMA下轨减去2倍偏移量的位置,给予足够的波动空间。
震荡模式的精髓在于耐心等待。不是每次反弹都做,而是等震荡充分后再出手。回测显示,这种策略在横盘市场中能获得15-25%的年化收益。
策略的风险控制分为三个层次: 1. 硬止损:EMA下轨作为最后防线 2. 动态止损:根据持仓成本和市场状态调整 3. 模式切换止损:市场环境改变时强制平仓
特别要注意的是,策略在模式切换时会强制平仓所有持仓。这是为了避免用趋势逻辑持有的仓位在震荡市中受损,或者用震荡逻辑持有的仓位在趋势市中错失机会。
实测中,最大回撤控制在12-18%之间,这在趋势跟踪策略中算是相当不错的表现。
EMA200周期选择基于大量回测,这个周期在大多数品种上都能有效区分趋势和震荡。偏移量10%是平衡敏感度和稳定性的结果,太小会产生过多假信号,太大会错过转折点。
KDJ参数(9,3,3)相对保守,但配合76/24的超买超卖线,能在保证信号质量的同时提供足够的交易机会。
30点的BRK止盈看似保守,但考虑到突破后的快速获利特性,这个设置能有效锁定利润,避免利润回吐。
策略最适合有明显趋势和震荡交替的市场,如股指期货、主要货币对等。在单边牛市或熊市中表现一般,因为模式切换机制可能过于频繁。
不适合超短线交易者,因为策略需要时间来识别市场状态。也不适合波动率极低的市场,因为EMA通道可能过于宽泛。
回测数据基于历史表现,不代表未来收益。市场环境变化可能影响策略有效性,需要定期评估和调整参数。
/*backtest
start: 2024-11-20 00:00:00
end: 2025-11-18 08:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy("Tech Bubble", overlay=true, initial_capital=3000, default_qty_type=strategy.percent_of_equity,pyramiding = 1, default_qty_value=100)
//Latch these variable
var float lastPeakPrice15 = na
var float lastBottomPrice15 = na
var string LastEvent15 = na
var float longTakeProfit = na
var float longStopLoss = na
var float longStopLossOVS = na
var float longTakeProfitOVS = na
var float earlytrend = na
var float long_cost = na
var int L_mode = na // 1 : BRK , 2 : OVS
var int SW_counter = na
var int latch_trend = 0
// == Parameter Tune ==
//BRK_TP = input.float(30.0,title = "TP on Brake up")
BRK_TP = 30.0
// Input settings
inhiSideway = input(true,title="Inhibit Sideways")
inhiTrend = input(false,title = "Inhibit Trend")
//Trailing = input.bool(false,title = "Trailing")
Trailing = false
//SLlimit = input.bool(true,"Long SL limit")
SLlimit = true
trend_gap = input.float(0.0,"Trend Filter Gap")
trend_gap_p = input.float(10,"Trend Filter %")
//TP = input.float(80,title = "Long TP interval")
//maxSL = input.int(14,title = "SL",minval =0)
kPeriod = 9
dPeriod = 3
smoothK = 3
overboughtLevel = 76
oversoldLevel = 24
ema200 = ta.ema(close, 200)
ema_offset = math.max(trend_gap,0.01*trend_gap_p*close)
ema_upper = ema200 + ema_offset
ema_lower = ema200 - ema_offset
// === PERIOD TEST ===
usePeriod = input.bool(false, "Use Testing Period")
startYear = input.int(2020, "Start Year")
startMonth = input.int(1, "Start Month")
endYear = input.int(2025, "End Year")
endMonth = input.int(10, "End Month")
// === TIME RANGE ===
startTime = timestamp(startYear, startMonth, 1, 00, 00)
endTime = timestamp(endYear, endMonth + 1, 1, 00, 00) - 1
inRange = not usePeriod or (time >= startTime and time <= endTime)
[high15, low15, close15, open15] = request.security(syminfo.tickerid, timeframe.period, [high, low, close, open])
k15 = ta.sma(ta.stoch(close15, high15, low15, kPeriod), smoothK)
d15 = ta.sma(k15, dPeriod)
isPeak15 = k15 > overboughtLevel and ta.crossunder(k15, d15)
isFalseBrk = SW_counter > 80 ? (k15 < 70 and ta.crossunder(k15, d15)) : (k15 > 65 and ta.crossunder(k15, d15)) // Short at early phase of SW
isRebound = k15 > 30 and ta.crossover(k15, d15)
isBottom15 = k15 < oversoldLevel and ta.crossover(k15, d15)
isPullback = k15 < 35 and ta.crossover(k15, d15)
if barstate.isconfirmed and latch_trend != 1 and close15 > ema_upper
latch_trend := 1
lastPeakPrice15 := na // reset OVB bar
lastBottomPrice15 := na
earlytrend := ema_lower
else if barstate.isconfirmed and latch_trend!= -1 and close15 < ema_lower
latch_trend := -1
earlytrend := ema_upper
lastPeakPrice15 := na // reset OVB bar
lastBottomPrice15 := na
trendMarket = latch_trend ==1 and barstate.isconfirmed
sidewaysMarket = latch_trend ==-1 and barstate.isconfirmed
// Code Start Here
if usePeriod and time > endTime
strategy.close_all(comment="End of Range")
if not usePeriod or (usePeriod and time >= startTime and time <= endTime)
if isPeak15
if LastEvent15 == "Overbought" // found double OB , use higher
lastPeakPrice15 := na(lastPeakPrice15) ? high15 : math.max(lastPeakPrice15, high15)
else
lastPeakPrice15 := high15
LastEvent15 := "Overbought"
if isBottom15
if LastEvent15 == "Oversold" // found double SD , usd lower
lastBottomPrice15 := na(lastBottomPrice15) ? low15 : math.min(lastBottomPrice15, low15)
else
lastBottomPrice15 := low15
LastEvent15 := "Oversold"
if trendMarket
// Clear S position
SW_counter := 0
if strategy.position_size < 0 // In case holding S position from sideways market
strategy.close("Short BRK", comment="Trend Change @ " + str.tostring(close15, "#,###"))
strategy.close("Short OVB", comment="Trend Change @ " + str.tostring(close15, "#,###"))
isSafeLong = close15 < ema_upper-10.0 and close15 >= ema200-20.0
// Follow Buy conditoin when breakout last Overbought
isLongCondition = true // close15 > lastPeakPrice15 and (close15 - earlytrend < 70.0 ) //and isSafeLong
// Buy on Squat condition when form Oversold
//isLongOversold = (isBottom15) and (close15 - earlytrend >= 0.0 ) and isSafeLong
isLongOversold =(close15 - earlytrend >= 40.0) and ((close15 > ema200 and close[1] <= ema200 and isSafeLong) or ((isBottom15) and isSafeLong))
//Open L
if strategy.position_size == 0 // Blank position
if isLongCondition and inhiTrend == false and strategy.position_size == 0
strategy.entry("Long BRK", strategy.long, comment="Long BRK " + str.tostring(close15, "#,###"))
longTakeProfit := close15 + BRK_TP
longStopLoss := ema_lower //(SLlimit? close15 - maxSL : lastPeakPrice15 -5.0)
longStopLossOVS := ema_lower
long_cost := close15
L_mode := 1 // BRK
//strategy.exit("TP Long BRK " + str.tostring(longTakeProfit,"#,###"), from_entry="Long BRK", limit=longTakeProfit)
if isLongOversold and inhiTrend == false
strategy.entry("Long OVS" , strategy.long, comment = "OVS 1 " + str.tostring(close15, "#,###"))
longStopLossOVS := ema_lower //math.min(lastBottomPrice15 - 5.0,ema200-5.0)
//longTakeProfitOVS := close15 + 15.0
long_cost := close15
L_mode := 2 // OVS
// Has L or S position
else if strategy.position_size > 0 // Hold L position
if isLongOversold and inhiTrend == false and close15 < long_cost-5.0
strategy.entry("Long OVS 2" , strategy.long , comment = "OVS 2 " + str.tostring(close15, "#,###"))
longStopLossOVS := ema_lower // lastBottomPrice15 - 20.0
//longTakeProfitOVS := close15 + 15.0
long_cost := (long_cost+close15)/2
isLongWin = close15 > long_cost + 10.0 and ((close15 < ema_upper and isPeak15) or (close[1]>=ema_upper and close15<ema_upper))
isLongLoss = close15 <= longStopLossOVS
isTrailingBRK = close15 > longTakeProfit and close15 > lastPeakPrice15
//if isTrailingBRK and L_mode == 1 // BRK
//longTakeProfit := longTakeProfit + 10.0
//label.new(bar_index, high15,text = "trailing ="+ str.tostring(close15, "#,###"), style=label.style_label_down, size=size.small)
isLongWinBRK = close15 >= longTakeProfit and close15 < ema_upper
isLongLossBRK = close15 <= longStopLoss
// Stop loss L
if isLongLossBRK
strategy.close("Long BRK", comment="SL Long BRK @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
//if close15 <= longStopLossOVS
if isLongLoss
if strategy.position_size == 2
strategy.close_all(comment="SL OVS @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
else
strategy.close("Long OVS", comment="SL Long OVS @"+ str.tostring(close15, "#,###"))
strategy.close("Long OVS 2", comment="SL Long OVS @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
//if close15 > longTakeProfitOVS //(close15 > longTakeProfitOVS -8.0 and isFalseBrk)
if isLongWin
if strategy.position_size == 2
strategy.close_all(comment="TP OVS @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
else
strategy.close("Long OVS", comment="TP OVS 1@"+ str.tostring(close15, "#,###"))
strategy.close("Long OVS 2", comment="TP OVS 2 @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
if false // isLongWinBRK
strategy.close("Long BRK", comment="TP Long BRK @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
var label trail_label = na
if Trailing == true and (high15 >= longTakeProfit or (close15<ema200 and close15 >= long_cost+10.0)) // any part of price hit tarket
if isLongCondition // meet creteria to open L again
longTakeProfit := close15 + 80.0
longStopLoss := (SLlimit? close15 - 15.0: lastBottomPrice15)
trail_label := label.new(bar_index, high15,text = "trailing ="+ str.tostring(close15, "#,###"), style=label.style_label_down, size=size.small)
else // Take Profit
strategy.close("Long BRK", comment="Reach" + str.tostring(longTakeProfit,"#,###"))
else if sidewaysMarket
SW_counter := SW_counter + 1
L_Rebound = SW_counter > 80 and close[2] < ema_lower and close[1] >= ema_lower and close15 > ema_lower //and k15 < 60
if strategy.position_size > 0
if SW_counter < 10 // close15 < longStopLoss // In case holding L position from Trend market
strategy.close("Long BRK", comment="Reverse SW " + str.tostring(close15, "#,###") )
L_mode := 0 // clear
if SW_counter < 10 // close15 < longStopLossOVS
strategy.close_all(comment="Stop all " + str.tostring(close15, "#,###"))
//strategy.close("Long OVS", comment="Stop Oversold " + str.tostring(close15, "#,###") )
//strategy.close("Long OVS 2", comment="SL Long OVS @"+ str.tostring(close15, "#,###"))
L_mode := 0 // clear
if SW_counter < 10 //close15 >= ema200-5.0
strategy.close("Long Rebound", comment="TP Rebound " + str.tostring(close15, "#,###") )
if strategy.position_size == 0 and L_Rebound and inhiSideway == false
strategy.entry("Long Rebound", strategy.long, comment="Rebound " + str.tostring(close15, "#,###"))
strategy.exit("Exit Long Rebound",from_entry="Long Rebound", stop = ema_lower - (ema_lower*2*trend_gap_p/100) , comment = "SL Rebound")
var label DebugLabel = na
label.delete(DebugLabel)
if not na(latch_trend)
DebugLabel := label.new(bar_index, high15, text="trend " + str.tostring(latch_trend,"#") , style=label.style_label_down, color=color.blue, textcolor=color.white, size=size.small)
// Plot Bollinger Bands
//plot(sidewaysMarket ? lastBottomPrice15 : na , color=color.yellow, style=plot.style_circles)
//plot(sidewaysMarket ? lastPeakPrice15 : na , color=color.blue, style=plot.style_circles)
plot(trendMarket ? lastBottomPrice15 : na, color=color.red, style=plot.style_circles)
plot(trendMarket ? lastPeakPrice15 : na, color=color.green, style=plot.style_circles)
bgcolor(sidewaysMarket ? color.new(color.black, 90) : na)
bgcolor(trendMarket ? color.new(color.lime, 90) : na)
// Plot the three lines
plot(ema200, title="EMA 200", color=color.white)
plot(ema_upper, title="EMA 200 + 20", color=color.white)
plot(ema_lower, title="EMA 200 - 20", color=color.white)