perp-futures-calc

Zero-dependency JavaScript (ESM + CLI) library for USDⓈ-M perpetual futures PnL, ROE%, round-trip fees, break-even price, and isolated liquidation price.

Core Formulas

For an isolated USDⓈ-M perpetual position with entry price P0, quantity Q, initial margin M, maintenance margin rate m, cumulative maintenance amount cumMaint, and per-side fee rate f:

Long Liquidation  = (Q * P0 - M - cumMaint) / (Q * (1 - m))
Short Liquidation = (Q * P0 + M + cumMaint) / (Q * (1 + m))

Long Break-Even   = P0 * (1 + f) / (1 - f)
Short Break-Even  = P0 * (1 - f) / (1 + f)

At 10× leverage (P0 = 100,000, m = 0.4%), an isolated long liquidates at 90,361.45 USDT (−9.64% from entry rather than −10%) because maintenance margin must remain before the account reaches zero equity.

ESM & CLI Example

import { calculatePerpPosition } from 'perp-futures-calc';

const res = calculatePerpPosition({
  side: 'long',
  entryPrice: 100000,
  exitPrice: 105000,
  leverage: 10,
  margin: 1000,
  feeRate: 0.0005,
  maintMarginRate: 0.004,
});

Related Live Analytics & Documentation

MIT License · Maintained by HoldWins (English)