Skip to main content

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

Why Exchange Order Forms Can Differ Slightly

  1. Mark Price Trigger: Exchanges trigger liquidation against Mark Price, not Last Traded Price.
  2. Funding Payments: Periodic funding fees add to or deduct from isolated margin over time (see Negative Funding Rates Explained).
  3. Cross Margin: Cross mode shares available wallet balance and unrealized PnL across open positions.

License

MIT © HoldWins

Metadata

Release files for perp-futures-calc 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for perp-futures-calc 1.0.0
File Size Uploaded Provenance Actions
perp_futures_calc-1.0.0.tar.gz 6.7 kB With provenance Details for perp_futures_calc-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for perp-futures-calc 1.0.0
File Interpreter ABI Platform Actions
perp_futures_calc-1.0.0-py3-none-any.whl Python 3 none any Details for perp_futures_calc-1.0.0-py3-none-any.whl

Total release size: 14.7 kB

Release files / perp_futures_calc-1.0.0.tar.gz

Download URL perp_futures_calc-1.0.0.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
b27c32cdd1fa6a66508946d3d5a52dfe5f50bd112f35256dd2a4c20660b282c0
BLAKE2b-256 checksum
How to use checksums
80a879e505c936b23dcee2b3972aed2d9ff67a36f7b1805cdbc4e956129b9017
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Release files / perp_futures_calc-1.0.0-py3-none-any.whl

Download URL perp_futures_calc-1.0.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f97c1cd04e797efa50488f9a6884e096f7e52befd05b3ca8778423e3ae88eae
BLAKE2b-256 checksum
How to use checksums
b53f21cc30e7fedad5ef0c47736802700ca61a5fa12a7ae878ad197f2bb1cbf1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

Security for perp-futures-calc 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for perp_futures_calc-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page