Skip to content

Volatility and channels

Volatility measures movement; a channel places levels around price or a recent range. ATR and channel levels use price units. Historical volatility instead returns an annualised percentage reading.

ATR

atr(14) returns Average True Range as a series of price distances, using high, low and previous close. The true range includes gaps relative to the previous close; it is not simply high minus low. The period defaults to 14 in atr(); atr(bars, 14) is explicit.

bars uses the strategy's current bar timeframe: on a one-minute strategy, both forms read one-minute OHLC history through the current accepted bar. atr(bars(50), 14) limits the input to the latest available 50 bars. If OHLC history is unavailable, the condition fails with a history error; it does not count as a false signal or substitute zero.

atr(14) > 2
atr(14) / close > 0.02

If ATR is 2 and price is 100, the second ratio is 0.02, or 2% of price. ATR alone does not say whether price is rising or falling. For a stop based on an ATR multiple, use the stops and targets guide; an indicator comparison does not itself place a stop.

Bollinger Bands

These functions return one price-level series each. Arguments are period, standard-deviation multiplier, with defaults 20, 2. The middle line is the SMA; the other lines are that average plus or minus the multiplier times rolling sample standard deviation.

Function Form Line
bb_upper bb_upper(20, 2) Upper band.
bb_mid bb_mid(20) Middle SMA; the optional second argument does not change this line.
bb_lower bb_lower(20, 2) Lower band.
close < bb_lower(20, 2)
close > bb_mid(20)
(bb_upper(20, 2) - bb_lower(20, 2)) / bb_mid(20) < 0.05

The final condition measures total band width relative to the middle price: 0.05 means a width under 5% of that price. For default settings with an explicit input use bb_upper(close), bb_mid(close) or bb_lower(close). For another price input, use bb_upper(high, 20, 2). Do not call bollinger_bands(...) > close or write .upper; use the named line.

Donchian channels

These return the highest high, lowest low and their midpoint over N bars including now. The default period is 20; no multiplier is used.

Function Form Line
donchian_upper donchian_upper(20) Highest high in the window.
donchian_mid donchian_mid(20) (upper + lower) / 2.
donchian_lower donchian_lower(20) Lowest low in the window.
close > donchian_mid(20)
close > donchian_upper(20)[1]
close < donchian_lower(20)[1]

For an earlier-range breakout use [1]: the upper line from one bar ago covers the N bars ending on that earlier bar. Without [1], close > donchian_upper(20) includes the current high and cannot be true for ordinary OHLC data. Previous-band comparisons need one more bar of history.

Empty component calls, such as donchian_upper(), use the default and select the bars automatically. Use the component names rather than the donchian_channel container.

Keltner channels

Keltner uses an EMA centre and ATR-based distance. Argument order matters: EMA period, ATR period, multiplier. Defaults are 20, 10, 2.

Function Form Line
keltner_upper keltner_upper(20, 10, 2) EMA + 2 × ATR.
keltner_mid keltner_mid(20, 10, 2) EMA centre. ATR period and multiplier do not change the centre.
keltner_lower keltner_lower(20, 10, 2) EMA − 2 × ATR.
close > keltner_upper(20, 10, 2)
close < keltner_lower(20, 10, 2)
close > keltner_mid(20)

keltner_upper(20, 2) sets a 2-bar ATR period; it does not set a multiplier of 2. To change only width to 3 with default periods, write keltner_upper(20, 10, 3). Empty calls use all defaults. Explicit complete-bar input is also accepted: keltner_upper(bars, 20, 10, 2).

Historical volatility

historical_volatility(20, 252) calculates rolling sample standard deviation of log returns, multiplied by the square root of the annualisation factor and by 100. It returns a series in annualised percentage points: a reading of 20 means 20%, not 0.20.

Arguments are lookback, observations per year. Defaults are 20, 252 with explicit input, as in historical_volatility(close). Use historical_volatility(close, 20, 252) to state everything.

historical_volatility(20, 252) < 30

The default factor assumes 252 observations per year. It is not automatically changed for intraday, weekly or continuously traded data. Choose a factor that matches the actual bars; otherwise the annualised number describes the wrong horizon. A 20-return window requires 21 prices, and log returns need positive prices. This indicator measures realised historical movement, not implied volatility or a portfolio allocation.

For a current rolling extreme without an indicator series, see highest and lowest.