TR — True Range
תוכן זה אינו זמין עדיין בשפה שלך.
The one-bar volatility unit behind ATR: the bar’s range, widened to include any gap from the previous close. Read it directly when you want raw per-bar volatility rather than its average.
Signature
TR(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Buffer depth used to reach the previous close. 2 is the natural value; larger values only delay warm-up. Bounds 2–100. |
on= |
symbol | No | Source symbol. Defaults to the trade symbol. Reads high, low and close. |
Formula
$$ \text{TR}t = \max\big(H_t - L_t,\ |H_t - C{t-1}|,\ |L_t - C_{t-1}|\big) $$
The bar’s high-to-low range, or the distance from the previous close to today’s high or low if a gap made that larger.
Warmup
period bars. 0 until the buffer holds a previous close.
Reading the value
tr.valuetr.value_at(1)tr > 25Example
class RangeExpansion(Strategy): """Enter on a bar whose true range is well above its recent average.""" period = Param(2, min=2, max=100) avg = Param(14, min=2, max=200) trade = Symbol() sizing = FixedCapital()
def execute(self): tr = TR(self.period, on=self.trade) tr_avg = SMA(self.avg, on=tr) self.stop_loss(2.0)
if tr.value > 1.5 * tr_avg.value and self.trade.close > self.trade.open and self.trade.is_flat: self.buy(self.trade, reason=f"Range expansion {tr.value:.2f} vs {tr_avg.value:.2f}")Notes
- Wilder, New Concepts in Technical Trading Systems (1978). ATR is the Wilder-smoothed average
of this series;
ATRexists in the platform set already. - The first bar after warm-up uses the oldest close in the buffer as “previous”; with
period=2that is exactly the prior bar.