> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xarchive.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Residual momentum

> Rank a point-in-time universe of Hyperliquid perps by the move BTC does not explain, hold the top fifth in one account, and read what each market made after costs.

![Residual momentum on a BTC chart](https://mintcdn.com/0xarchive-e895b8e7/aieykuawGqiJD2iw/images/terminal/examples/residual-momentum.webp?fit=max\&auto=format\&n=aieykuawGqiJD2iw\&q=85\&s=12dc5b918fc00fecee7640561baa24e5)

BTC on 1-day bars in the 0xArchive Terminal, 2026-10-28.

> **[Open on a chart](https://terminal.0xarchive.io/?symbol=BTC\&interval=1d\&scripts=example%3Aresidual-momentum)**
>
> Load it on BTC 1d in the terminal, then change its settings or open it in the script editor.

A portfolio across Hyperliquid's perps, in one account. Every Sunday's close it ranks the universe by each coin's residual momentum, the last 15 days of returns that its 60-day beta to BTC does not explain, over its 30-day volatility, and holds the top fifth from Monday's open. The arrows on the chart are its own market's fills; the equity curve is the whole account's.

## How it works

`universe.topN(25)` picks the 25 perps with the most dollar volume over the last 30 days, among those with 60 days of prices, again every 30 days. The pick reads only days before it, from every perp that traded then, delisted ones included, so the run never holds a coin for what it later became.

On each Sunday's close, `pool.resid(market, 'BTC', 15, 60)` and `pool.volatility(market, 30)` give each member's score, and `portfolio.longShort(scores, { quantile: 0.2, sides: 'long' })` turns the top fifth into equal weights that add up to the account. `strategy.targets()` sets the whole book: a coin that leaves the top fifth is sold at the next open. Set **Sides** to long the top and short the bottom instead.

The Strategy report's **Markets** tab shows what each coin made after fees, slippage and funding, and **Book** each rebalance's weights beside the universe's picks. A portfolio pays a flat 2 bps of slippage a fill: the archive's order book readings start on 6 Sep 2026.

## The script

```ts residual-momentum.ts theme={"theme":"github-dark"}
// Residual momentum across Hyperliquid's perps: every Sunday's close, rank the universe by the
// move BTC does not explain, over its volatility, and hold the top fifth from Monday's open. The
// universe is picked point in time (the 25 with the most dollar volume, re-picked every 30 days,
// delisted perps included while they traded), so the run never trades a coin for what it later
// became. Each pick and each read sees only days that had closed by then.
strategy('Residual momentum', { shortName: 'Resid mom', precision: 0, capital: 100_000, from: '2025-06-01', portfolio: true });

const size = input.int('Universe', 25, { min: 5, max: 50, group: 'Universe' });
const fit = input.int('Beta window (days)', 60, { min: 20, max: 120, group: 'Signal' });
const days = input.int('Residual (days)', 15, { min: 5, max: 60, group: 'Signal' });
const volDays = input.int('Volatility (days)', 30, { min: 10, max: 90, group: 'Signal' });
const quantile = input.float('Quantile', 0.2, { min: 0.05, max: 0.5, step: 0.05, group: 'Book' });
const sides = input.choice('Sides', 'long', ['long', 'both'], { labels: { long: 'Long the top only', both: 'Long top, short bottom' }, group: 'Book' });

const pool = universe.topN(size, { window: 30, minHistory: 60, every: 30 });
const chart = bars();

chart.onClose(() => {
  // Sunday's close: the orders fill at Monday's open.
  if (clock.parts(chart.time).dayOfWeek !== 0) return;
  const scores: Record<string, number> = {};
  for (const market of pool.current) {
    if (market === 'BTC') continue;
    const score = pool.resid(market, 'BTC', days, fit) / pool.volatility(market, volDays);
    if (!isNa(score)) scores[market] = score;
  }
  if (Object.keys(scores).length === 0) return;
  strategy.targets(portfolio.longShort(scores, { quantile, sides }));
});
```

## Next step

See the next example, [RSI cross, pasted Pine](https://docs.0xarchive.io/terminal/examples/rsi-pine.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.