Hello community,
what is your choice of data type for representing numeric quantities that must be precise and stay precise after mathematical operations and why?
My goal is to handle numeric values such as money amounts, order quantities and prices as precisely as possible. "Precise" here means "has the smallest deviation possible from the actual mathematical result"
Example of an issue when adding position sizes:
input: 0.7 + 0.6
python output: 1.2999999999999998
mathematical result: 1.3
If this small error keeps propagating after 1000s of operations, it leads to some differences. Example: 5 years backtest with 5000 trades, each with a small PnL error, could add up to a wrong backtest PnL. And not only PnL. Quantities, limits, entry/exit prices could all be wrong by some factor.
For a backtesting environment I am considering the Decimal class (https://docs.python.org/3/library/decimal.html) as simple floats have the above precision issues (https://docs.python.org/3/tutorial/floatingpoint.html).
For a test run with a broker and live market data I am unsure whether Decimal is sufficient.
One possibility for a live run could be a custom "Money" class for money quantities but it seems overkill for a backtest:
https://pypi.org/project/py-moneyed/ (Last release 2018)
https://code.google.com/archive/p/python-money/ (old/not maintained. Last release: 2011)
Plus: using such class for non-money quantities such as order quantity would be indequate design (e.g. an order quantity is not a money entity)
Further questions:
- Should I stick with Decimal for backtesting for simplicity?
- What about live trading?
- Should I attempt to round (truncate?) the result of each operation (+,-,*,/,...) to N decimal digits after every operation?
- Is this a problem at all or am I being too pedantic?
Any insight is welcome. Ideally from experience with live trading applications and real money.
Thanks and kind regards,
~s