本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=5
indicator(title="UT Bot Alerts with R-Targets & Results", overlay=true, max_labels_count=500, max_lines_count=500)
// ============================================
// INPUTS
// ============================================
a = input.int(1, title="Key Value. 'This changes the sensitivity'")
c = input.int(10, title="ATR Period")
h = input.bool(false, title="Signals from Heikin Ashi Candles")
// ============================================
// UT BOT LOGIC (Original)
// ============================================
xATR = ta.atr(c)
nLoss = a * xATR
src = h ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=barmerge.lookahead_off) : close
var float xATRTrailingStop = 0.0
xATRTrailingStop := src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0) ? math.max(nz(xATRTrailingStop[1]), src - nLoss) :
src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0) ? math.min(nz(xATRTrailingStop[1]), src + nLoss) :
src > nz(xATRTrailingStop[1], 0) ? src - nLoss : src + nLoss
var int pos = 0
pos := src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0) ? 1 :
src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0) ? -1 : nz(pos[1], 0)
xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue
ema = ta.ema(src, 1)
above = ta.crossover(ema, xATRTrailingStop)
below = ta.crossover(xATRTrailingStop, ema)
buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below
barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop
// ============================================
// TRADE MANAGEMENT SYSTEM
// ============================================
var int activeDir = 0
var float entryPrice = na
var float slPrice = na
var float tp1Price = na
var float tp2Price = na
var float tp3Price = na
var int entryIndex = na
var bool tradeClosedThisBar = false
// Reset flag at bar start
tradeClosedThisBar := false
// --------------------------------------------
// CHECK EXITS FOR LONG TRADES
// --------------------------------------------
if activeDir == 1
if low <= slPrice
label.new(bar_index, low, "Result: SL", style=label.style_label_up, color=color.red, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if high >= tp3Price
label.new(bar_index, high, "Result: TP3", style=label.style_label_down, color=color.purple, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if high >= tp2Price
label.new(bar_index, high, "Result: TP2", style=label.style_label_down, color=color.blue, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if high >= tp1Price
label.new(bar_index, high, "Result: TP1", style=label.style_label_down, color=color.lime, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
// --------------------------------------------
// CHECK EXITS FOR SHORT TRADES
// --------------------------------------------
if activeDir == -1 and not tradeClosedThisBar
if high >= slPrice
label.new(bar_index, high, "Result: SL", style=label.style_label_down, color=color.red, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if low <= tp3Price
label.new(bar_index, low, "Result: TP3", style=label.style_label_up, color=color.purple, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if low <= tp2Price
label.new(bar_index, low, "Result: TP2", style=label.style_label_up, color=color.blue, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
else if low <= tp1Price
label.new(bar_index, low, "Result: TP1", style=label.style_label_up, color=color.lime, textcolor=color.white, size=size.small)
activeDir := 0
tradeClosedThisBar := true
// --------------------------------------------
// NEW BUY SIGNAL
// --------------------------------------------
if buy
if activeDir == -1
label.new(bar_index, high, "Exit: Reverse", style=label.style_label_down, color=color.orange, textcolor=color.white, size=size.small)
activeDir := 1
entryPrice := close
slPrice := xATRTrailingStop
float rLong = math.abs(entryPrice - slPrice)
tp1Price := entryPrice + 1 * rLong
tp2Price := entryPrice + 2 * rLong
tp3Price := entryPrice + 3 * rLong
entryIndex := bar_index
tradeClosedThisBar := false
label.new(bar_index, tp1Price, "TP1\n" + str.tostring(tp1Price, format.mintick), style=label.style_label_left, color=color.lime, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, tp2Price, "TP2\n" + str.tostring(tp2Price, format.mintick), style=label.style_label_left, color=color.blue, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, tp3Price, "TP3\n" + str.tostring(tp3Price, format.mintick), style=label.style_label_left, color=color.purple, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, slPrice, "SL\n" + str.tostring(slPrice, format.mintick), style=label.style_label_left, color=color.red, textcolor=color.white, size=size.tiny, yloc=yloc.price)
// --------------------------------------------
// NEW SELL SIGNAL
// --------------------------------------------
if sell
if activeDir == 1
label.new(bar_index, low, "Exit: Reverse", style=label.style_label_up, color=color.orange, textcolor=color.white, size=size.small)
activeDir := -1
entryPrice := close
slPrice := xATRTrailingStop
float rShort = math.abs(slPrice - entryPrice)
tp1Price := entryPrice - 1 * rShort
tp2Price := entryPrice - 2 * rShort
tp3Price := entryPrice - 3 * rShort
entryIndex := bar_index
tradeClosedThisBar := false
label.new(bar_index, tp1Price, "TP1\n" + str.tostring(tp1Price, format.mintick), style=label.style_label_left, color=color.lime, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, tp2Price, "TP2\n" + str.tostring(tp2Price, format.mintick), style=label.style_label_left, color=color.blue, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, tp3Price, "TP3\n" + str.tostring(tp3Price, format.mintick), style=label.style_label_left, color=color.purple, textcolor=color.white, size=size.tiny, yloc=yloc.price)
label.new(bar_index, slPrice, "SL\n" + str.tostring(slPrice, format.mintick), style=label.style_label_left, color=color.red, textcolor=color.white, size=size.tiny, yloc=yloc.price)
// ============================================
// PLOTTING
// ============================================
bool showLines = activeDir != 0 or tradeClosedThisBar
plot(showLines ? slPrice : na, title="SL", color=color.red, linewidth=2, style=plot.style_linebr)
plot(showLines ? tp1Price : na, title="TP1", color=color.lime, linewidth=2, style=plot.style_linebr)
plot(showLines ? tp2Price : na, title="TP2", color=color.blue, linewidth=2, style=plot.style_linebr)
plot(showLines ? tp3Price : na, title="TP3", color=color.purple, linewidth=2, style=plot.style_linebr)
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 : barsell ? color.red : na)
alertcondition(buy, "UT Long", "UT Long")
alertcondition(sell, "UT Short", "UT Short")