Heuristic:51bitquant Ai hedge fund crypto Quantity Decimal Precision
| Knowledge Sources | |
|---|---|
| Domains | Optimization, Quant_Trading |
| Last Updated | 2026-10-28 09:34 GMT |
Overview
Trade quantities are rounded to 3 decimal places (QUANTITY_DECIMALS = 3) to mimic exchange lot sizing and avoid floating-point dust in position tracking.
Description
The constant QUANTITY_DECIMALS = 3 in src/utils/constants.py controls how trade quantities are rounded throughout the system. In Backtester.execute_trade, every incoming quantity is rounded via round(float(quantity), QUANTITY_DECIMALS) before any position update. This ensures quantities like 0.123456789 become 0.123, matching the precision that real exchanges enforce. It also prevents floating-point accumulation errors from creating phantom positions over thousands of trades.
Usage
This heuristic is always active during backtest trade execution. If you are simulating a different exchange with different lot-size rules (e.g., some exchanges use 8 decimals for crypto), adjust QUANTITY_DECIMALS in src/utils/constants.py.
The Insight (Rule of Thumb)
- Action: Set
QUANTITY_DECIMALSto match the exchange's step size for the asset being traded. - Value:
3(i.e., 0.001 minimum quantity increment). - Trade-off: Lower precision may cause rounding errors in small positions; higher precision may not match exchange execution.
- Compatibility: Applied uniformly to all tickers in the current implementation. Real exchanges have per-symbol step sizes — this is a simplification.
Reasoning
Exchanges like Binance enforce lot size filters (e.g., LOT_SIZE of 0.001 for BTCUSDT). Rounding quantities to the exchange's step size ensures the backtest produces trade sizes that would actually be accepted by the exchange. Additionally, floating-point arithmetic on quantities like 0.1 + 0.2 yields 0.30000000000000004 — rounding to 3 decimals eliminates this noise from position tracking and display.
Code Evidence
Constant definition from src/utils/constants.py:15:
QUANTITY_DECIMALS = 3
Rounding in trade execution from src/backtest/backtester.py:95:
quantity = round(float(quantity), QUANTITY_DECIMALS) # force to keep just 0.001
Display formatting from src/utils/util_func.py:110:
f"{action_color}{quantity:,.{QUANTITY_DECIMALS}f}{Style.RESET_ALL}",