ATR — Average True Range
תוכן זה אינו זמין עדיין בשפה שלך.
Wilder’s Average True Range over (high, low, close) — the canonical volatility measure. Single value.
Signature
ATR(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Wilder smoothing period. Default 14, bounds 2–200. |
on= |
symbol | No | Source. Defaults to the trade symbol. (ATR needs HLC — pass the symbol, not a single field.) |
Formula
For each bar:
$$ \text{TR}_t = \max(\text{high}_t - \text{low}_t,\ |\text{high}t - \text{close}{t-1}|,\ |\text{low}t - \text{close}{t-1}|) $$
(the very first bar has no previous close, so its TR is simply high − low). The first full window seeds the average, then Wilder smoothing takes over:
$$ \text{ATR}_{\text{seed}} = \frac{1}{N} \sum \text{TR}, \qquad \text{ATR}t = \frac{\text{ATR}{t-1} \cdot (N-1) + \text{TR}_t}{N} $$
Warmup
period bars. Before the window fills, the value is the simple average of the TRs observed so far.
Reading the value
atr = ATR(14, on=self.trade)
atr.valueatr.value / self.trade.close * 100 # ATR as a % of price — comparable across instrumentsExample
class QuietMarketTrend(Strategy): """Enter trend trades only when volatility is subdued.""" period = Param(14, min=5, max=50) trade = Symbol() sizing = FixedCapital()
def execute(self): atr = ATR(self.period, on=self.trade) sma = SMA(20, on=self.trade) self.stop_loss(2.0)
atr_pct = atr.value / self.trade.close * 100
if atr_pct < 1.5 and self.trade.close > sma.value and self.trade.is_flat: self.buy(self.trade, reason=f"Quiet uptrend (ATR {atr_pct:.2f}%)")
if self.trade.close < sma.value and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Closed below SMA")Notes
- ATR is unbounded and price-scaled — normalize by price before comparing instruments or setting fixed thresholds.
- Exit rules take percentage thresholds, not prices — to approximate an ATR-scaled stop, convert:
self.stop_loss(atr.value / self.trade.close * 100 * 2)registers a 2-ATR stop as a percentage. - ATR measures range, not direction — a rising ATR says “moves are getting bigger,” nothing about which way.