Number Formats
Understand the number and date formats used in the 🍋.markets Brokerage API
This section will describe number formats used in our API and also tell you why these
decisions were made.
Monetary Values
Being a financial institution, we put great store by calculating correctly. The default implementation for floating pointer numbers are not suitable for these kinds of calculations.
The two most important problems with floating point numbers are arithmetically imprecise operation and data munging.
Solution
To avoid the aforementioned issues at lemon.markets, we specify monetary values as fixed-point fractions numbers with 4 decimal places after the decimal point. These numbers are wrapped in Strings to provide a precise expression going forward, e.g. "123.4567"
.
Programming languages with a decimal number type can correctly represent these numbers in memory (in contrast to floating-point numbers). Please refer to these data types if possible:
- Java:
BigDecimal
- Python:
decimal
- Swift:
Decimal
Precision
The lemon.markets platform uses different levels of precision in different contexts of usage.
Monetary Amounts
Monetary amounts are specified by using either two or four decimal places:
- 2 decimal places are used for transaction values, e.g. a deposit, paying taxes, buying/selling securities. (Whenever you think of something as changing the balance of your account, we’ll use two decimal places.)
- 4 decimal places are traditionally used for share prices of individual instruments.
Share Quantities
Quantities of shares can be expressed with up to 5 decimal places.
Example
Looking at this trade as an example, we can see all types precisions being used:
- Monetary Amount (2 decimal places)
amount
fee
- Monetary Amount (4 decimal places)
price
- Share Quantity (5 decimal places)
quantity
{
"amount": "100.00",
"currency": "EUR",
"execution_venue": "XGAT",
"fee": "0.10",
"id": "tr_611041a7e62c4e7d82a8dd86f91e7277",
"instrument": "IE00B0M62X26",
"order": "bord_b088cafa88564670a4b7e1acc7e30b5b",
"price": "216.0199",
"quantity": "0.46292",
"side": "buy",
"taxes": []
}
Further Reading
- Decimal vs. Float in Python, a short introduction into the problems in dealing with floating point numbers.
- Twitter IDs, a quick overview for Twitter's solution to handle numbers that cannot be represented as numbers in JavaScript.
- Goldberg 1991, What every computer scientist should know about floating-point arithmetic, a scientific exploration into the traps and boundaries of dealing with floating-point numbers.
Updated 7 months ago