本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("UT Bot + MACD BUY Delayed Confirm v6", overlay=true)
//━━━━━━━━ INPUTS ━━━━━━━━
a = input.int(1, title="Key Value")
c = input.int(10, title="ATR Period")
h = input.bool(false, title="Use Heikin Ashi")
fastLen = input.int(12, title="MACD Fast")
slowLen = input.int(26, title="MACD Slow")
signalLen = input.int(9, title="MACD Signal")
//━━━━━━━━ SOURCE ━━━━━━━━
src = h
? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close)
: close
//━━━━━━━━ ATR ━━━━━━━━
xATR = ta.atr(c)
nLoss = a * xATR
//━━━━━━━━ TRAILING STOP ━━━━━━━━
var float xATRTrailingStop = na
prevStop = nz(xATRTrailingStop[1], src)
xATRTrailingStop :=
src > prevStop and src[1] > prevStop ? math.max(prevStop, src - nLoss) :
src < prevStop and src[1] < prevStop ? math.min(prevStop, src + nLoss) :
src > prevStop ? src - nLoss :
src + nLoss
//━━━━━━━━ UT BOT BUY ━━━━━━━━
utBuy = src > xATRTrailingStop and src[1] <= prevStop
//━━━━━━━━ MACD ━━━━━━━━
macdLine = ta.ema(src, fastLen) - ta.ema(src, slowLen)
signalLine = ta.ema(macdLine, signalLen)
macdBuy =
ta.crossover(macdLine, signalLine) and
macdLine > 0 and
signalLine > 0
//━━━━━━━━ MEMORY LOGIC ━━━━━━━━
var bool utBuyActive = false
// Store UT Buy first
if utBuy
utBuyActive := true
// Final buy only when MACD confirms later
buySignal = utBuyActive and macdBuy
// Reset memory after confirmation
if buySignal
utBuyActive := false
//━━━━━━━━ PLOTS ━━━━━━━━
plot(xATRTrailingStop, color=color.red, title="ATR Trailing Stop")
plotshape(
buySignal,
title="BUY",
text="BUY",
style=shape.labelup,
location=location.belowbar,
color=color.green,
textcolor=color.white,
size=size.small
)
barcolor(buySignal ? color.new(color.green, 0) : na)
//━━━━━━━━ ALERT ━━━━━━━━
alertcondition(
buySignal,
title="UT + MACD BUY (Delayed)",
message="UT Bot Buy occurred earlier, MACD bullish crossover confirmed later"
)