# Volatility reference

> All 8 volatility in AlgoBarsX, each with its parameters, defaults, ranges and an example: atr, true_range, bollinger, keltner, donchian, envelope, hist_volatility, choppiness.

Source: https://algobarsx.com/docs/ref-fn-volatility/

### `atr(length = 14, on = bars())` → series<distance>

Average true range.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `length` | int | `14` | 1 to 5000 | Number of bars in the calculation. |
| `on` | BarSet | `bars()` |  | Bars to calculate on; defaults to the script's own bars. |

**Formula** Wilder's smoothing (rma) of the true range.

**Warm-up** length bars

```algobarsx
stop_distance = atr(14) * 1.5
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `true_range(on = bars())` → series<distance>

True range of the current bar.

#### Parameters

| Name | Type | Default | What it is |
| --- | --- | --- | --- |
| `on` | BarSet | `bars()` | Bars to calculate on; defaults to the script's own bars. |

```algobarsx
tr = true_range()
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `bollinger(source = close, length = 20, multiplier = 2.0)` → Bands

Bollinger Bands.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |
| `multiplier` | number | `2.0` | 0.1 to 10 | Standard deviations. |

#### Outputs, read with a dot

| Name | Type | What it is |
| --- | --- | --- |
| `upper` | series<price> | Upper band. |
| `middle` | series<price> | Middle band. |
| `lower` | series<price> | Lower band. |
| `width` | series<number> | Band width relative to the middle. |
| `percent_b` | series<number> | Position inside the bands. |

```algobarsx
bb = bollinger(close, 20, 2.0)
```

```algobarsx
squeeze = bollinger(close).width < 0.02
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `keltner(source = close, length = 20, multiplier = 2.0, atr_length = 10)` → Bands

Keltner channels.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |
| `multiplier` | number | `2.0` | 0.1 to 10 | ATR multiplier. |
| `atr_length` | int | `10` | 1 to 5000 | ATR length. |

#### Outputs, read with a dot

| Name | Type | What it is |
| --- | --- | --- |
| `upper` | series<price> | Upper channel. |
| `middle` | series<price> | Middle line. |
| `lower` | series<price> | Lower channel. |

```algobarsx
kc = keltner(close, 20, 2.0, 10)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `donchian(length = 20, on = bars())` → Bands

Donchian channels.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |
| `on` | BarSet | `bars()` |  | Bars to calculate on; defaults to the script's own bars. |

#### Outputs, read with a dot

| Name | Type | What it is |
| --- | --- | --- |
| `upper` | series<price> | Highest high. |
| `middle` | series<price> | Midpoint. |
| `lower` | series<price> | Lowest low. |

```algobarsx
dc = donchian(20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `envelope(source = close, length = 20, percent = 2%)` → Bands

Moving average envelope.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |
| `percent` | percent | `2%` |  | Distance from the average. |

#### Outputs, read with a dot

| Name | Type | What it is |
| --- | --- | --- |
| `upper` | series<price> | Upper line. |
| `middle` | series<price> | Average. |
| `lower` | series<price> | Lower line. |

```algobarsx
env = envelope(close, 20, percent: 2%)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `hist_volatility(source = close, length = 20, annualize = 252)` → series<number>

Historical volatility.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `source` | series<number> | `close` |  | Series to calculate from. |
| `length` | int | `20` | 1 to 5000 | Number of bars in the calculation. |
| `annualize` | int | `252` | 1 to 100000 | Periods per year. |

```algobarsx
hv = hist_volatility(close, 20)
```

Works in: strategy, indicator, alert, library. Since 1.0.

### `choppiness(length = 14, on = bars())` → series<number>

Choppiness index.

#### Parameters

| Name | Type | Default | Range | What it is |
| --- | --- | --- | --- | --- |
| `length` | int | `14` | 1 to 5000 | Number of bars in the calculation. |
| `on` | BarSet | `bars()` |  | Bars to calculate on; defaults to the script's own bars. |

```algobarsx
chop = choppiness(14)
```

Works in: strategy, indicator, alert, library. Since 1.0.
