> ## Documentation Index
> Fetch the complete documentation index at: https://docs.quantnomad.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How UT Bot v2 Works

> How UT Bot v2 calculates its ATR trailing stop, when the trend flips, and when buy and sell signals and strategy entries are generated.

UT Bot v2 is built on one idea: follow price with a trailing stop whose distance depends on volatility, and flip the trend when price crosses it. Everything on this page comes from the [open-source code](https://docs.quantnomad.com/free-indicators/ut-bot-v2/source-code.md).

## The calculation

**1. Measure volatility**

Calculate the Average True Range over **ATR Period** bars:

```pine theme={null}
atr = ta.atr(atr_len)
```

New to ATR? See [ATR and Signal Sensitivity](https://docs.quantnomad.com/guides/atr-sensitivity.md).

**2. Set the stop distance**

Multiply ATR by the **Multipier** input:

```pine theme={null}
sl_value = mult * atr
```

With the defaults (multiplier 1, ATR period 10), the stop sits one 10-bar ATR away from price.

**3. Update the trailing stop**

On every bar, compare the price **Source** (close, by default) with the previous bar's stop:

| Situation | New stop |
| - | - |
| Price was above the stop and is still above it (uptrend continues) | The higher of the previous stop and `source − sl_value`. The stop can only move up. |
| Price was below the stop and is still below it (downtrend continues) | The lower of the previous stop and `source + sl_value`. The stop can only move down. |
| Price just crossed above the stop | Reset below price: `source − sl_value` |
| Price just crossed below the stop | Reset above price: `source + sl_value` |

**4. Detect a trend flip**

A **Buy** signal is generated when the source crosses above the trailing stop. A **Sell** signal is generated when it crosses below. Both require the bar to be closed:

```pine theme={null}
buy  = ta.crossover(source, tsl_price) and barstate.isconfirmed
sell = ta.crossover(tsl_price, source) and barstate.isconfirmed
```

## Why it trails

In an uptrend, the stop follows price upward and never moves down. If price pulls back without crossing the stop, the trend holds. If the pullback is deeper than the stop distance, price crosses the stop and the trend flips to down. Downtrends work the same way in reverse.

The **Multipier** sets how deep a pullback has to be:

* **Higher** multiplier: wider stop, fewer trend flips, later signals.
* **Lower** multiplier: tighter stop, more trend flips, earlier signals, more whipsaws in choppy markets.

Because the distance is measured in ATR, the stop automatically widens in volatile periods and tightens in quiet ones.

## Signal timing

Signals are only generated on **confirmed (closed) bars** (`barstate.isconfirmed`). In practice:

* A Buy or Sell label never appears on the bar that's still forming. It's added when that bar closes.
* Once a label appears, it doesn't disappear later.
* The **trailing stop line** on the current bar does still move with live prices until the bar closes. That's expected.

This avoids the most common source of disappearing signals. See [Confirmed vs Unconfirmed Signals](https://docs.quantnomad.com/guides/confirmed-signals.md).

## Strategy behavior

UT Bot v2 is a TradingView strategy. On each signal it places a simulated market order:

| Signal | Strategy action |
| - | - |
| Buy | Enter **Long** (closing any short) |
| Sell | Enter **Short** (closing any long) |

The strategy is always in the market after its first signal, reversing on each flip. Orders are only placed within the backtest date range (see [Settings](https://docs.quantnomad.com/free-indicators/ut-bot-v2/settings.md#backtesting-range)). By TradingView's standard behavior, a market order generated on a bar's close is filled at the open of the next bar.

> [!WARNING]
>
> The script doesn't set commission or slippage, so default backtest results don't include trading costs. Set them under **Settings → Properties** before reading too much into the results. Backtests describe the past, not the future.

## Related pages

* [Settings & Parameters](https://docs.quantnomad.com/free-indicators/ut-bot-v2/settings.md)
* [Buy & Sell Signals](https://docs.quantnomad.com/free-indicators/ut-bot-v2/signals.md)
* [Understanding Indicator Signals](https://docs.quantnomad.com/guides/indicator-signals.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.