NBarNarrowRange — N-Bar Narrow Range
The narrowest range of the last N bars — the NR4 and NR7 compression setups. Volatility contracts before it expands, and this is the simplest way to say “it has contracted”.
Signature
NBarNarrowRange(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Window. 4 for NR4, 7 for NR7. Default 7, bounds 2–100. |
on= |
symbol | No | Source symbol. Defaults to the trade symbol. Reads high and low. |
Formula
$$ \text{NR} = 1 \text{ when } (H_t - L_t) \le \min(H_i - L_i,\ i \in \text{window}) $$
Warmup
period bars; 0 until the window is full.
Reading the value
nr.valuenr > 0Example
class NR7Breakout(Strategy): """Buy the breakout that follows an NR7 compression bar.""" trade = Symbol() sizing = FixedCapital()
def execute(self): nr = NBarNarrowRange(7, on=self.trade) dh = DonchianHighTrue(2, on=self.trade) if nr.value_at(1) > 0 and self.trade.close > dh.value_at(1) and self.trade.is_flat: self.buy(self.trade, reason="NR7 breakout") if self.trade.close < dh.value * 0.98 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Break failed")Notes
- Toby Crabel’s narrow-range setups.
- Proven by an assert-via-trade run on SPY daily (matched a strategy-side rolling minimum exactly on every bar; zero alarm trades).