本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("BTM Triple Confluence", shorttitle="BTM Triple", overlay=true)
// ── Settings ──
sarStart = input.float(0.02, "Start", minval=0.001, group="Parabolic SAR")
sarStep = input.float(0.02, "Increment", minval=0.001, group="Parabolic SAR")
sarMax = input.float(0.20, "Maximum", minval=0.001, group="Parabolic SAR")
kLength = input.int(14, "%K length", minval=1, group="Stochastic")
kSmooth = input.int(3, "%K smoothing", minval=1, group="Stochastic")
dLength = input.int(3, "%D smoothing", minval=1, group="Stochastic")
zoneFilter = input.bool(false, "Require crossover in extreme zone", group="Stochastic")
oversold = input.float(20, "Oversold", minval=0, maxval=49, group="Stochastic")
overbought = input.float(80, "Overbought", minval=51, maxval=100, group="Stochastic")
obvLength = input.int(20, "OBV EMA length", minval=1, group="OBV")
allowFallback = input.bool(false, "Allow signals without volume", group="OBV", tooltip="Allows SAR + Stochastic signals when volume is missing or zero. OBV confirmation is bypassed.")
windowBars = input.int(0, "Extra confirmation candles", minval=0, maxval=20, group="Signals", tooltip="0 = crossover candle only. 2 = crossover candle plus the next two. Opposite crosses cancel pending setups.")
useCooldown = input.bool(false, "Enable signal cooldown", group="Signals")
cooldownBars = input.int(5, "Minimum candles between signals", minval=1, maxval=500, group="Signals")
showSar = input.bool(true, "Show SAR dots", group="Display")
showPanel = input.bool(true, "Show confirmation panel", group="Display")
// ── Calculations ──
sar = ta.sar(sarStart, sarStep, sarMax)
k = ta.sma(ta.stoch(close, high, low, kLength), kSmooth)
d = ta.sma(k, dLength)
obv = ta.obv
obvAverage = ta.ema(obv, obvLength)
crossUp = ta.crossover(k, d)
crossDown = ta.crossunder(k, d)
ready = not na(sar) and not na(k) and not na(d)
volumeAvailable = not na(volume) and volume > 0
obvReady = volumeAvailable and not na(obv) and not na(obvAverage)
fallbackActive = not volumeAvailable and allowFallback
obvBull = (obvReady and obv > obvAverage) or fallbackActive
obvBear = (obvReady and obv < obvAverage) or fallbackActive
// ── Signal logic ──
var int pendingDirection = 0
var int pendingBar = na
var int lastSignalBar = na
bool buySignal = false
bool sellSignal = false
if barstate.isconfirmed
if pendingDirection != 0
if bar_index - pendingBar > windowBars
pendingDirection := 0
pendingBar := na
// A new crossover cancels the previous setup.
if crossUp or crossDown
pendingDirection := 0
pendingBar := na
if crossUp and (not zoneFilter or (k <= oversold and d <= oversold))
pendingDirection := 1
pendingBar := bar_index
else if crossDown and (not zoneFilter or (k >= overbought and d >= overbought))
pendingDirection := -1
pendingBar := bar_index
cooldownReady = not useCooldown or na(lastSignalBar) or bar_index - lastSignalBar >= cooldownBars
if ready and cooldownReady
buySignal := pendingDirection == 1 and close > sar and k > d and obvBull
sellSignal := pendingDirection == -1 and close < sar and k < d and obvBear
// Each crossover can generate only one signal.
if buySignal or sellSignal
lastSignalBar := bar_index
pendingDirection := 0
pendingBar := na
// ── Chart plots ──
plot(showSar ? sar : na, title="Parabolic SAR", color=close > sar ? color.teal : color.orange, style=plot.style_circles)
plotshape(buySignal, title="Buy signal", text="BUY", style=shape.labelup, location=location.belowbar, color=color.teal, textcolor=color.white, size=size.small)
plotshape(sellSignal, title="Sell signal", text="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.small)
plot(k, title="Stochastic %K", display=display.data_window)
plot(d, title="Stochastic %D", display=display.data_window)
plot(obv, title="OBV", display=display.data_window)
plot(obvAverage, title="OBV EMA", display=display.data_window)
// ── Alerts ──
alertcondition(buySignal and not fallbackActive, title="Buy signal", message="BTM BUY: SAR + Stochastic + OBV on {{ticker}} ({{interval}}). Close: {{close}}")
alertcondition(sellSignal and not fallbackActive, title="Sell signal", message="BTM SELL: SAR + Stochastic + OBV on {{ticker}} ({{interval}}). Close: {{close}}")
alertcondition(buySignal and fallbackActive, title="Buy signal - no volume", message="BTM BUY: SAR + Stochastic ONLY; no OBV confirmation on {{ticker}} ({{interval}}). Close: {{close}}")
alertcondition(sellSignal and fallbackActive, title="Sell signal - no volume", message="BTM SELL: SAR + Stochastic ONLY; no OBV confirmation on {{ticker}} ({{interval}}). Close: {{close}}")
// ── Last closed candle snapshot ──
// The dashboard stays aligned with closed-candle signals.
var string sarStatus = "Warming up"
var string stochStatus = "Warming up"
var string obvStatus = "Warming up"
var string modeStatus = "Warming up"
var string setupStatus = "None"
var string cooldownStatus = "Off"
if barstate.isconfirmed
sarStatus := na(sar) ? "Warming up" : close > sar ? "Bullish" : close < sar ? "Bearish" : "Neutral"
stochStatus := na(k) or na(d) ? "Warming up" : k > d ? "Bullish" : k < d ? "Bearish" : "Neutral"
obvStatus := not volumeAvailable ? "No volume" : not obvReady ? "Warming up" : obv > obvAverage ? "Bullish" : obv < obvAverage ? "Bearish" : "Neutral"
modeStatus := fallbackActive ? "Fallback" : not volumeAvailable ? "Blocked" : not ready or not obvReady ? "Warming up" : "Full"
setupStatus := buySignal ? "BUY confirmed" : sellSignal ? "SELL confirmed" : pendingDirection == 1 ? "Buy pending" : pendingDirection == -1 ? "Sell pending" : "None"
remaining = na(lastSignalBar) ? 0 : math.max(0, cooldownBars - (bar_index - lastSignalBar))
cooldownStatus := not useCooldown ? "Off" : remaining > 0 ? str.tostring(remaining) + " candles remaining" : "Ready"
// ── Dashboard palette ──
panelBg = color.rgb(12, 20, 34)
labelBg = color.rgb(19, 31, 49)
headerBg = color.rgb(25, 40, 61)
borderCol = color.rgb(48, 68, 91)
goldCol = color.rgb(238, 190, 85)
whiteCol = color.rgb(235, 242, 250)
mutedCol = color.rgb(153, 177, 204)
bullBg = color.rgb(12, 66, 57)
bullText = color.rgb(71, 229, 173)
bearBg = color.rgb(77, 28, 43)
bearText = color.rgb(255, 115, 137)
amberBg = color.rgb(73, 53, 23)
amberText = color.rgb(255, 207, 107)
blueBg = color.rgb(23, 49, 79)
blueText = color.rgb(116, 192, 255)
// ── Dashboard styling ──
statusBg(string value) =>
switch value
"Bullish" => bullBg
"Bearish" => bearBg
"No volume" => amberBg
=> blueBg
statusText(string value) =>
switch value
"Bullish" => bullText
"Bearish" => bearText
"No volume" => amberText
=> blueText
statusLabel(string value) =>
switch value
"Bullish" => "▲ BULLISH"
"Bearish" => "▼ BEARISH"
"Neutral" => "◆ NEUTRAL"
"No volume" => "! NO VOLUME"
=> "WARMING UP"
// ── Dashboard ──
var table panel = table.new(position.top_right, 2, 8, bgcolor=panelBg, frame_color=borderCol, frame_width=2, border_color=panelBg, border_width=2)
if barstate.isfirst
table.merge_cells(panel, 0, 0, 1, 0)
table.merge_cells(panel, 0, 1, 1, 1)
if barstate.islast
if showPanel
table.cell(panel, 0, 0, "BTM | TRIPLE CONFLUENCE", bgcolor=headerBg, text_color=goldCol, text_size=size.normal)
table.cell(panel, 0, 1, "CONFIRMATION • LAST CLOSED CANDLE", bgcolor=panelBg, text_color=mutedCol, text_size=size.small)
table.cell(panel, 0, 2, "TREND / SAR", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 2, statusLabel(sarStatus), bgcolor=statusBg(sarStatus), text_color=statusText(sarStatus), text_size=size.small)
table.cell(panel, 0, 3, "MOMENTUM / STOCH", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 3, statusLabel(stochStatus), bgcolor=statusBg(stochStatus), text_color=statusText(stochStatus), text_size=size.small)
table.cell(panel, 0, 4, "VOLUME / OBV", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 4, statusLabel(obvStatus), bgcolor=statusBg(obvStatus), text_color=statusText(obvStatus), text_size=size.small)
modeLabel = modeStatus == "Full" ? "● FULL CONFIRMATION" : modeStatus == "Blocked" ? "■ BLOCKED: NO VOLUME" : modeStatus == "Fallback" ? "! OBV BYPASSED" : "WARMING UP"
modeBg = modeStatus == "Full" ? bullBg : modeStatus == "Blocked" ? bearBg : amberBg
modeText = modeStatus == "Full" ? bullText : modeStatus == "Blocked" ? bearText : amberText
table.cell(panel, 0, 5, "SIGNAL MODE", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 5, modeLabel, bgcolor=modeBg, text_color=modeText, text_size=size.small)
setupBuy = setupStatus == "BUY confirmed"
setupSell = setupStatus == "SELL confirmed"
setupPending = setupStatus == "Buy pending" or setupStatus == "Sell pending"
setupLabel = setupBuy ? "▲ BUY CONFIRMED" : setupSell ? "▼ SELL CONFIRMED" : setupStatus == "Buy pending" ? "BUY PENDING" : setupStatus == "Sell pending" ? "SELL PENDING" : "◆ WAITING FOR SETUP"
setupBg = setupBuy ? bullBg : setupSell ? bearBg : setupPending ? amberBg : blueBg
setupText = setupBuy ? bullText : setupSell ? bearText : setupPending ? amberText : blueText
table.cell(panel, 0, 6, "SETUP", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 6, setupLabel, bgcolor=setupBg, text_color=setupText, text_size=size.small)
cooldownWaiting = cooldownStatus != "Off" and cooldownStatus != "Ready"
cooldownLabel = cooldownStatus == "Off" ? "OFF" : cooldownStatus == "Ready" ? "● READY" : cooldownStatus
table.cell(panel, 0, 7, "COOLDOWN", bgcolor=labelBg, text_color=whiteCol, text_size=size.small, text_halign=text.align_left)
table.cell(panel, 1, 7, cooldownLabel, bgcolor=cooldownWaiting ? amberBg : blueBg, text_color=cooldownWaiting ? amberText : blueText, text_size=size.small)
else
table.clear(panel, 0, 0, 1, 7)