Formula methodology · 12 calculators · 22 test vectors

How TradingCalc formulas work

Every number TradingCalc returns is the result of a deterministic formula — derived from exchange documentation, regression-tested against exact-value vectors, and verifiable on demand. This page explains where each formula comes from and how correctness is enforced.

The core principle: same inputs, same outputs

TradingCalc is not an AI. It does not estimate, approximate, or sample. Given the same inputs, it always produces the same outputs — down to the last cent. This property is not incidental; it is enforced by design and verified continuously.

Deterministic
No randomness. No model weights. Pure arithmetic.
Traceable
Every formula links to exchange documentation or a derivation.
Verifiable
Anyone can call system.verify and get pass/fail per vector.

Three-layer verification system

Correctness is enforced at three independent levels. Breaking one layer surfaces in at least one other.

01
Canonical test vectors
src/lib/verify.ts

22 exact input/output pairs, each with step-by-step arithmetic in the source field. Written independently of the formula code — they define the expected answer, not the implementation.

02
Automated test suite
src/lib/calculators/calculators.test.ts

66 Vitest tests: 22 exact-value vector tests + 44 property tests (sign invariants, round-trips, monotonicity). Runs in CI on every push. A push that breaks any test does not deploy.

03
Live verification endpoint
/api/mcp → system.verify

Any caller — human or AI agent — can call system.verify via MCP to run all 22 vectors against the live production code. Returns pass/fail per vector with expected vs actual values.

Formula reference — all 12 calculators

Each row shows the core formula, its exchange documentation source, and how many canonical test vectors cover it.

PnL2 vectors
Net PnL = (exitPrice − entryPrice) × size − openFee − closeFee

Binance Futures documentation — "How to calculate PnL"

Liquidation Price3 vectors
Liq (long) = entryPrice × (1 − 1/leverage + maintenanceMarginRate)

Bybit Derivatives documentation — "Liquidation Price Calculation"

Breakeven Price2 vectors
Breakeven (long) = entryPrice × (1 + openFeePct + closeFeePct)

Derived from fee structure; verified against Binance and Bybit fee schedules

Position Size2 vectors
size = riskAmount / (|entryPrice − stopLoss| / entryPrice × entryPrice)

Standard position sizing formula; cross-verified with broker documentation

Average Entry2 vectors
avgEntry = Σ(price_i × qty_i) / Σ(qty_i)

Weighted average formula; standard across all exchanges

Target Exit2 vectors
exitPrice (long) = (targetPnl + size × entryPrice + openFee) / (size − closeFee)

Derived from PnL formula by solving for exitPrice

Funding Cost2 vectors
fundingCost = notional × fundingRate × nPeriods

Binance Futures — "Funding Rate Mechanism"

Compound Funding2 vectors
totalCost = Σ (notional_t × fundingRate) where notional decays with P&L

Derived; cross-verified with Bybit funding cost examples

Funding Arbitrage1 vector
annualYield = (rateA − rateB) × periodsPerYear × 100

Standard carry trade formula; exchange-agnostic

Hedge Ratio1 vector
shortSize = spotSize × (1 + fundingCost / spotNotional)

Delta-neutral hedge formula; Binance spot + futures hedging guide

Max Safe Leverage1 vector
maxLev = 1 / (maintenanceMarginRate + (volatility × safetyFactor))

Derived from maintenance margin requirements; cross-referenced with exchange margin tables

Scenario PnL1 vector
Applies PnL formula across N price targets; returns P&L table

Composed from PnL formula; no additional derivation

What a test vector looks like

Each vector in src/lib/verify.ts contains explicit step-by-step arithmetic so the expected answer can be verified by hand — independent of the implementation.

{
  calc: 'pnl',
  label: 'BTC long, 10× leverage, maker/taker fees',
  input: {
    side: 'long',
    entryPrice: 60000,
    exitPrice: 63000,
    contractSize: 0.1,
    leverage: 10,
    feeOpenPct: 0.0002,   // 0.02% maker
    feeClosePct: 0.0005,  // 0.05% taker
  },
  expected: {
    grossPnl: 300,
    openFee: 1.20,
    closeFee: 3.15,
    netPnl: 295.65,
    roe: 49.275,
  },
  source: `
    Gross = (63000 - 60000) × 0.1 = 300
    Open fee  = 60000 × 0.1 × 0.0002 = 1.20
    Close fee = 63000 × 0.1 × 0.0005 = 3.15
    Net = 300 - 1.20 - 3.15 = 295.65
    Margin = 60000 × 0.1 / 10 = 600
    ROE = 295.65 / 600 × 100 = 49.275%
  `,
}

Self-verification via MCP

AI agents and trading systems can call system.verify before relying on results. The response includes pass/fail per vector, expected vs actual values, and a timestamp.

// Request
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": { "name": "system.verify", "arguments": {} }
}

// Response
{
  "status": "pass",
  "passed": 22,
  "failed": 0,
  "total": 22,
  "timestamp": "2026-04-09T...",
  "results": [
    { "label": "BTC long, 10× leverage", "status": "pass", "calc": "pnl" },
    { "label": "Isolated long, 10× leverage", "status": "pass", "calc": "liquidation" },
    ...
  ]
}

Live proof (runs on every page load, no cache): tradingcalc.io/verify

What this means in practice

AI agents

Call system.verify before a trading session. If it passes, every formula call you make during that session returns auditable, exact outputs.

Trading bots

Embed the MCP endpoint. Your bot gets exact liquidation prices and position sizes — not LLM estimates that vary between calls.

Risk managers

The same formula runs in CI, in the API, and on the public /verify page. Three independent surfaces showing the same result.

Developers evaluating the API

Read the test vectors in verify.ts. If the arithmetic matches your manual calculation, you can trust the endpoint.

Related

Start using deterministic calculations

19 MCP tools. Free tier, no signup required. Add to Claude Desktop, Cursor, or call the REST endpoint directly.