本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("UT Bot + EMA + RSI Indicator (Alignment Fixed)", overlay=true, max_labels_count=500, max_lines_count=500)
// --- INPUTS ---
var float kv = input.float(1.0, "UT Key Value")
var int atrp = input.int(10, "UT ATR Period")
var int f_ema = input.int(9, "Fast EMA")
var int s_ema = input.int(20, "Slow EMA")
var float rr = input.float(2.0, "Risk:Reward Ratio")
var int line_len = input.int(15, "Target Line Length (Bars)")
// Filters
var int rsi_buy = input.int(55, "RSI Buy")
var int rsi_sell = input.int(45, "RSI Sell")
var int fresh_window = input.int(15, "EMA Cross Window")
// --- CALCULATIONS ---
float xATR = ta.atr(atrp)
float nLoss = kv * xATR
var float xTS = 0.0
xTS := close > nz(xTS[1]) and close[1] > nz(xTS[1]) ? math.max(nz(xTS[1]), close - nLoss) :
close < nz(xTS[1]) and close[1] < nz(xTS[1]) ? math.min(nz(xTS[1]), close + nLoss) :
close > nz(xTS[1]) ? close - nLoss : close + nLoss
float ema9 = ta.ema(close, f_ema)
float ema20 = ta.ema(close, s_ema)
float rsi = ta.rsi(close, 14)
bool recent_bull = ta.barssince(ta.crossover(ema9, ema20)) <= fresh_window
bool recent_bear = ta.barssince(ta.crossunder(ema9, ema20)) <= fresh_window
// --- ALIGNMENT LOGIC (Fixed) ---
// We check if all conditions are ALIGNED, rather than perfectly timed crossovers.
// barstate.isconfirmed ensures labels don't flicker on live candles.
bool bull_aligned = (close > xTS) and (ema9 > ema20) and recent_bull and (rsi > rsi_buy) and barstate.isconfirmed
bool bear_aligned = (close < xTS) and (ema9 < ema20) and recent_bear and (rsi < rsi_sell) and barstate.isconfirmed
// --- STATE TRACKING (Prevents label spam) ---
var bool in_long = false
var bool in_short = false
bool buy_trigger = bull_aligned and not in_long
bool sell_trigger = bear_aligned and not in_short
if buy_trigger
in_long := true
in_short := false
if sell_trigger
in_short := true
in_long := false
// Reset states when the Cloud completely flips
if ta.crossunder(ema9, ema20)
in_long := false
if ta.crossover(ema9, ema20)
in_short := false
// --- VISUALS ---
p1 = plot(ema9, "9 EMA", color.orange, 1)
p2 = plot(ema20, "20 EMA", color.blue, 2)
fill(p1, p2, ema9 > ema20 ? color.new(color.green, 90) : color.new(color.red, 90))
plot(xTS, "UT Trailing Stop", color=color.new(#d500f9, 30), style=plot.style_linebr, linewidth=2)
draw_rr(entry, sl, tp) =>
int end_bar = bar_index + line_len
line.new(bar_index, entry, end_bar, entry, color=color.new(color.yellow, 50), width=1, style=line.style_dashed)
line.new(bar_index, sl, end_bar, sl, color=color.new(color.red, 30), width=2)
line.new(bar_index, tp, end_bar, tp, color=color.new(color.green, 30), width=2)
// --- SIGNAL EXECUTION ---
if buy_trigger
label.new(bar_index, low, "B", color=color.green, textcolor=color.white, style=label.style_label_up, size=size.small)
float sl_p = low[1]
float risk = close - sl_p
if risk > 0
draw_rr(close, sl_p, close + (risk * rr))
if sell_trigger
label.new(bar_index, high, "S", color=color.red, textcolor=color.white, style=label.style_label_down, size=size.small)
float sl_p = high[1]
float risk = sl_p - close
if risk > 0
draw_rr(close, sl_p, close - (risk * rr))
// --- ALERTS ---
alertcondition(buy_trigger, "Long Entry", "UT Bot Buy Signal")
alertcondition(sell_trigger, "Short Entry", "UT Bot Sell Signal")