> For the complete documentation index, see [llms.txt](https://hydromancer.xyz/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hydromancer.xyz/docs/charting/hydromancer-charts/liquidation-map.md). Agents can also search and read this documentation through its [MCP server](https://hydromancer.xyz/docs/~gitbook/mcp).

# Liquidation Map

{% hint style="info" %}
**Using a coding agent?** Copy the whole page or the brief in this [section](#for-coding-agents).
{% endhint %}

### Quick start

**1. Install.** The package is private. [Ask us](https://hydromancer.xyz/docs/contact-us.md) for a read token, then:

```ini
# .npmrc
@hydromancer:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
```

```sh
npm i @hydromancer/charts   # or pnpm, bun, yarn
```

**2. Get a key.** [Send us](https://hydromancer.xyz/docs/contact-us.md) the origins your chart runs on, for example `https://app.example.com`, and `http://localhost:*` for local dev. We send back a publishable key (`pk_…`). It's public by design: it goes in your frontend code.

**3. Attach it to your chart.**

```ts
import { createLiqMapClient } from '@hydromancer/charts/liq-map';
import { attachLiqMap } from '@hydromancer/charts/liq-map/tradingview';

const client = createLiqMapClient({ publishableKey: 'pk_your_key' });

widget.onChartReady(() => {
  attachLiqMap(widget, { client, container });
});
```

`widget` is your TradingView widget, and `container` the element you passed to `new TradingView.widget({ container, … })`: the same element, not a new one. Open a BTC chart: the map appears within a few seconds, drawn over the candles.

That's the whole integration. The sections below put the map behind the candles, add its settings, and cover the rest.

### Full integration

#### 1. The stylesheet

TradingView draws its chart inside an iframe with an opaque background. To show the map behind the candles, that background has to be transparent. TradingView lets you load your own stylesheet into its iframe (the widget option `custom_css_url`), and the package ships the one that does it.

The package installs a small command, `hydromancer-charts`, that writes the stylesheet to a file. Add it to your `package.json` so the file always matches the installed version:

```json
{
  "scripts": {
    "predev": "hydromancer-charts underlay-css public/tv-underlay.css",
    "prebuild": "hydromancer-charts underlay-css public/tv-underlay.css"
  }
}
```

Or run it once by hand: `npx hydromancer-charts underlay-css public/tv-underlay.css`.

* **Serve it from the same site as your TradingView library.** TradingView ignores a stylesheet from another origin: the pane stays opaque and the map stays hidden. If your library is under `public/`, put the file there too.
* **Already pass a `custom_css_url`?** Add your file at the end of the command. The output holds yours first, then ours, and replaces your `custom_css_url`:

  ```sh
  hydromancer-charts underlay-css public/tv-underlay.css src/your-tradingview.css
  ```
* **Never `import` it into your page.** It makes your page's background transparent, not only the chart's.

#### 2. The widget

`underlayWidgetOptions` sets four widget options: `theme`, `custom_css_url`, `loading_screen` and `overrides`. Put it after your options so it wins, and merge the two objects you may already have:

```ts
import { createLiqMapClient } from '@hydromancer/charts/liq-map';
import { liqMapOverlay } from '@hydromancer/charts/liq-map/tradingview';
import { createOverlays, overlaysWidgetOptions } from '@hydromancer/charts/overlays/tradingview';
import { prepareUnderlayContainer, underlayWidgetOptions } from '@hydromancer/charts/tradingview';

const container = document.getElementById('chart');
const yours = { container, library_path, datafeed, symbol: 'BTC', interval: '15' /* , … */ };

const prepared = prepareUnderlayContainer(container, { theme: 'dark' });
const underlay = underlayWidgetOptions('dark', { cssUrl: '/tv-underlay.css' });
const options = {
  ...yours,
  ...underlay,
  overrides: { ...yours.overrides, ...underlay.overrides },
  loading_screen: { ...yours.loading_screen, ...underlay.loading_screen },
};

const client = createLiqMapClient({ publishableKey: 'pk_your_key' });
const overlayOptions = {
  overlays: [liqMapOverlay({ client })],
  defaults: { liqMap: { enabled: true } },
  widgetOptions: options,
};

const widget = new TradingView.widget(overlaysWidgetOptions(options, overlayOptions));
widget.onChartReady(() => {
  const overlays = createOverlays(widget, overlayOptions);
  // on teardown: overlays.dispose(), client.dispose(), widget.remove(), then prepared.dispose()
});
```

* `'dark'` is your chart's theme: pass the same one to both calls, and `'light'` for a light chart.
* `prepareUnderlayContainer` runs before `new TradingView.widget`. Without it, a dark page can show a white pane.
* `document.getElementById('chart')` stands for your chart's element: in React or Vue, use your ref.
* The map hides TradingView's horizontal grid while it shows: a grid line through a bar reads as two bars. It puts the grid back when it hides.

#### 3. The Liquidations button

`createOverlays` adds a **Liquidations** button to TradingView's header. It shows whether the map is live, and opens its panel: an on/off switch, presets, the layout, and the rest of the settings.

* **It also adds a row to TradingView's legend**, with an eye to show and hide the map.
* **On or off at first load:** `defaults: { liqMap: { enabled: true } }`. After that, each trader's choice wins, and the panel remembers their settings in the browser.
* **The button somewhere else**, for example in your own toolbar: pass an element as `entry`, and the button renders there.

  ```ts
  createOverlays(widget, { ...overlayOptions, entry: document.getElementById('my-toolbar-slot') });
  ```
* **Your own button:** pass `entry: false`, and open the panel from your button.

  ```ts
  const overlays = createOverlays(widget, { ...overlayOptions, entry: false });
  myButton.onclick = () => overlays.open(myButton);
  ```

### Did it work?

1. **Network.** One `POST …/frontend/v1/session`, then `GET …/snapshot` and `GET …/map/BTC.json`, all 200.
2. **On the chart.** Bars on the right, behind the candles. Hover one for its card, click it for the wallets.
3. **The console is quiet.** If the package prints anything, the line names the fix:

| Console says                                      | Fix                                                                                                                                        |
| ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| `publishable key pk_… is not allowed on <origin>` | [Send us](https://hydromancer.xyz/docs/contact-us.md) that exact origin. The map recovers within 5 minutes of us adding it, with no reload |
| `unknown or disabled publishable key`             | Check the key for a typo, or [ask us](https://hydromancer.xyz/docs/contact-us.md) for a new one                                            |
| `the API refused a session minted for this page`  | The requests go through a proxy. Talk to our API directly, or see Logged-in users                                                          |
| `requests to … are blocked`                       | Your CSP's `connect-src` needs `https://api-frontend.hydromancer.xyz`, or an ad blocker is in the way                                      |
| `No liquidation map for market "…"`               | Map your symbols: see Your symbols                                                                                                         |
| No error, but the pane isn't transparent          | The stylesheet isn't loading: check the network tab for `tv-underlay.css`, and that it's on the same site as the library                   |

Still stuck? [Send us](https://hydromancer.xyz/docs/contact-us.md) the output of `diagnoseLiqMap(handle)`, a screen recording, and your browser. It never contains a key, a token or a wallet.

### Common issues

* **`attachLiqMap: pass container or widgetOptions.container`.** `container` is `null`: usually `document.getElementById('…')` with an id that isn't on the page. Pass the element you gave TradingView. In React or Vue that's your ref (`containerRef.current`), not an id. The error shows under `charting_library.js` because TradingView runs `onChartReady`, but it comes from your call.
* **TypeScript: `… is not assignable to parameter of type 'TvWidgetLike'`.** Your own widget type is missing methods. Type the widget with TradingView's `IChartingLibraryWidget` (from your `charting_library.d.ts`): `attachLiqMap` accepts it as is.
* **React: errors or two maps in development.** StrictMode mounts your component twice, and the first chart is removed before it's ready. Guard the callback and dispose in the cleanup:

  ```ts
  let disposed = false;
  widget.onChartReady(() => {
    if (disposed) return;
    handle = attachLiqMap(widget, { client, container });
  });
  return () => {
    disposed = true;
    handle?.dispose();
    client.dispose();
    widget.remove();
  };
  ```
* **A new client on every symbol change.** Create the client once, with the widget. The map follows the chart's symbol by itself.
* **Works on `localhost`, not on `127.0.0.1`.** They're different origins: `http://localhost:*` on your key covers `localhost` only.
* **The map draws over the candles, not behind them.** That's the quick start. For behind, follow the full integration: the stylesheet and `underlayWidgetOptions`.

### Advanced

#### Your symbols

The map reads the chart symbol as a Hyperliquid market (`BTC`, `ETH`, `xyz:GOLD`). If yours are different, map them:

```ts
liqMapOverlay({
  client,
  symbolToMarket: (symbol) => (symbol === 'BTCUSDT' ? { market: 'BTC', priceMultiplier: 1 } : null),
});
```

* `null` means no map for that symbol.
* `priceMultiplier` is your chart's price divided by Hyperliquid's: `0.001` when your chart shows one PEPE and Hyperliquid prices `kPEPE` per 1,000.

#### Show and hide it from code

Without the Liquidations button (the quick start's `attachLiqMap`), the map has no switch of its own. Keep the handle and wire your own:

```ts
let handle;
widget.onChartReady(() => {
  handle = attachLiqMap(widget, { client, container });
});

let shown = true;
myToggle.onclick = () => {
  shown = !shown;
  handle.setVisible(shown);
};
```

Hidden, the map stops drawing and TradingView's grid comes back.

#### Colours

Pass a theme when you attach, and change it live with `setTheme`. `mode` fills in every colour you leave out.

```ts
const handle = attachLiqMap(widget, {
  client,
  container,
  theme: { mode: 'dark', colors: { long: '#ef5350', short: '#26a69a' } },
});

handle.setTheme({ mode: 'dark', colors: { long: '#f472b6', short: '#22d3ee' } }); // live, no remount
```

* `long` colours long liquidations (below the price), `short` short liquidations. Don't map them to price direction.
* The hover card and the wallet popup use the same colours.
* `themeFromColors({ bg, text, accent, long, short })` builds a whole theme from your brand colours, and `checkTheme(theme)` lists contrast and colour-blindness problems. Both from `@hydromancer/charts`.
* With the Liquidations button, pass `theme` in the options you give `createOverlays`.

#### Next.js and SSR

Every import is safe on the server, and a client created there makes no request. Create the widget and attach the map in a client component, inside an effect or `onChartReady`, and dispose everything in the effect's cleanup.

### For coding agents

Copy this into your coding agent (Claude Code, Cursor, Codex…). It's in the [llms.txt](https://llmstxt.org) format.

```markdown
# Hydromancer Liquidation Map

> Adds the Hyperliquid liquidation map to an existing TradingView chart (Advanced Charts or Trading Platform, v27 or later) with the npm package `@hydromancer/charts`: bars behind the candles, a hover card, a wallet popup and a Liquidations button in TradingView's header.

Ask the user for what you can't know: the npm read token (in `NPM_TOKEN`), their publishable key (`pk_…`, public by design; never invent one), and their chart symbols if they aren't Hyperliquid market names (`BTC`, `ETH`, `xyz:GOLD`). Everything not covered here is in the installed package's README, `node_modules/@hydromancer/charts/README.md`: read it before guessing an API.

## Install

1. `.npmrc`:
   @hydromancer:registry=https://registry.npmjs.org/
   //registry.npmjs.org/:_authToken=${NPM_TOKEN}
2. Install `@hydromancer/charts` with the repo's package manager, pinned to the exact version.

## Integrate

1. Stylesheet. Add `hydromancer-charts underlay-css <dir>/tv-underlay.css` to the `predev` and `prebuild` scripts, where `<dir>` is the folder that serves the TradingView library (same origin). If the widget already has a `custom_css_url`, append that file to the command. Never write these CSS rules yourself and never import the file into the page.
2. Before `new TradingView.widget`: `prepareUnderlayContainer(container, { theme })` from `@hydromancer/charts/tradingview`. `theme` is the chart's `'dark'` or `'light'`.
3. Widget options. Keep the user's and put ours after them:
   const underlay = underlayWidgetOptions(theme, { cssUrl: '/tv-underlay.css' });
   const options = { ...yours, ...underlay, overrides: { ...yours.overrides, ...underlay.overrides }, loading_screen: { ...yours.loading_screen, ...underlay.loading_screen } };
4. One client per chart, not per symbol: `createLiqMapClient({ publishableKey })` from `@hydromancer/charts/liq-map`.
5. Overlays:
   const overlayOptions = { overlays: [liqMapOverlay({ client })], defaults: { liqMap: { enabled: true } }, widgetOptions: options };
   const widget = new TradingView.widget(overlaysWidgetOptions(options, overlayOptions));
   widget.onChartReady(() => { overlays = createOverlays(widget, overlayOptions); });
   `liqMapOverlay` is from `@hydromancer/charts/liq-map/tradingview`; `overlaysWidgetOptions` and `createOverlays` from `@hydromancer/charts/overlays/tradingview`. The Liquidations button goes in TradingView's header; `entry: <element>` puts it elsewhere, `entry: false` plus `overlays.open(button)` uses the user's own button.
6. Symbols that aren't Hyperliquid's: `liqMapOverlay({ client, symbolToMarket: (symbol) => ({ market: 'BTC', priceMultiplier: 1 }) })`, returning `null` for no map.
7. Teardown: `overlays.dispose()`, `client.dispose()`, `widget.remove()`, then the prepared container's `dispose()`.
8. CSP, if the app sets one: add `https://api-frontend.hydromancer.xyz` to `connect-src`.

## Rules

- `container` is the element passed to `new TradingView.widget`. In React or Vue, use the ref, not an id lookup.
- Type the widget with TradingView's `IChartingLibraryWidget`.
- React StrictMode: guard `onChartReady` with a `disposed` flag set in the cleanup.
- Server rendering (Next.js and others): create the widget, the client and the overlays in a client component, inside an effect.
- Requests go straight from the browser to Hydromancer's API: no proxy with a publishable key. Don't call the API yourself and don't reimplement the map.

## Verify

- Network: one `POST …/frontend/v1/session`, then `GET …/snapshot` and `GET …/map/<market>.json`, all 200.
- In `onChartReady`, `checkUnderlayOptions(options)` and `checkUnderlayLeak()` (from `@hydromancer/charts/tradingview`) return `[]`.
- The console has no `[hydromancer/…]` line. If one shows, it names its own fix. Show the user the result.

## Docs

- [Package README](node_modules/@hydromancer/charts/README.md): the reference: every entry and option, status and errors, framework recipes, troubleshooting
- [Package CHANGELOG](node_modules/@hydromancer/charts/CHANGELOG.md): what changed in each version

## Optional

- [Theming](node_modules/@hydromancer/charts/README.md#theming): `theme` and live `setTheme`, `themeFromColors`, `checkTheme`
```
