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

# Perpetual Margin Events

> Fills, margin adjustments and leverage updates on Hyperliquid isolated-margin perpetual positions, with the margin held in the position after each event.

The `hyperliquid.assets.perpetual_margin_events` table contains one row per event on an isolated-margin perpetual position, for native Hyperliquid perps and HIP-3 perps. An event is a fill, an isolated margin adjustment (margin added to or removed from the position) or a leverage update. Every fill on an isolated position is listed, including the closing fill. Margin adjustments and leverage updates are listed while the position is open, or while `position_size` is null. Cross-margin positions, and events while a position is in cross margin, are not included.

Each row gives the position after the event: its size, its leverage, and `margin_contributed`, the margin held in the position excluding unrealized PnL. For an isolated position this is the value Hyperliquid's `clearinghouseState` reports as `marginUsed` minus `unrealizedPnl`. Hyperliquid's API reports only the current value; this table gives its history.

Amounts are denominated in the market's collateral token: USDC on native perps, and the HIP-3 DEX's collateral token on HIP-3 markets (for example USDE, USDH, USDT0). Do not treat those columns as USD unless the collateral token is a dollar stablecoin at peg. [Perpetual Funding Events](https://docs.allium.so/historical-data/supported-blockchains/hyperliquid/assets/perpetual-funding-events.md) carries the `collateral_token` of each `coin`.

Coverage starts at `2025-07-10` UTC.

### Table Details

| Property | Value |
| - | - |
| Table Name | `hyperliquid.assets.perpetual_margin_events` |
| Table Status | Beta 🌱 |
| Unique Key | `unique_id` |

> [!NOTE]
>
> Always filter with `WHERE user = ...`. User-scoped queries are the supported access pattern.

### Margin Values

Rows of one `user` and `coin` are ordered by `timestamp`, then `event_sequence`. In that order, `margin_contributed` equals the previous row's `margin_contributed` plus `funding_since_previous_event`, `snapshot_adjustment_since_previous_event` and `margin_contributed_change`. The sum does not hold when one of these values is null, across events that are not listed (events while the position was in cross margin, and margin adjustments and leverage updates on a flat position), or where the previous row shows the position still open but the next fill opens it from flat, because the closing event is not listed.

`margin_quality` describes each row's `margin_contributed`:

| Value | Meaning |
| - | - |
| `exact` | No fill has opened, increased or flipped the position since its last position snapshot or full close. |
| `estimated` | A fill has opened, increased or flipped the position since its last position snapshot or full close. On such a fill, the margin Hyperliquid moves into the position depends on the mark price at that instant (see [Mark Price](#mark-price)). |
| `unknown` | The margin could not be determined. `margin_contributed`, `margin_before_fees_and_funding`, `margin_contributed_change` and `margin_used` are null. |

A position snapshot is Hyperliquid's own record of a position's state, which Allium stores at regular intervals in `hyperliquid.raw.perpetual_positions`. Where a snapshot is available, values are checked against it, and any difference appears on the position's next row in `snapshot_adjustment_since_previous_event`. Rows with `is_best_effort` set are older than the first position snapshot available for their HIP-3 DEX or for native perps (2025-09-22 for native perps, 2025-12-08 or later for HIP-3 DEXs), so their values could not be checked against one.

### Mark Price

`mark_price` is the mark price in effect after the latest on-chain price update before the event. Hyperliquid publishes mark prices on chain about every 3 seconds. The mark Hyperliquid applies at the exact instant of an event can differ from this value, usually by a fraction of a basis point to a few basis points.

* `position_value` and `margin_used` are valued at `mark_price`, so they approximate Hyperliquid's own values at the event.
* When a fill opens, increases or flips a position at a price worse than the mark price, Hyperliquid moves extra margin into the position to cover the immediate loss against the mark. Rows after such a fill are `estimated`.
* `mark_price` is null when no recent mark price is available.

### Known Limitations

* Hyperliquid transactions do not carry the vault or sub-account address. A margin adjustment or leverage update signed for a vault or sub-account is recorded under the signing wallet, and the vault's or sub-account's margin can be off until its next position snapshot.
* After a `topUpIsolatedOnlyMargin` action, the margin is unknown until the position's next snapshot.
* A margin addition sent in the same block as the fill that opens the position can be missing until the position's next snapshot.
* A change of margin mode that is not an `updateLeverage` action, for example a HIP-3 deployer changing a market's margin mode, is reflected only once a later position snapshot shows it.
* A wallet whose isolated setting on a market predates the available history, and was never changed, can be missing until a position snapshot shows the position as isolated.

### Margin of Closed Positions

[Perpetual Closed Positions](https://docs.allium.so/historical-data/supported-blockchains/hyperliquid/assets/perpetual-closed-positions.md) has no margin columns. For an isolated position, match its rows in this table on `user`, `coin` and `timestamp` between `open_time` and `close_time`. Cross-margin positions have no rows here, so the query below keeps them with null margin columns. When one position closes and the next opens in the same timestamp, rows at that timestamp match both positions.

```sql theme={null}
SELECT
  p.coin,
  p.side,
  p.open_time,
  p.close_time,
  MAX(IFF(m.source_fill_trade_id = p.open_trade_id, m.margin_contributed, NULL)) AS margin_after_opening_fill,
  MAX(m.margin_contributed) AS peak_margin_contributed,
  COUNT_IF(m.margin_quality = 'unknown') AS rows_with_unknown_margin
FROM hyperliquid.assets.perpetual_closed_positions p
LEFT JOIN hyperliquid.assets.perpetual_margin_events m
  ON m.user = '0xb46904a0248c5d56fd8de8b29e3cff81376ead2a'
  AND m.user = p.user
  AND m.coin = p.coin
  AND m.timestamp BETWEEN p.open_time AND p.close_time
WHERE p.user = '0xb46904a0248c5d56fd8de8b29e3cff81376ead2a'
  AND p.close_time >= '2026-11-20'
  AND p.close_time < '2026-11-27'
GROUP BY 1, 2, 3, 4
ORDER BY p.close_time;
```

### Table Columns

| Column Name | Data Type | Description |
| - | - | - |
| timestamp | TIMESTAMP\_NTZ(9) | UTC timestamp of the event. |
| event\_sequence | NUMBER(38,0) | Order of the event among the events of the same user and coin that share a `timestamp`: leverage updates, then margin adjustments, then fills in the order they changed the position. Sort by `timestamp`, then `event_sequence`. |
| user | VARCHAR(16777216) | The user address. |
| coin | VARCHAR(16777216) | Perpetual market coin, including HIP-3 names such as `xyz:NVDA`. |
| perp\_dex | VARCHAR(16777216) | HIP-3 DEX name. Null for native Hyperliquid perps. |
| event\_type | VARCHAR(16777216) | Kind of event: `position_fill`, `isolated_margin_adjustment` or `leverage_update`. |
| source\_fill\_hash | VARCHAR(16777216) | Transaction hash of the fill. Null unless `event_type` is `position_fill`. |
| source\_fill\_order\_id | VARCHAR(16777216) | Order id of the fill. Null unless `event_type` is `position_fill`. |
| source\_fill\_trade\_id | VARCHAR(16777216) | Trade id of the fill. Null unless `event_type` is `position_fill`. |
| source\_transaction\_hash | VARCHAR(16777216) | Hash of the transaction that carried the margin adjustment or leverage update. Null for fills. |
| source\_action\_type | VARCHAR(16777216) | Exchange action of that transaction: `updateIsolatedMargin`, `topUpIsolatedOnlyMargin` or `updateLeverage`. Null for fills. |
| fill\_direction | VARCHAR(16777216) | Direction label of the fill as reported by Hyperliquid, for example `Open Long`, `Close Short`, `Long > Short` or `Liquidated Isolated Long`. Null unless `event_type` is `position_fill`. |
| fill\_size | NUMBER(38,18) | Unsigned size of the fill, in the market's base asset. Null unless `event_type` is `position_fill`. |
| fill\_price | NUMBER(38,18) | Price of the fill, in collateral per coin. Null unless `event_type` is `position_fill`. |
| fill\_fee | NUMBER(38,18) | Fee charged on the fill, in the market's collateral token, including any builder fee. Negative for a rebate. Null unless `event_type` is `position_fill`. |
| position\_size | NUMBER(38,18) | Signed size of the position after the event, in the market's base asset. Negative for a short, 0 once the position is closed. Null when the size could not be determined. |
| leverage\_type | VARCHAR(16777216) | Leverage type of the position. Always `isolated`. |
| leverage\_value | NUMBER(38,0) | Leverage multiplier of the position after the event. |
| margin\_contributed | NUMBER(38,18) | Margin held in the isolated position after the event, excluding unrealized PnL, in the market's collateral token. Corresponds to `marginUsed` minus `unrealizedPnl` in Hyperliquid's `clearinghouseState`. Null when `margin_quality` is `unknown`. |
| margin\_before\_fees\_and\_funding | NUMBER(38,18) | Margin the user put into the part of the position that is still open, before trading fees and funding, in the market's collateral token. Null when the position opened before coverage starts, when its size could not be confirmed, and when `margin_contributed` is null. |
| margin\_contributed\_change | NUMBER(38,18) | Change in `margin_contributed` caused by the event itself. Null when the margin before or after the event is unknown, and when the position was not in isolated margin before the event. |
| funding\_since\_previous\_event | NUMBER(38,18) | Sum of the funding settlements applied to the position since the previous row of the same user and coin, in the market's collateral token. Negative when the position paid funding. |
| snapshot\_adjustment\_since\_previous\_event | NUMBER(38,18) | Sum of the corrections to `margin_contributed` made since the previous row of the same user and coin, when a position snapshot reported a different margin. |
| mark\_price | NUMBER(38,18) | Mark price after the latest on-chain price update before the event, in collateral per coin. Null when no recent mark price is available. See [Mark Price](#mark-price). |
| position\_value | NUMBER(38,18) | Value of the position at `mark_price`, in the market's collateral token. Null when `position_size` or `mark_price` is null. |
| margin\_used | NUMBER(38,18) | `margin_contributed` plus the position's unrealized PnL at `mark_price`. Corresponds to `marginUsed` in Hyperliquid's `clearinghouseState`. Null when `margin_contributed` is null. |
| margin\_quality | VARCHAR(16777216) | `exact`, `estimated` or `unknown`. See [Margin Values](#margin-values). |
| is\_best\_effort | BOOLEAN | True when the row is older than the first position snapshot available for its exchange, so its value could not be checked against one. |
| unique\_id | VARCHAR(16777216) | Unique identifier of the event. |
| \_created\_at | TIMESTAMP\_NTZ(9) | Row creation timestamp. |
| \_updated\_at | TIMESTAMP\_NTZ(9) | Row last update timestamp. |

### Sample Query

```sql theme={null}
SELECT
  timestamp,
  event_sequence,
  coin,
  event_type,
  fill_direction,
  fill_size,
  fill_price,
  position_size,
  leverage_value,
  margin_contributed,
  margin_contributed_change,
  funding_since_previous_event,
  mark_price,
  margin_used,
  margin_quality
FROM hyperliquid.assets.perpetual_margin_events
WHERE user = '0xb46904a0248c5d56fd8de8b29e3cff81376ead2a'
  AND timestamp >= '2026-11-20'
  AND timestamp < '2026-11-27'
ORDER BY coin, timestamp, event_sequence
LIMIT 100;
```
