UpCloseRatio: Up-Close Ratio
תוכן זה אינו זמין עדיין בשפה שלך.
The percentage of the last N bars that closed above the bar before. Blunt, robust and immune to outliers: a single huge down bar counts exactly as much as a small one, which is the point.
Signature
UpCloseRatio(period, on=<source>)| Argument | Type | Required | Notes |
|---|---|---|---|
period |
int Param or literal | Yes | Window. Default 20, bounds 1–400. |
on= |
symbol, OHLCV field, or indicator | No | Source. Defaults to the trade symbol’s close. |
Formula
$$ \text{UpCloseRatio} = 100,\frac{#{C_i > C_{i-1}}}{n} $$
Warmup
period + 1 bars; partial windows count only the bars seen.
Reading the value
ucr.valueucr > 60Example
class UpBarFilter(Strategy): """Only hold when most recent bars have been up bars.""" trade = Symbol() sizing = FixedCapital()
def execute(self): ucr = UpCloseRatio(20, on=self.trade) if ucr.value > 60 and self.trade.is_flat: self.buy(self.trade, reason=f"{ucr.value:.0f} pct up bars") if ucr.value < 40 and self.trade.is_long: self.sell(self.trade, self.trade.position, reason="Up bars now a minority")Notes
- Unchanged closes count as down bars (strict
>test), stated because it matters on low-tick instruments. - Proven by an assert-via-trade run on SPY daily (matched a strategy-side count exactly on every bar; zero alarm trades).
- A 50% reading is the coin-flip baseline; sustained departures are what a regime filter reads.