Skip to main content

coinpopbit-futures-math

Deterministic, zero-dependency math formulas for cryptocurrency perpetual futures planning — available in both JavaScript/TypeScript (ESM) and Python 3.9+.

  • Pure math only: No network calls, no market or funding API data, no exchange fee presets, no regional leverage caps, and no affiliate links. You pass every rate and margin parameter explicitly.
  • Cross-language parity: Both implementations share the same test vectors (tests/vectors.json) and match the interactive calculators on CoinPopBit.
  • Estimates only: Every result is a mathematical estimate for planning and education. Actual liquidation prices, fees, and funding depend on each exchange's live mark price, maintenance margin tiers, contract rules, and account mode — always check the exchange screen before placing an order.

Functions & Interactive Calculators

Function (JS / Python) What it estimates Interactive calculator
isolatedLiquidationPrice / isolated_liquidation_price Isolated-margin liquidation price (style: 'fee' | 'mmr' | 'add'), bankruptcy price, distance %, margin, and max isolated loss Liquidation Price Calculator
breakevenPrice / breakeven_price Fee- and funding-inclusive breakeven exit price (long or short) Futures Breakeven Calculator
roundTripFee / round_trip_fee Entry + exit trading fee using entry notional and exit price Crypto Futures Calculator
positionSizeByRisk / position_size_by_risk Risk-based position size, coin quantity, margin, and liquidation-before-stop check Crypto Position Size Calculator
averageEntryPrice / average_entry_price Weighted average entry price across buys plus quantity needed to reach a target average Crypto Average Price Calculator
riskRewardRatio / risk_reward_ratio Raw and fee-adjusted risk-to-reward ratio and breakeven win rate Risk-Reward Ratio Calculator

Installation

JavaScript / TypeScript (Node 18+ / Browser ESM)

npm install coinpopbit-futures-math
import {
  isolatedLiquidationPrice,
  breakevenPrice,
  roundTripFee,
  positionSizeByRisk,
  averageEntryPrice,
  riskRewardRatio,
} from 'coinpopbit-futures-math';

const liq = isolatedLiquidationPrice({
  side: 'long',
  entryPrice: 100000,
  leverage: 10,
  notional: 10000,
  mmr: 0.005,
  feeRate: 0.0005,
  style: 'fee',
});
console.log(liq.liquidationPrice); // ~90497.7376 (estimate)

Python (3.9+, standard library only)

pip install coinpopbit-futures-math
from coinpopbit_futures_math import (
    isolated_liquidation_price,
    breakeven_price,
    round_trip_fee,
    position_size_by_risk,
    average_entry_price,
    risk_reward_ratio,
)

liq = isolated_liquidation_price(
    side="long",
    entry_price=100000,
    leverage=10,
    notional=10000,
    mmr=0.005,
    fee_rate=0.0005,
    style="fee",
)
print(liq["liquidation_price"])  # ~90497.7376 (estimate)

Formulas & Reference Documentation

Let $q = N / E$ be position quantity, $E$ entry price, $N$ entry notional, $L$ leverage ($\ge 1$), $X$ extra isolated margin ($\ge 0$), $\text{mmr}$ maintenance margin rate ($\ge 0$), and $f$ fee rate ($0 \le f < 1$).

1. Isolated Liquidation Price (style: 'fee' | 'mmr' | 'add')

Exchanges differ in how liquidation fees and extra margin enter the isolated-margin equation. Pass style explicitly to select the formula family:

  • fee (maintenance margin ratio includes closing fee in denominator; see OKX liquidation calculation help):
    • Long: $P = (E \cdot q - E \cdot q / L - X) / (q \cdot (1 - \text{mmr} - f))$
    • Short: $P = (E \cdot q + E \cdot q / L + X) / (q \cdot (1 + \text{mmr} + f))$
    • Requires $\text{mmr} + f < 1$.
  • mmr (extra margin adjusted by $(1 \mp f)$, denominator uses $\text{mmr}$ only; see Bybit UTA liquidation process):
    • Long: $P = (E \cdot q - E \cdot q / L - X / (1 - f)) / (q \cdot (1 - \text{mmr}))$
    • Short: $P = (E \cdot q + E \cdot q / L + X / (1 + f)) / (q \cdot (1 + \text{mmr}))$
    • Requires $\text{mmr} < 1$.
  • add (linearized offset with additive extra margin; see MEXC margin calculation guide):
    • Long: $P = E \cdot (1 + \text{mmr} - 1 / L) - X / q$
    • Short: $P = E \cdot (1 - \text{mmr} + 1 / L) + X / q$
    • Requires $\text{mmr} < 1$.

Validation & outputs:

  • Throws if any input is out of range, if the resulting price is non-finite, or if liquidation sits on the wrong side of entry (P >= E for long, P <= E for short).
  • Long liquidation prices $\le 0$ (such as 1x long) are clamped to 0.
  • Bankruptcy price: long $\max(0,; E(1 - 1/L) - X/q)$, short $E(1 + 1/L) + X/q$.
  • Max isolated loss equals full committed isolated margin: $N / L + X$ (not just the price move to liquidation).

2. Breakeven Price & Round-Trip Fee

  • Funding paid (positive = paid, negative = received; see OKX funding fee mechanism): $$\text{fundingPaid} = N \cdot r_{\text{funding}} \cdot n_{\text{settlements}} \cdot (\text{long} ? +1 : -1), \quad F = \text{fundingPaid} / N$$
  • Breakeven price (requires $0 \le f_{\text{open}}$, $0 \le f_{\text{close}} < 1$):
    • Long: $E \cdot (1 + f_{\text{open}} + F) / (1 - f_{\text{close}})$
    • Short: $E \cdot (1 - f_{\text{open}} - F) / (1 + f_{\text{close}})$
  • Round-trip fee at exitPrice: $N \cdot f_{\text{open}} + (N / E) \cdot P_{\text{exit}} \cdot f_{\text{close}}$.

3. Risk-Based Position Size

  • Risk amount: $R = \text{balance} \times (\text{riskPct} / 100)$
  • Loss per unit at stop $S$ (stop assumed filled at $S$ with closeFeeRate): $$\text{perUnit} = (\text{long} ? E - S : S - E) + E \cdot f_{\text{open}} + S \cdot f_{\text{close}}$$
  • Quantity $Q = R / \text{perUnit}$, notional $N = Q \cdot E$, margin $M = N / L$. Leverage sets margin and liquidation proximity without changing risk-based quantity.
  • Includes liqBeforeStop flag comparing estimated liquidation against $S$ using the chosen style.

4. Average Entry Price & Target Averaging

  • Weighted average over positive (price, quantity) entries: $A = \sum(p_i q_i) / \sum q_i$.
  • Optional target average $T$ at new price $P$ (valid whenever $P < T < A$ or $P > T > A$): $$Q_{\text{add}} = Q \cdot (A - T) / (T - P)$$

5. Risk-Reward Ratio & Breakeven Win Rate

  • Raw: $\text{risk} = |E - S|$, $\text{reward} = |T - E|$, $R = \text{reward} / \text{risk}$, $\text{winRate} = 1 / (1 + R)$.
  • Fee-adjusted (single per-side fee rate $f \ge 0$): $$R_f = \frac{\text{reward} - (E \cdot f + T \cdot f)}{\text{risk} + (E \cdot f + S \cdot f)}$$ If $R_f \le 0$, feeAdjustedRatio is null (None in Python) and feeAdjustedBreakevenWinRate is 1.0.

Running Tests

# JavaScript (Node 18+)
node packages/js/test/run.mjs

# Python (3.9+)
python -m unittest discover -s packages/python/tests -v

Disclaimer & License

MIT License — Copyright (c) 2026 CoinPopBit (contact@coinpopbit.com). All outputs are deterministic mathematical estimates for educational and planning use only, not financial or trading advice.

Metadata

Release files for coinpopbit-futures-math 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 coinpopbit-futures-math 1.0.0
File Size Uploaded Provenance Actions
coinpopbit_futures_math-1.0.0.tar.gz 12.8 kB With provenance Details for coinpopbit_futures_math-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 21.7 kB

Release files / coinpopbit_futures_math-1.0.0.tar.gz

Download URL coinpopbit_futures_math-1.0.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b636d6511118fca3072260b1cf4a48053e9d060b00fa74ce7e02ea3f40e55bd2
BLAKE2b-256 checksum
How to use checksums
e278509e245fd221fad5489cbcd059fad6967fbf88271ed55bbd7ac4b918a744
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 Nov 27, 2026.

Transparency log

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

Download URL coinpopbit_futures_math-1.0.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cf1f89ac3bdf289f2a9354e8c61a9876ded216c5bb0453392c13503f2f3199b
BLAKE2b-256 checksum
How to use checksums
ab823bbc5a69b3c3295a4a217a3ab7133f9eb76a9395b053e55461f1268317b7
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 Nov 27, 2026.

Transparency log

Security for coinpopbit-futures-math 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 coinpopbit_futures_math-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 Nov 27, 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