本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("UT Bot Alerts (T-15s Confirm)", overlay=true)
// Inputs
a = input.int(1, title="Key Value. Sensitivity")
c = input.int(10, title="ATR Period")
h = input.bool(false, title="Signals from Heikin Ashi Candles")
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) : close
var float xATRTrailingStop = na
xATRTrailingStop := src > nz(xATRTrailingStop[1]) and src[1] > nz(xATRTrailingStop[1]) ?
math.max(nz(xATRTrailingStop[1]), src - nLoss) :
src < nz(xATRTrailingStop[1]) and src[1] < nz(xATRTrailingStop[1]) ?
math.min(nz(xATRTrailingStop[1]), src + nLoss) :
src > nz(xATRTrailingStop[1]) ? src - nLoss : src + nLoss
var int pos = 0
pos := src[1] < nz(xATRTrailingStop[1]) and src > nz(xATRTrailingStop[1]) ? 1 :
src[1] > nz(xATRTrailingStop[1]) and src < nz(xATRTrailingStop[1]) ? -1 :
nz(pos[1])
emaVal = ta.ema(src, 1)
above = ta.crossover(emaVal, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, emaVal)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
plotshape(buy, title="Buy", text="Buy", style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, size=size.tiny)
plotshape(sell, title="Sell", text="Sell", style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, size=size.tiny)
barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)
// ===== T-15 seconds confirmation (signal must still be valid) =====
ms_to_close = time_close - timenow
tminus15 = ms_to_close <= 15000 and ms_to_close > 0
newbar = ta.change(time) != 0
// Track whether a Buy/Sell label has appeared at any point during this candle
var bool buy_seen_this_bar = false
var bool sell_seen_this_bar = false
// Ensure we only fire once per candle
var bool sentLong = false
var bool sentShort = false
if newbar
buy_seen_this_bar := false
sell_seen_this_bar := false
sentLong := false
sentShort := false
// Record crossover event whenever it happens during the candle
if buy
buy_seen_this_bar := true
if sell
sell_seen_this_bar := true
// Confirm at T-15 that the signal is still "there" (i.e. state still matches)
buy_t15 = barstate.isrealtime and tminus15 and buy_seen_this_bar and barbuy and not sentLong
sell_t15 = barstate.isrealtime and tminus15 and sell_seen_this_bar and barsell and not sentShort
if buy_t15
sentLong := true
if sell_t15
sentShort := true
// Alerts (optional originals)
alertcondition(buy, "UT Long", "UT Long")
alertcondition(sell, "UT Short", "UT Short")
// Alerts you actually use
alertcondition(buy_t15, "UT Long T-15", "UT Long T-15")
alertcondition(sell_t15, "UT Short T-15", "UT Short T-15")