InsideBar — Inside Bar
A bar whose entire range sits inside the previous bar’s: a contraction, and the setup half of most breakout rules. Returns 1 when present, 0 otherwise.
Signature
InsideBar(lookback, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
lookback |
int Param | Yes | Pinned at 1; the previous bar is kept in state. Bounds 1–1. |
on= |
symbol | No | Source symbol. Defaults to the trade symbol. Reads high and low. |
Formula
$$ \text{InsideBar} = 1 \text{ when } H_t \le H_{t-1} \text{ and } L_t \ge L_{t-1} $$
Warmup
One bar of state.
Reading the value
ib.valueib > 0A flag, not a level: 1 present, 0 absent.
Example
class InsideBarBreakout(Strategy): """Buy the break of an inside bar's parent range.""" trade = Symbol() sizing = FixedCapital()
def execute(self): ib = InsideBar(1, on=self.trade) dh = DonchianHighTrue(2, on=self.trade) if ib.value_at(1) > 0 and self.trade.close > dh.value_at(1) and self.trade.is_flat: self.buy(self.trade, reason="Inside bar break") if self.trade.close < dh.value * 0.98 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Break failed")Notes
- Uses
<=and>=, so a bar with an identical range to its predecessor counts as inside. - Proven by an assert-via-trade run on SPY daily (matched a strategy-side reference exactly on every bar; zero alarm trades).