// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not // distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. // © QuantNomad: "UT Bot Alerts", https://www.tradingview.com/script/n8ss8BID/ // Modified by ProEA, 2026: ported from Pine to TypeScript for a drill on simulated candles, with a reading taken // part-way through a candle. // Source of this file: https://proea.app/open-source/ut-bot.ts.txt, listed at https://proea.app/en/legal/open-source // // UT Bot Alerts as QuantNomad's original computes it (TradingView script n8ss8BID, Pine v4, code read 2026-10-31), pure: // no React, no DOM; the settings post's drill, its cover and its test import it. // // The script, line by line (its own names): // xATR = atr(c) ATR Period, default 10: Wilder's ATR on the chart's own candles, always // nLoss = a * xATR Key Value, default 1: the trail's distance from the source // src = h ? Heikin Ashi close : close "Signals from Heikin Ashi Candles", off: the HA close is (o + h + l + c) / 4 // xATRTrailingStop: while this src and the last are both above the last stop, the higher of the last stop and // src − nLoss; while both are below, the lower of the last stop and src + nLoss; otherwise (src has just crossed // it) src − nLoss if src is above the last stop, else src + nLoss. A missing last stop counts as 0 (nz). // buy = src > stop and crossover(ema(src, 1), stop) ema(src, 1) is src itself // sell = src < stop and crossover(stop, ema(src, 1)) // Nothing waits for the close: on the live candle, src is the current price, so a Buy or Sell can print and be gone at // the close. liveRead() reads a candle part-way through it, the way the script does on every price update. import { atr, type Candle } from "@/components/blog/supertrend/trail"; export type { Candle }; /** The script's three inputs. */ export type UtSettings = { key: number; len: number; ha: boolean }; /** The original's defaults: Key Value 1, ATR Period 10, Heikin Ashi off. */ export const ORIGINAL: UtSettings = { key: 1, len: 10, ha: false }; /** src on each candle; the trailing stop (NaN while the ATR warms up); Buy and Sell as the script plots them. */ export type UtRead = { src: number[]; stop: number[]; buy: boolean[]; sell: boolean[] }; /** The script's source: the close, or the Heikin Ashi close, (open + high + low + close) / 4 of the chart's candle. */ export const srcOf = ([, o, h, l, c]: Candle, ha: boolean) => (ha ? (o + h + l + c) / 4 : c); /** One step of xATRTrailingStop. `prev` is the last stop (NaN = na), `src1` the last source (NaN on the first candle: * Pine's comparisons with na are false). nz(prev, 0) is the script's; a missing nLoss (ATR warming up) gives na. */ export function stepStop(prev: number, src: number, src1: number, nLoss: number): number { if (!Number.isFinite(nLoss)) return NaN; const p = Number.isFinite(prev) ? prev : 0; if (src > p && src1 > p) return Math.max(p, src - nLoss); if (src < p && src1 < p) return Math.min(p, src + nLoss); return src > p ? src - nLoss : src + nLoss; } /** ta.crossover(a, b): above now, at or below on the last candle (false where anything is na). */ const crossover = (a: number, b: number, a1: number, b1: number) => a > b && a1 <= b1; export function utBot(cs: readonly Candle[], s: UtSettings): UtRead { return run(cs, s).r; } function run(cs: readonly Candle[], s: UtSettings): { a: number[]; r: UtRead } { const a = atr(cs, s.len); const src: number[] = [], stop: number[] = [], buy: boolean[] = [], sell: boolean[] = []; cs.forEach((c, i) => { src.push(srcOf(c, s.ha)); stop.push(stepStop(i ? stop[i - 1]! : NaN, src[i]!, i ? src[i - 1]! : NaN, s.key * a[i]!)); const s1 = i ? src[i - 1]! : NaN, t1 = i ? stop[i - 1]! : NaN; buy.push(src[i]! > stop[i]! && crossover(src[i]!, stop[i]!, s1, t1)); sell.push(src[i]! < stop[i]! && crossover(stop[i]!, src[i]!, t1, s1)); }); return { a, r: { src, stop, buy, sell } }; } /** Candle `j` part-way through it: the high and low so far, and the current price as its close (the open is the * candle's own). */ export type Part = [h: number, l: number, c: number]; export type Live = { buy: boolean; sell: boolean; stop: number; src: number }; /** What the script shows on candle `j` when only `part` of it has traded: everything before it closed, the live candle * read as it stands (its true range, so its ATR, its Heikin Ashi close and its stop, all from the partial candle, as * Pine recomputes them on each update). The plain form recomputes the whole chart with the partial candle last. */ export function liveRead(cs: readonly Candle[], s: UtSettings, j: number, part: Part): Live { const c = cs[j]!; const r = utBot([...cs.slice(0, j), [c[0], c[1], part[0], part[1], part[2]]], s); return { buy: r.buy[j]!, sell: r.sell[j]!, stop: r.stop[j]!, src: r.src[j]! }; } /** liveRead() from the closed candles' read and ATR, one step instead of the whole chart (the same numbers once the ATR * is past its first value; the test holds the two together). */ function liveStep(cs: readonly Candle[], s: UtSettings, base: { a: number[]; r: UtRead }, j: number, part: Part): Live { if (j < s.len || j < 1) return liveRead(cs, s, j, part); const [, o] = cs[j]!, [h, l, c] = part, pc = cs[j - 1]![4]; const tr = Math.max(h - l, Math.abs(h - pc), Math.abs(l - pc)); const a = (base.a[j - 1]! * (s.len - 1) + tr) / s.len; const src = s.ha ? (o + h + l + c) / 4 : c; const s1 = base.r.src[j - 1]!, t1 = base.r.stop[j - 1]!; const stop = stepStop(t1, src, s1, s.key * a); return { buy: src > stop && crossover(src, stop, s1, t1), sell: src < stop && crossover(stop, src, t1, s1), stop, src }; } /** A label the live candle printed that wasn't there at its close: candle `bar`, `at` = which partial reading (0, 1, …), * dir 1 = a Buy, −1 = a Sell. */ export type Ghost = { bar: number; at: number; dir: 1 | -1 }; export type Flip = { bar: number; dir: 1 | -1 }; /** Flips (Buy and Sell at the close) from candle `from` on, and the labels the live candle printed that its close * didn't keep (`parts[j]` holds candle j's partial readings, earliest first). */ export function readWindow(cs: readonly Candle[], parts: readonly (readonly Part[])[], s: UtSettings, from: number) { const base = run(cs, s), r = base.r; const flips: Flip[] = []; const ghosts: Ghost[] = []; for (let j = from; j < cs.length; j++) { if (r.buy[j]) flips.push({ bar: j, dir: 1 }); if (r.sell[j]) flips.push({ bar: j, dir: -1 }); const live = (parts[j] ?? []).map((p) => liveStep(cs, s, base, j, p)); const firstBuy = live.findIndex((l) => l.buy), firstSell = live.findIndex((l) => l.sell); if (!r.buy[j] && firstBuy >= 0) ghosts.push({ bar: j, at: firstBuy, dir: 1 }); if (!r.sell[j] && firstSell >= 0) ghosts.push({ bar: j, at: firstSell, dir: -1 }); } return { ...r, flips, ghosts }; } /** Median distance from the source to the line over candles `from` on, in price. */ export function medianGap(r: UtRead, from: number): number { const d = r.src.slice(from).map((x, k) => Math.abs(x - r.stop[from + k]!)).filter(Number.isFinite).sort((a, b) => a - b); return d.length ? d[Math.floor(d.length / 2)]! : NaN; }