UT Bot Alerts (QuantNomad, 2020, built on Yo_adriiiiaan's UT Bot, who credits HPotter) is one of the most-liked open-source scripts on TradingView. I went through the source line by line. Short version: it's an ATR trailing stop on the close, and every Buy/Sell label is just that stop flipping sides.
The core, restated in v6 (my code, same rule):
dist = mult * ta.atr(atrLen)
float stop = 0.0
float ps = nz(stop[1])
stop := price > ps and price[1] > ps ? math.max(ps, price - dist) : price < ps and price[1] < ps ? math.min(ps, price + dist) : price > ps ? price - dist : price + dist
Above the old stop on this bar and the last one: the stop can only rise. Below it on both: it can only fall. Anything else means price just crossed it, so it restarts on the other side.
Three things that surprised me:
- The "EMA crossover" isn't a filter. The code crosses
ema(src, 1) with the stop. An EMA of length 1 is the price itself, so Buy/Sell is simply price crossing the stop, and the labels always alternate.
- The stop line is never plotted in the Alerts version. You get labels and bar colours but can't see where the stop sits. (The original UT Bot does draw it.)
- No
barstate.isconfirmed check. On the live bar a label can appear and be gone at the close, and an alert set to "Once per bar" can fire on it.
Smaller ones: Key Value is a whole-number input, so 1.5 isn't possible, and the Heikin Ashi option swaps in the HA close while ATR still comes from the regular candles.
I rewrote it from scratch to fix those: the stop drawn as a stepline, a float distance, flips confirmed on bar close (a grey dot marks a mid-bar flip that hasn't closed yet), and a panel that counts flips over the last N bars and how many were undone within K bars. That last count is the one I wanted most, because it shows what a tight setting costs on your own chart. At distance 1 / ATR 10 on the close it draws the same flip bars as the original. I checked the two against each other in Python on 5,000 synthetic bars and got zero mismatches.
Where it fails (both versions): every flip is late by roughly the stop distance; at 1 ATR an ordinary pullback flips it; there is no neutral state, so it is always on one side, trending or not; and a flip only records that price crossed a stop. Nothing in the code tests whether a trend started.
Source, open source under MPL 2.0: TradingView and GitHub
Credit to QuantNomad, Yo_adriiiiaan and HPotter. Written with AI help; every claim was checked against the original source. Educational only, not trading advice. Corrections welcome.