OPEN-SOURCE SCRIPT

Breakout buy and sell

85
//version=6
indicator("突破 + 反轉指標(嚴格版)", overlay=true)

// 均線計算
ma5 = ta.sma(close, 5)
ma20 = ta.sma(close, 20)
ma_cross_up = ta.crossover(ma5, ma20)
ma_cross_down = ta.crossunder(ma5, ma20)

// 成交量判斷(嚴格:放量 1.5 倍以上)
vol = volume
vol_avg = ta.sma(vol, 20)
vol_increase = vol > vol_avg * 1.5

// 價格突破(嚴格:創 20 根新高/新低)
price_breakout_up = close > ta.highest(close, 20)
price_breakout_down = close < ta.lowest(close, 20)

// KDJ 隨機指標(抓反轉)
k = ta.stoch(close, high, low, 14)
d = ta.sma(k, 3)
kd_cross_up = ta.crossover(k, d)
kd_cross_down = ta.crossunder(k, d)

// 時間過濾(美股正規交易時段)
isRegularSession = (hour >= 14 and hour < 21) or (hour == 21 and minute == 0)

// 冷卻時間(避免連續訊號)
var int lastEntryBar = na
var int lastExitBar = na
entryCooldown = na(lastEntryBar) or (bar_index - lastEntryBar > 5)
exitCooldown = na(lastExitBar) or (bar_index - lastExitBar > 5)

// 嚴格進場條件
entryBreakout = ma_cross_up and vol_increase and price_breakout_up
entryReversal = kd_cross_up
entrySignal = (entryBreakout or entryReversal) and isRegularSession and entryCooldown

// 嚴格出場條件
exitBreakdown = ma_cross_down and vol_increase and price_breakout_down
exitReversal = kd_cross_down
exitSignal = (exitBreakdown or exitReversal) and isRegularSession and exitCooldown

// 更新冷卻時間
if entrySignal
lastEntryBar := bar_index
if exitSignal
lastExitBar := bar_index

// 顯示訊號
plotshape(entrySignal, location=location.belowbar, color=color.green, style=shape.labelup, title="進場訊號", text="買入")
plotshape(exitSignal, location=location.abovebar, color=color.red, style=shape.labeldown, title="出場訊號", text="賣出")

// 均線顯示
plot(ma5, color=color.orange, title="5MA")
plot(ma20, color=color.blue, title="20MA")

// 提醒條件(alertcondition)
alertcondition(entrySignal, title="買入訊號", message="出現買入訊號")
alertcondition(exitSignal, title="賣出訊號", message="出現賣出訊號")

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.