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


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})

Metadata

Release files for perp-futures-calc 1.1.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.1.0
File Size Uploaded Provenance Actions
perp_futures_calc-1.1.0.tar.gz 7.0 kB With provenance Details for perp_futures_calc-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 15.2 kB

Release files / perp_futures_calc-1.1.0.tar.gz

Download URL perp_futures_calc-1.1.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
bea5527bb4b21fce45813244f0c459009a5c49776e4b5188c0d0f059b6f76cb9
BLAKE2b-256 checksum
How to use checksums
0ede1dfd9be11f9d4fe2438e5cae676496e896e965cf963a02e5d5d0da50a24c
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 5, 2026.

Transparency log

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

Download URL perp_futures_calc-1.1.0-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35f11e42b55c353b5911150a48e3d6a3b9ebf1824407987f85bc9ea21ba6f9bb
BLAKE2b-256 checksum
How to use checksums
a91c54eae5fb8a1153e3e10af6d3f812324c6684208a2fb73712269735bc18eb
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 5, 2026.

Transparency log

Security for perp-futures-calc 1.1.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.1.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 5, 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.1.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