# oslib — cheat sheet for z.ai (paste this page as context)

You write Colab backtest scripts for a BTCUSDT (and ETH) perp bot. You cannot browse or see our engine code. Import the library and call only its public API. Do not reimplement fees, stops, or verdicts.

## Header (required, first two lines of every script)

```
!wget -q -O oslib.py https://osom-boba.pages.dev/colab/oslib.py
from oslib import *
```

Then your idea. In Colab run the cell as-is (`wget` is a Colab shell line). No other network calls — all data goes through oslib.

## Data (Colab only; refused on the author's box)

- `load(asset, tf='1h')`  asset `'btc'|'eth'`, tf `'15m'|'1h'|'4h'|'1d'` (15m/1h hosted; 4h/1d resampled from 1h).
- `funding(asset, venue='binance')`  venue `'binance'` (8h, years) or `'hl'` (hourly, 2023-05+).
- `hl_candles(asset)`  Hyperliquid 1h (public cap, ~5000 bars from 2026-04).
- `etf_flows()`  daily US spot BTC ETF net flows, column `flow_usd` (2024+).
- `expiry_days(start='2013-01-01', end=None)`  last-Friday-of-month 08:00 UTC (calendar; no download).
- `fng()`  daily Fear & Greed (`time`, `fng`, `day`).

OHLCV columns: `time` (ms UTC), `open`, `high`, `low`, `close`, `volume`.

## Indicators

`sma(x,n)` `ema(x,n)` `atr(h,l,c,n=14)` `adx(h,l,c,n=14) -> adx, plusDI, minusDI` `bb_width(x,n=20,k=2) -> upper, mid, lower, width` `donchian(h,l,n=20) -> upper, lower, mid` `chop(h,l,c,n=14)` `er(close,n=10)` (Kaufman) `rsi(close,n=14)` `supertrend(h,l,c,n=10,f=3) -> +1/-1`.

## Engines and simulator

- `engines(asset)` — `asset` is `'btc'`/`'eth'` or a 1h DataFrame. Returns dict of the 10 live engines: K8, V7, B1G, S5, COCO, C1, C2, R0, MOM30, D34. Each trade: `entry_time, exit_time, side, entry, stop, exit, R, why` (`side` +1/−1; times in ms).
- `simulate(df, signals, stop, target=None, time_stop=None, be_at=None, partial=None, trail=None, entry='next_open'|'stop_order')`
  - `signals`: −1/0/+1 array, one per bar. One position at a time.
  - `stop`: price array, or a fraction of entry (e.g. `0.02`).
  - `target`: price array, or an R-multiple (e.g. `2.0`).
  - `time_stop`: bars from fill. `be_at=1.0` moves the stop to entry after 1R.
  - `partial=(1.0, 0.5)` takes half off at 1R. `trail=(1.0, 0.5)` gives back 0.5R from the peak after 1R.
  - `entry='next_open'` fills at the next bar's open; `'stop_order'` fills when price trades through the signal-bar high (long) or low (short).
  - Intrabar: stop is checked before target. Costs are 0.12%/side (fee 0.07% + slip 0.05%). A stop exit is never worse than about −1R minus costs unless the bar gaps through the stop.

## Verdicts

- `evaluate(name, trades, placebo_trades, asset)` — `asset` is `'btc'`/`'eth'` or the OHLCV frame used. Headline 2017+. Row has PF, ret%, maxDD%, MAR, avgR, blocks 2013-16 / 2017-20 / 2021-23 / 2024+ / 2026, corrK8, placebo.
- `verdict(btc_rows, eth_rows=None)` — KEEP if (1) PF ≥ 1.3 and n ≥ 30, (2) PF beats its placebo by ≥ 0.3, (3) early and late year-blocks that have trades are both positive, (4) ETH PF ≥ 1.1 when ETH rows exist, (5) corr with K8 < 0.3. WATCH if exactly one miss, else DROP.
- `robustness(fn, params)` — `fn(**params)` returns trades; each numeric param is rerun at ×0.8 and ×1.2.
- `overlay(engines, mask_or_multiplier)` — basket at live risk (K8 2%; COCO, B1G, S5 3%; others 1%). Callable `(engine, trade) -> bool or float`, or a dict of engine sizes.
- `paired_exit_test(trades_a, trades_b, n_boot=2000)` — pair on (entry_time, side), bootstrap CI on mean(R_a − R_b).
- `report(rows, eth_rows=None, title='RESULTS')` — compact tables, RANKED, PLAIN WORDS, CSVs.

`selftest()` runs every function on synthetic data (safe anywhere). Costs, 1% standalone risk, and the KEEP rules are already in the library — do not change them.

## House rules

1. Every idea has a named placebo (same entry/stop/target, one ingredient removed). Never test an idea against a random or unmatched control as the only check.
2. Run BTC and ETH when the data exists. Headline window is 2017+.
3. Prefer daily / 4H ideas. 15m/1h fade, sweep, wick, PDH/PDL, climax and false-break patterns have already failed — do not send those.
4. Compact output only: use `report(...)`. End with PLAIN WORDS, not a prose essay.
5. No network except `from oslib import *`. No new fee model, no peeking future bars, no changing KEEP thresholds.

## 25-line example

```
!wget -q -O oslib.py https://osom-boba.pages.dev/colab/oslib.py
from oslib import *
import numpy as np

def ema_break(df, n=20, sl=0.02, tgt=2.0):
    e = ema(df.close, n)
    sig = np.where(df.close > e, 1, np.where(df.close < e, -1, 0))
    sig[:n] = 0
    return simulate(df, sig, stop=sl, target=tgt, time_stop=10*24, entry="next_open")

def ema_placebo(df, n=20, sl=0.02, tgt=2.0):
    rng = np.random.default_rng(0)
    sig = rng.choice([-1, 1], size=len(df)); sig[:n] = 0
    return simulate(df, sig, stop=sl, target=tgt, time_stop=10*24, entry="next_open")

rows, erows = [], []
for asset, bucket in (("btc", rows), ("eth", erows)):
    d = load(asset, "1h")
    bucket.append(evaluate("EMA20-side", ema_break(d), ema_placebo(d), d))
report(rows, erows, title="EMA20-side vs random-side placebo")
```
