You open a BTC long at 5x, an ETH short at 10x and a SOL long at 20x. What is your leverage? Each exchange screen shows the leverage of one position. None of them answers the question that matters for the whole account: how big is everything you hold, compared with the money behind it?
That number is the effective leverage: total notional divided by account equity. This post builds it in TypeScript with pure functions, together with margin used, free margin, the price move that wipes a given share of your margin, and the difference between isolated and cross margin. No network call, no library, and a small fixture of three positions so every number below is reproducible.
Disclosure: I am the founder of copy.top. We publish a free crypto leverage calculator page. The code below is a separate, minimal version written for this article. copy.top is independent and not affiliated with any exchange mentioned here.
Leverage amplifies losses as much as gains. Nothing here is financial advice, and you can lose everything you put in.
Setup
The files are TypeScript and run on Node 22.6 or newer with type stripping (tested here on Node 24.7):
node --experimental-strip-types run.ts
Imports between files need the .ts extension, and types use import type, which this mode requires. For plain JavaScript, rename to .mjs, delete the annotations and the import type lines.
What this post does not repeat
Two earlier posts cover the single-position basics: the liquidation price formula and position sizing from a risk budget. Here the unit is the account, not the trade.
The definitions
Four words, used the same way in all the code.
- Notional of a position: size times the current price. This is how much exposure you really carry.
- Nominal leverage of a position: the leverage setting you chose for it (5x, 10x, 20x). It decides how much margin the exchange locks for that position: margin = notional / nominal leverage.
- Equity: wallet balance plus the unrealized profit or loss of all open positions.
- Effective leverage: total notional of all positions divided by equity.
The calculator page linked above uses the same relation for one trade: position size = margin times leverage. The step to the account level is just a sum.
Types
// types.ts
export interface Position {
coin: string;
side: "long" | "short";
size: number; // in coin units, always positive
entry: number;
mark: number; // current price
leverage: number; // the nominal leverage chosen for this position
}
export interface Account {
balance: number; // wallet balance in USD, before unrealized PnL
positions: Position[];
}
Size is always positive and the side is a separate field, which keeps the sign logic in one place.
Step 1: the account maths
// leverage.ts
import type { Account, Position } from "./types.ts";
const dir = (p: Position) => (p.side === "long" ? 1 : -1);
export const notional = (p: Position) => p.size * p.mark;
export const unrealized = (p: Position) => dir(p) * p.size * (p.mark - p.entry);
export const marginUsed = (p: Position) => notional(p) / p.leverage;
export function accountStats(a: Account) {
const totalNotional = a.positions.reduce((s, p) => s + notional(p), 0);
const uPnl = a.positions.reduce((s, p) => s + unrealized(p), 0);
const equity = a.balance + uPnl;
const used = a.positions.reduce((s, p) => s + marginUsed(p), 0);
return {
totalNotional,
equity,
marginUsed: used,
freeMargin: equity - used,
effectiveLeverage: totalNotional / equity,
};
}
// Price move against you (in percent) that wipes `fraction` of the margin.
export const moveToWipe = (leverage: number, fraction: number) =>
(fraction / leverage) * 100;
// Isolated: only this position's own margin is at risk. Maintenance margin ignored.
export function liqIsolated(p: Position): number {
const margin = (p.size * p.entry) / p.leverage;
return p.entry - (dir(p) * margin) / p.size;
}
// Cross: the whole balance backs this position, and the other positions
// are frozen at their current mark. Maintenance margin ignored.
export function liqCross(a: Account, p: Position): number {
const others = a.positions
.filter((q) => q !== p)
.reduce((s, q) => s + unrealized(q), 0);
const backing = a.balance + others;
return p.entry - (dir(p) * backing) / p.size;
}
A few notes on what is in there.
unrealized multiplies by 1 for a long and -1 for a short, so a short gains when the mark falls under the entry.
accountStats returns equity, margin used, free margin and the effective leverage in one object. Free margin is equity minus the margin locked by the positions. It is what would be left to open something new, or to absorb losses before the account runs out.
moveToWipe is the rule of thumb in code form. At leverage L, a price move of 1 / L against you wipes the whole margin of that position. For a share X of the margin, the move is X / L. At 10x, a 1% move against you is 10% of the margin.
liqIsolated and liqCross are deliberately simple. Both ignore the maintenance margin, fees and funding that a real exchange adds, so treat the results as a model, not as what your exchange will show. Each venue publishes its own rules, and they differ.
Step 2: a fixture
Three positions on a 10,000 USD balance, with different sides and different nominal leverage.
// fixture.ts
import type { Account } from "./types.ts";
export const account: Account = {
balance: 10000,
positions: [
{ coin: "BTC", side: "long", size: 0.2, entry: 90000, mark: 91000, leverage: 5 },
{ coin: "ETH", side: "short", size: 4, entry: 3200, mark: 3100, leverage: 10 },
{ coin: "SOL", side: "long", size: 60, entry: 150, mark: 148, leverage: 20 },
],
};
Step 3: the runner
// run.ts
import { account } from "./fixture.ts";
import {
accountStats, notional, unrealized, marginUsed,
moveToWipe, liqIsolated, liqCross,
} from "./leverage.ts";
console.log("positions");
console.table(
account.positions.map((p) => ({
coin: p.coin,
side: p.side,
nominal: `${p.leverage}x`,
notional: notional(p).toFixed(2),
uPnl: unrealized(p).toFixed(2),
margin: marginUsed(p).toFixed(2),
})),
);
const s = accountStats(account);
console.log(`equity: ${s.equity.toFixed(2)} USD`);
console.log(`total notional: ${s.totalNotional.toFixed(2)} USD`);
console.log(`margin used: ${s.marginUsed.toFixed(2)} USD`);
console.log(`free margin: ${s.freeMargin.toFixed(2)} USD`);
console.log(`effective leverage: ${s.effectiveLeverage.toFixed(2)}x`);
console.log("\nprice move against you that wipes X of the margin");
const levs = [2, 5, 10, 20, 50];
const fracs = [0.1, 0.25, 0.5, 1];
console.table(
Object.fromEntries(
levs.map((l) => [
`${l}x`,
Object.fromEntries(
fracs.map((f) => [`${f * 100}%`, `${moveToWipe(l, f).toFixed(2)}%`]),
),
]),
),
);
// A model price at or below zero means no price of this coin alone liquidates it.
const fmtLiq = (x: number) => (x > 0 ? x.toFixed(2) : "none");
console.log("\nliquidation price, isolated vs cross");
console.table(
account.positions.map((p) => ({
coin: p.coin,
side: p.side,
entry: p.entry,
isolated: fmtLiq(liqIsolated(p)),
cross: fmtLiq(liqCross(account, p)),
})),
);
Output
This is the output, exactly as node prints it. First the positions and the account summary:
positions
┌─────────┬───────┬─────────┬─────────┬────────────┬───────────┬───────────┐
│ (index) │ coin │ side │ nominal │ notional │ uPnl │ margin │
├─────────┼───────┼─────────┼─────────┼────────────┼───────────┼───────────┤
│ 0 │ 'BTC' │ 'long' │ '5x' │ '18200.00' │ '200.00' │ '3640.00' │
│ 1 │ 'ETH' │ 'short' │ '10x' │ '12400.00' │ '400.00' │ '1240.00' │
│ 2 │ 'SOL' │ 'long' │ '20x' │ '8880.00' │ '-120.00' │ '444.00' │
└─────────┴───────┴─────────┴─────────┴────────────┴───────────┴───────────┘
equity: 10480.00 USD
total notional: 39480.00 USD
margin used: 5324.00 USD
free margin: 5156.00 USD
effective leverage: 3.77x
Then the price move that wipes a share of the margin, for several leverage levels:
price move against you that wipes X of the margin
┌─────────┬─────────┬──────────┬──────────┬──────────┐
│ (index) │ 10% │ 25% │ 50% │ 100% │
├─────────┼─────────┼──────────┼──────────┼──────────┤
│ 2x │ '5.00%' │ '12.50%' │ '25.00%' │ '50.00%' │
│ 5x │ '2.00%' │ '5.00%' │ '10.00%' │ '20.00%' │
│ 10x │ '1.00%' │ '2.50%' │ '5.00%' │ '10.00%' │
│ 20x │ '0.50%' │ '1.25%' │ '2.50%' │ '5.00%' │
│ 50x │ '0.20%' │ '0.50%' │ '1.00%' │ '2.00%' │
└─────────┴─────────┴──────────┴──────────┴──────────┘
And the liquidation price of each position, isolated versus cross:
liquidation price, isolated vs cross
┌─────────┬───────┬─────────┬───────┬────────────┬────────────┐
│ (index) │ coin │ side │ entry │ isolated │ cross │
├─────────┼───────┼─────────┼───────┼────────────┼────────────┤
│ 0 │ 'BTC' │ 'long' │ 90000 │ '72000.00' │ '38600.00' │
│ 1 │ 'ETH' │ 'short' │ 3200 │ '3520.00' │ '5720.00' │
│ 2 │ 'SOL' │ 'long' │ 150 │ '142.50' │ 'none' │
└─────────┴───────┴─────────┴───────┴────────────┴────────────┘
Check one example by hand
Take the BTC long. Size 0.2, mark 91,000, so the notional is 0.2 times 91,000 = 18,200. At 5x the locked margin is 18,200 / 5 = 3,640. Its unrealized profit is 0.2 times (91,000 minus 90,000) = 200. The ETH short has notional 4 times 3,100 = 12,400 and a profit of 4 times (3,200 minus 3,100) = 400. The SOL long has notional 60 times 148 = 8,880 and a loss of 60 times (148 minus 150) = 120.
Total notional is 18,200 + 12,400 + 8,880 = 39,480. Equity is 10,000 + 200 + 400 - 120 = 10,480. The effective leverage is 39,480 / 10,480 = 3.77, which matches the output.
Why the two leverage numbers differ
The three positions carry nominal leverage of 5x, 10x and 20x, and the account effective leverage is 3.77x. Both are true, and they answer different questions.
Nominal leverage tells you how much margin each position locks: 5,324 USD in total here, leaving 5,156 USD free. Effective leverage tells you how sensitive the whole account is. A 1% move against every position at once (BTC and SOL down, ETH up) costs about 1% of 39,480 = 395 USD, which is about 3.8% of equity. That is the account view, and the worst case. If every price simply drops 1%, the ETH short gains while the two longs lose, and the net change is only about 147 USD: total notional measures gross exposure, not net direction.
The exchange only checks that the locked margins fit in your equity. Nothing stops you from setting 20x on small positions and keeping a low effective leverage, or from setting 3x on three huge positions and ending far above it. If you size positions with a fixed share of equity, the effective leverage is the number to watch, and the per-position setting is mostly a way to choose how much cash gets locked.
The wipe-out table
The second table is the same idea for one position. Read the 10x row: a 1% move against you is 10% of the margin, 5% is half of it, and 10% is all of it. At 50x, 2% is all of it. Price swings of that size are routine in crypto, which is why high nominal leverage is fragile even when the effective leverage of the account looks calm.
The table is generated, not typed in, so you can change the lists levs and fracs in run.ts and print your own.
Isolated versus cross, with numbers
In isolated mode, a position can lose only the margin assigned to it. The BTC long at 5x has 18,000 / 5 = 3,600 USD of margin at entry (size 0.2 times entry 90,000, divided by 5). The loss reaches 3,600 when the price falls by 3,600 / 0.2 = 18,000 USD, so the modelled liquidation price is 72,000, a 20% fall from entry. That is the 1 / L rule again.
In cross mode, the whole balance backs every position. The BTC long can now absorb losses until the equity of the account is gone. The other two positions are showing 400 - 120 = 280 USD of profit, so the cushion is 10,000 + 280 = 10,280 USD, and the price has to fall by 10,280 / 0.2 = 51,400 USD. The modelled liquidation price is 38,600. Much further away, and the output agrees.
The same logic runs the other way for the ETH short: isolated at 3,520 (a rise of 10%, from the 10x), cross at 5,720.
Two caveats matter more than the numbers.
- Cross is not safer, it is different. The far liquidation price assumes the other positions stay where they are. In a fast market they move too, usually against the same account at the same moment. The cross price is a best case for the single position, not a floor for the account.
-
The SOL row says none. The raw cross result is 150 - 10,600 / 60 = -26.67, below zero: even at a price of zero, the loss on 60 SOL bought at 150 (9,000 USD) stays under the 10,600 USD cushion, so the model finds no price of SOL alone that empties the account.
fmtLiqprints "none" for that case. Real exchanges use maintenance margin and can liquidate the account for other reasons, so read it as "no price of this coin alone does it", nothing more.
What this does not tell you
- Maintenance margin is ignored. Real liquidation happens before equity hits zero. The calculator page linked above exposes a maintenance margin input; here I left it out to keep the code short.
- Fees and funding are not included. On perps, funding payments change equity over time. There is a separate post on funding.
- Marks move. Every figure is a snapshot at the mark prices in the fixture. Effective leverage rises when equity falls, even if you do nothing: lose 30% of equity on the same positions and your effective leverage goes up by about 43%, with no new order.
- Exchange rules differ. Margin modes, tiers, hedged positions and cross-margin offsets change from venue to venue. Check the documentation of yours before relying on any model.
- A model is not a safety net. These functions tell you how fragile a set of positions is. They do not tell you what will happen to the price.
To see the formulas in a form instead of code, the free tools page lists the single-trade calculators.
Leverage amplifies losses as much as gains, and past results do not predict future results. Nothing here is financial advice.
Top comments (0)