
Volatility indicator measuring the average range between high and low prices over a period. Unlike directional indicators, ATR measures market volatility magnitude without indicating direction.
Volatility indicator measuring the average range between high and low prices over a period. Unlike directional indicators, ATR measures market volatility magnitude without indicating direction.
ATR is essential for position sizing and risk management. Instead of fixed stop-loss distances, ATR-based stops adapt to current volatility: wider stops in volatile markets, tighter stops in calm markets. This prevents premature exits during normal volatility while protecting against genuine reversals.
Learn more about Risk Management Strategies →import backtrader as bt
class ATRPositionSizing(bt.Strategy):
params = (('atr_period', 14), ('risk_multiple', 2.0), ('risk_pct', 0.02))
def __init__(self):
self.atr = bt.indicators.ATR(self.data, period=self.p.atr_period)
def next(self):
if not self.position:
# Entry signal (simplified)
if self.data.close[0] > self.data.close[-1]:
# Position size based on ATR
stop_distance = self.p.risk_multiple * self.atr[0]
risk_per_share = stop_distance
position_size = (self.broker.getvalue() * self.p.risk_pct) / risk_per_share
self.buy(size=position_size)
self.stop_price = self.data.close[0] - stop_distance
else:
# ATR-based trailing stop
if self.data.close[0] < self.stop_price:
self.close()| Parameter | Default | Description |
|---|---|---|
| period | 14 | ATR lookback period |