Keep Decimal Precision in Contract Research
Prediction-market contracts trade in pennies. A YES contract moves between 1 cent and 99 cents, and a binary settles at a round dollar or zero. That sounds like a domain where arithmetic is trivial. It is not. The moment you multiply a price by a contract count, add a fee, and round to cash, floating-point error and loose unit handling start quietly corrupting your research. This post is about keeping that arithmetic honest.
Why float is the wrong type for contract prices
Python's float is a binary IEEE 754 double. The problem is simple and well documented: most decimal fractions have no exact binary representation, so the machine stores the nearest value it can hold.
The canonical demonstration is that 0.1 plus 0.2 does not equal 0.3 under binary floating point. The Python decimal module documentation makes the contrast explicit: in decimal floating point, 0.1 + 0.1 + 0.1 - 0.3 is exactly zero, while in binary floating point the result is 5.5511151231257827e-017. A float is a 64-bit double giving roughly 15 to 17 decimal digits of precision, which is plenty for physics and silently wrong for money.
The standard library answer is the decimal module. Most decimal fractions have no exact binary form, so float rounds to the nearest value it can hold, while Decimal keeps the digits you wrote. Decimal stores numbers as base-10 digit sequences, which is especially useful where precision and accuracy are critical, such as financial and monetary systems.
The one rule that bites everyone: construct from strings
Decimal only helps if you feed it exact input. If you build a Decimal from a float, you inherit the float's error before you have done any arithmetic. The Python docs give a stark example: Decimal(float('1.1')) converts to Decimal('1.100000000000000088817841970012523233890533447265625').
So the discipline is: parse incoming price text straight into Decimal, never through float. If a venue field arrives as a float already, you have lost precision upstream and should treat it as such.
from decimal import Decimal
# WRONG: float imprecision is baked in before you start
bad = Decimal(0.1) + Decimal(0.2)
# RIGHT: exact decimal values from strings
good = Decimal('0.1') + Decimal('0.2')
assert good == Decimal('0.3') # holdsQuantity versus notional: two different numbers
A recurring source of bugs is conflating how many contracts you hold with how much cash that position represents. They are different quantities with different units:
- Quantity: an integer count of contracts. You hold 100 contracts, not 99.9999.
- Price: cash per contract, a Decimal in dollars (for example 0.37) or cents (37).
- Notional / cost basis: quantity times price, a dollar amount. 100 contracts at 0.37 is 37.00 USD of cost basis, not 100 of anything.
- Settlement value: a binary pays out per winning contract, so settlement is a function of quantity, not of your entry price.
Mixing these up produces position sizes that are off by a factor of the price. If an API wants USD notional and you hand it a contract count, you have overspent by roughly 1/price. This is exactly why interfaces are usually explicit about which unit they take. In the Banger paper-research SDK, for instance, Context.place takes a USD notional size, while Context.close works on held-contract quantity. Those are not interchangeable arguments, and reading the signature before you call it saves a class of silent sizing errors.
Keep settlement separate too. A marked value from the current bid, a settlement claim of a dollar per winning contract, cash actually credited after fees, and a hypothetical executable sale price are four different numbers. Do not let one stand in for another in a results table.
Rounding is a venue rule, not a math rule
Generic decimal rounding and a venue's fee rounding are different things, and you must not assume one matches the other. Kalshi's general trading fee is a formula, not a flat percentage. The published form is 0.07 x C x P x (1-P), rounded up, where C is the number of contracts and P is the price in dollars, charged when your order matches immediately as a taker. Because of the P times (1-P) term, the fee peaks near a 50 cent contract price and gets smaller toward 1 cent or 99 cents.
The rounding detail matters for reconciliation. One widely cited description rounds the fee up to the next whole cent, so tiny orders pay a slightly higher effective rate. But the venue's own current schedule is finer: it describes rounding so that fee plus position cost lands on a centicent ($0.0001), per the Kalshi fee schedule PDF effective 7 July 2026. If your research code rounds up to a whole cent and the venue rounds fee-plus-cost to a centicent, your paper numbers and a real fill will diverge by small amounts that compound over many trades. Neither is a bug. They are two different models, and you should know which one your result used.
Two more cautions. Maker orders are not free everywhere: resting orders pay nothing on most series, but some series charge a maker fee at a rate of 0.0175 in place of 0.07. And some Kalshi products carry their own separate fee schedule, so the general formula is a baseline, not a universal rule. Preserve the unknown rather than hard-coding the baseline everywhere.
A fully hypothetical worked example
No credentials, no funds, no orders. Chosen inputs, shown with units, purely to illustrate the arithmetic. Take 100 contracts at a price of 0.50 dollars per contract.
- Cost basis: 100 x 0.50 = 50.00 USD.
- Taker fee under the general formula: 0.07 x 100 x 0.50 x (1 - 0.50) = 1.75 USD before rounding.
- If YES settles, payout is 100 x 1.00 = 100.00 USD, from quantity alone.
- Net of fee on entry: 100.00 - 50.00 - 1.75 = 48.25 USD in this toy scenario.
The 1.75 USD figure matches the published Kalshi formula output for a 50-cent, 100-contract taker trade (0.07 x 100 x 0.50 x 0.50 = 1.75). Note what this is not: it is not a market observation, not a calibrated parameter, and not a recommendation. Change the price and the fee changes nonlinearly, which is the whole point of carrying exact arithmetic instead of eyeballing a percentage.
Serialization: where precision goes to die
You can do everything right in memory and still lose precision when you write results to disk or a wire format. A few habits prevent that:
- Serialize Decimals as strings, not floats. Writing a Decimal through a float encoder re-introduces binary error on the way out.
- Store prices in a single canonical unit (dollars or integer cents) and document which. Integer cents sidestep fractional representation entirely for whole-cent prices.
- Round only at defined boundaries, using an explicit rounding mode such as ROUND_HALF_UP, and round once rather than at every intermediate step. The decimal module lets you control rounding precisely, for example quantize to 0.01 with ROUND_HALF_UP.
- Keep marked value, settlement claim, credited cash, and hypothetical proceeds in separate fields. Collapsing them during export is unrecoverable.
What a paper result actually proves
Clean Decimal arithmetic makes a backtest or paper run internally consistent. It does not make it reality. A paper number is evidence under a specific data, fill, fee, and valuation model, not proof of profitable or real execution. The fee-rounding gap above is a concrete example of model risk hiding inside correct-looking math. Treat the precision work as table stakes for trustworthy research, then keep asking which model produced the number and where that model departs from the venue. Inspect the actual result object rather than assuming a setting reproduces exact current fees in every mode.
Sources
- decimal — Decimal fixed-point and floating-point arithmetic (Python 3 docs)
- Understanding Python Floats: Operations, Casting, and Best Practices
- Understanding the Difference Between float and decimal in Python
- decimal (Real Python reference)
- Floating Point Numbers in Python: What They Are and How to Use Them
- Kalshi Fees: Why the Cost Peaks Near 50 Cents (Allium)
- Kalshi Fees Explained (2026): Formula and Examples