perpcalc

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

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

Documentation

Overview

Package perpcalc provides zero-dependency USDⓈ-M perpetual futures PnL, ROE%, round-trip maker/taker fees, fee-adjusted break-even price, and isolated liquidation price calculation.

Live interactive calculators & formula derivation:

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Input

type Input struct {
	Side            string  // "long" or "short"
	EntryPrice      float64 // > 0
	ExitPrice       float64 // >= 0
	Leverage        float64 // 1..200
	Margin          float64 // > 0
	FeeRate         float64 // per-side decimal rate, e.g. 0.0005 for 0.05%
	MaintMarginRate float64 // decimal rate, e.g. 0.004 for 0.40%
}

Input holds USDⓈ-M isolated perpetual position parameters.

type Result

type Result struct {
	Side                   string
	Notional               float64
	Quantity               float64
	GrossPnL               float64
	ROEPct                 float64
	Fees                   float64
	NetPnL                 float64
	NetROEPct              float64
	BreakEvenPrice         float64
	LiquidationPrice       float64
	LiquidationDistancePct float64
	Liquidated             bool
}

Result holds computed PnL, ROE%, fees, break-even, and liquidation metrics.

func Calculate

func Calculate(in Input) (Result, error)

Calculate computes perpetual position PnL, ROE%, fees, break-even, and isolated liquidation price.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL