r/quant • • 12d ago

Technical Infrastructure Decimals, floats, kdb

It's often touted not to use floating point types in finance, and instead store as integers with some multiplier or use fixed decimal types.

Yet kdb is/was rather popular for storing tick and derived data, and has no concept of a decimal data type.

What gives here?

18 Upvotes

35 comments sorted by

42

u/Regular-Hotel892 12d ago

It may be touted but it’s not rooted in reality, firms do use floats

18

u/ChooseCarefully100 12d ago

Makes sense for a bank, or for operations/accounting. Should my pricing/trading logic care if an input price is 14.9999997 instead of 15? Maybe not

12

u/EngineeringApart4606 12d ago edited 12d ago

I’m still waiting to see the real problem case that double-precision can’t handle, after ~2 decades in the industry

Like not a vague description of a problem, or an “if you get it wrong, then it’s wrong” type statement, but a specific number and set of operations that you would encounter in a real system that couldn’t work in binary double precision.

I’ve even seen string decimal to binary double and back be provably lossless to the limits of double precision (almost 16 sig figs), so long as the decimal strings had enough precision (from memory, 17 sig figs in, 15 out.

I’ve also met a surprising number of educated people who think floating-point numbers are either magical infinite precision, or some completely arbitrary bit-mangling machine that cannot be trusted to do anything without discarding precision in a completely unfathomable way

2

u/dazzleator147 10d ago

It is very true that a common misconception among computer scientists is that floating points are non-deterministic. They can lose precision, yes, but deterministically. The same operations always lead to the same result.

The caveat here is that it's deterministic within a compiler version, but practically that just means you have to regenerate your backtest goldens when you upgrade.

0

u/[deleted] 12d ago

[deleted] — view deleted comment

2

u/EngineeringApart4606 11d ago

Yes, don’t get me wrong, I’m also a big proponent of fixed-point where that fits too, and/or of not touching what’s not broken etc.,

I’m just referring to situations where double-precision is the existing or obvious choice and people start with the ghost stories.

3

u/starostise 12d ago

In our case, floats are sent as strings from the data source.

We store them as strings in database and we convert them to floats in RAM for computations. We never round during computations.

We only round the output (display and API requests).

1

u/m_a_n_t_i_c_o_r_e 12d ago

Wdym you never round during computations? Do you mean that you never explicitly round/truncate?

2

u/starostise 11d ago edited 11d ago

Rounding during computations makes us loose precision and imprecision grows as one accumulates roundings.

1

u/m_a_n_t_i_c_o_r_e 11d ago

Yes obviously but I’m saying you can’t avoid it. The second you “convert them to floats in RAM for computations” you’re rounding. Like yeah sure technically there is a vanishingly small subset of possible reduction trees in which no rounds occur but lol no.

0

u/starostise 11d ago

The second you “convert them to floats in RAM for computations” you’re rounding

This conversion, as well as the values ​​held in memory, depend on the CPU instructions being used.

Compiling Python, Pandas, and NumPy natively —using the appropriate CPU flags— ensures that the level of precision remains more than sufficient.

2

u/m_a_n_t_i_c_o_r_e 11d ago

Fine, you have a different (significantly more relaxed) definition of “rounding” than the rest of the computing industry. Good luck with that.

0

u/starostise 11d ago

We never faced any error so far. How do you proceed ?

3

u/m_a_n_t_i_c_o_r_e 11d ago

Most people/systems don’t experience any observable/material effects due to rounding! That was my original point — like, of course you’re rounding, everyone is rounding all the time. It’s ludicrous to claim that you’re “not rounding”

But since you asked, there are very interesting bespoke options! (primarily from the HPC community)
- Kahan summation (and successors thereof)
- cancellation detectors like CADNA
- super accumulator approaches
- rounding/carry-free parallel reductions (eg Demmel lab’s work)
- old fashioned bigfloats

I’ve never seen any of these in production at trading firms specifically, but they’re there if truly needed.

1

u/EngineeringApart4606 11d ago

Maybe he’s using Ziv’s multilevel strategy and recomputing at arbitrarily high precisions when encountering difficult-to-round cases? I wouldn’t bet on it though…

2

u/m_a_n_t_i_c_o_r_e 11d ago

lol sure I’ll take your action on that :)

1

u/starostise 10d ago

Ok. I meant that we don't explicitly round in the computing part of the code. Only right before displaying, computing transactions fees and executing.

1

u/Independent-Quote923 12d ago

So you cant do any arithmetics in db?

1

u/starostise 11d ago edited 11d ago

No operation in db except selecting the historical data that fits inside the frame that is used for real time analysis in RAM with the right dtype.

Basically the whole history of a market sits in db but decisions are made with only few days of history.

1

u/desi_cutie4 12d ago

yeah crypto is a pain in the ass.

1

u/starostise 11d ago edited 11d ago

Yeah, we need to ceil at the right decimal but only at the end of computations.

1

u/FireWrath9 10d ago

Why store them as strings? wouldn't storing them as a float in binary be much faster to parse as well as take up less storage?

1

u/starostise 10d ago

It's just a preference to avoid alteration of the data we receive.

Storing transactions (we're not using candles) prevents from making many API calls to fetch all transactions in the considered time frame.

Our db is read and the list of transactions is converted only once at the engine's initialization. Then it is only entries to the db and to RAM at the same time and in real time.
There can be weeks between two consecutive reads while there are many writes per second.

All the logic is done in RAM with the right data type because it is faster.

1

u/lordnacho666 12d ago

You either just use floats and figure out some rounding, or you store the number as an int and have a table that tells you how many decimals it has.

1

u/Stunning_Carry784 12d ago

Just don't use unsigned ints for quantities, that's a sure fire way to go broke pretty quickly.

1

u/NoNameSwitzerland 12d ago

How can you go broke with unsigned, when instead of going negative it overflows to billionaire level?

1

u/desi_cutie4 12d ago

Use floats where accuracy is not required.

For example, I would run my entire pipeline in floats and only round to ticks before sending orders. Btw this is for sub-minute options strategy so it doesn't matter.

1

u/jeffjeffjeffw 12d ago

Interesting as coincidentially I've encountered so many float64 vs float32 issues recently

1

u/algoseekHQ 9d ago

I think it really depends on what you're doing with the data. Float64 is perfectly reasonable for most market data analysis and modeling. But when you need exact values, like cash balances, settlement, or tick-level price comparisons, decimals or scaled integers make more sense. You don't necessarily need the same representation for storage and computation.

1

u/supercoco9 11d ago

Disclaimer: I work at QuestDB.

We had a lot of users working at crypto or market data complaining that not having a proper decimal type was an issue for them, forcing them to use workarounds like some of the ones described at the comments here.

We eventually added a decimal datatype about a year ago https://questdb.com/blog/questdb-9-2-release/. Most users still go with DOUBLE by default, as it is lighter and good enough, but when you really need a decimal, nothing beats storing your numbers and knowing arithmetic will work

0

u/nrs02004 12d ago

I think strings are the safest -- prices aren't meant to be mutable.

1

u/supercoco9 11d ago

Strings have a few downsides. Not only that it is hard to do math with them, but also in terms of storage, they typically take much more than a dedicted numeric type.

If data is small enough, and you don't need to operate on them, strings are a good solution though. It all depends :)

0

u/Academic-Village2923 11d ago

Genuine question how popular is kdb really and is this thing going to stay with pandas etc being so handy?

1

u/bleeuurgghh 11d ago

Used in pretty much every major bank and a lot of hedge funds. Not about to be replaced by Python as they’re already used alongside each other. In competition with other newer but less established columnar databases.

0

u/QuantGrindApp 11d ago

Still pretty entrenched at banks and a lot of older trading shops, mostly as the tick database where years of order book data live and get queried. Pandas isn't really competing there since it chokes on that much data in memory. Newer shops tend to skip the license cost and go polars/parquet or something like ClickHouse though, so I'd expect it to slowly shrink rather than vanish.