perp-futures-calc
Zero-dependency Python (decimal.Decimal) & JavaScript (ESM) library + CLI for USDⓈ-M perpetual futures position math: gross/net PnL, ROE%, round-trip maker/taker fees, fee-adjusted break-even price, and isolated liquidation price (supporting both flat tier-1 maintenance margin rates and exchange maintenance brackets).
Installation (Python / PyPI)
pip install perp-futures-calc
from perp_futures_calc import calculate_perp_position
res = calculate_perp_position(
side="long",
entry_price="100000",
exit_price="105000",
leverage="10",
margin="1000",
fee_rate="0.0005", # 0.05% taker fee per side
maint_margin_rate="0.004" # 0.40% tier-1 maintenance margin
)
print(f"Liquidation Price: {res.liquidation_price:.2f} USDT (-{res.liquidation_distance_pct:.2f}%)")
print(f"Break-Even Price: {res.break_even_price:.2f} USDT")
print(f"Net PnL (ROE): {res.net_pnl:.2f} USDT ({res.net_roe_pct:.2f}%)")
CLI (perp-calc):
perp-calc --side long --entry 100000 --exit 105000 --leverage 10 --margin 1000 --fee 0.05 --mmr 0.4
Why 10× Leverage Does Not Liquidate at −10%
Traders often estimate a 10× isolated long liquidation price as entry × (1 − 1/10) = −10%. On real USDⓈ-M perpetual exchanges (such as Binance or Hyperliquid), forced liquidation triggers when remaining margin falls to the maintenance margin requirement Q × P_liq × m - cumMaint:
Long Liquidation = (Q × P0 − Margin − cumMaint) / (Q × (1 − m))
Short Liquidation = (Q × P0 + Margin + cumMaint) / (Q × (1 + m))
For a single tier-1 position (cumMaint = 0, Q × P0 = Margin × L), this simplifies to:
Long Liquidation = P0 × (1 − 1/L) / (1 − m)
Short Liquidation = P0 × (1 + 1/L) / (1 + m)
At P0 = 100,000 USDT, L = 10×, and tier-1 m = 0.4% (0.004):
P_liq = 100,000 × 0.9 / 0.996 = 90,361.45 USDT (−9.64% from entry, not −10%).
Fee-Adjusted Break-Even Price
Because trading fees f are charged on full notional value on both entry and exit:
Long Break-Even = P0 × (1 + f) / (1 − f)
Short Break-Even = P0 × (1 − f) / (1 + f)
Quick Start
Programmatic Usage (ESM / Node / Browser)
import { calculatePerpPosition } from './src/index.mjs';
const res = calculatePerpPosition({
side: 'long',
entryPrice: 100000,
exitPrice: 105000,
leverage: 10,
margin: 1000,
feeRate: 0.0005, // 0.05% taker fee per side
maintMarginRate: 0.004, // 0.40% tier-1 maintenance margin
});
console.log(res);
/*
{
side: 'long',
notional: 10000,
quantity: 0.1,
liquidationPrice: 90361.44578313253,
liquidationDistancePct: 9.63855421686747,
breakEvenPrice: 100100.0500250125,
grossPnl: 500,
roePct: 50,
fees: 10.25,
netPnl: 489.75,
netRoePct: 48.975
}
*/
Tiered Maintenance Margin Brackets
For larger positions that cross maintenance margin tiers, pass tiers:
const tiered = calculatePerpPosition({
side: 'long',
entryPrice: 100000,
exitPrice: 102000,
leverage: 20,
margin: 50000, // 1,000,000 USDT notional
tiers: [
{ floor: 0, cap: 50000, maintMarginRate: 0.004, maintAmount: 0 },
{ floor: 50000, cap: 250000, maintMarginRate: 0.005, maintAmount: 50 },
{ floor: 250000, cap: 1000000, maintMarginRate: 0.01, maintAmount: 1300 },
],
});
CLI Usage
node ./bin/cli.mjs --side long --entry 100000 --exit 105000 --leverage 10 --margin 1000 --fee 0.05 --mmr 0.4
- Mark Price Trigger: Exchanges trigger liquidation against Mark Price, not Last Traded Price.
- Funding Payments: Periodic funding fees add to or deduct from isolated margin over time (see Negative Funding Rates Explained).
- Cross Margin: Cross mode shares available wallet balance and unrealized PnL across open positions.
License
MIT © HoldWins
Go Module Installation (pkg.go.dev)
go get github.com/dalangdalang934/perp-futures-calc@latest
import "perpcalc" // github.com/dalangdalang934/perp-futures-calc
res, _ := perpcalc.Calculate(perpcalc.Input{Side: "long", EntryPrice: 100000, ExitPrice: 105000, Leverage: 10, Margin: 1000, FeeRate: 0.0005, MaintMarginRate: 0.004})