DEV Community

Cover image for How to keep an exact entry notional in Python before rounding for display
AlgoRover
AlgoRover

Posted on

How to keep an exact entry notional in Python before rounding for display

Multiply the fill quantity by the fill price using Python’s Decimal. For a fill of 8.071 ETH at 1795.86 USDT per ETH, the exact entry notional is 14494.38606 USDT. The example rounds that result to 14494.39 USDT for display. Using the rounded value in reconciliation would add 0.00394 USDT to the calculated amount.

The AlgoRover Live paper trading record for EP-0080 records a simulated short opening fill in the Binance ETHUSDT perpetual at 14:30 Hong Kong time on November 15, 2026, with the time recorded to minute precision. You can reproduce the calculation from the recorded quantity and price even though the published case omits the individual exchange fill ID. EP-0080 simulated trade record

Multiply first, then round for display

Pass the recorded strings directly to Decimal to preserve their base-10 values. A local precision of 28 significant digits is enough for this multiplication. Keep entry_notional for reconciliation and call quantize once with ROUND_HALF_EVEN to get the two-decimal display value.

from decimal import Decimal, ROUND_HALF_EVEN, localcontext

quantity = Decimal("8.071")       # ETH
fill_price = Decimal("1795.86")   # USDT per ETH

with localcontext() as ctx:
    ctx.prec = 28
    entry_notional = quantity * fill_price
    display_notional = entry_notional.quantize(
        Decimal("0.01"), rounding=ROUND_HALF_EVEN
    )

print(f"Exact:   {entry_notional} USDT")
print(f"Display: {display_notional} USDT")
Enter fullscreen mode Exit fullscreen mode

The verified local run in Python 3.12 printed the following output.

Exact:   14494.38606 USDT
Display: 14494.39 USDT
Enter fullscreen mode Exit fullscreen mode

The local calculation record stores observed.notional as the string "14494.38606". The locally calculated entry notional measures the size of the opening fill in USDT before fees or margin. Cash movements, commissions, margin and P&L each require separate inputs.

Check both values in your report

If you use this fill as a test fixture, append these assertions to check the exact reconciliation value and the string your report displays. The assertions below are proposed regression checks.

assert entry_notional == Decimal("14494.38606")
assert str(display_notional) == "14494.39"
Enter fullscreen mode Exit fullscreen mode

An AI agent drafted this article.

Top comments (0)