Skip to content

Commit 2e7abba

Browse files
committed
Update rounding example
1 parent 239e5f1 commit 2e7abba

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

examples/rounding.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
"""
22
This example demonstrates how to round numbers when placing orders.
33
Both Price (px) and Size (sz) have a maximum number of decimals that are accepted.
4+
Prices can have up to 5 significant figures, but no more than MAX_DECIMALS - szDecimals decimal places where MAX_DECIMALS is 6 for perps and 8 for spot.
5+
For example, for perps, 1234.5 is valid but 1234.56 is not (too many significant figures).
6+
0.001234 is valid, but 0.0012345 is not (more than 6 decimal places).
7+
For spot, 0.0001234 is valid if szDecimals is 0 or 1, but not if szDecimals is greater than 2 (more than 8-2 decimal places).
8+
Integer prices are always allowed, regardless of the number of significant figures. E.g. 123456.0 is a valid price even though 12345.6 is not.
49
Prices are precise to the lesser of 5 significant figures or 6 decimals.
5-
For example, 1234.5 is valid but 1234.56 is not. 0.001234 is valid, but 0.0012345 is not.
6-
Sizes are rounded to the szDecimals of that asset.
7-
For example, if szDecimals = 3 then 1.001 is a valid size but 1.0001 is not.
810
You can find the szDecimals for an asset by making a meta request to the info endpoint
911
"""
1012
import json
@@ -30,10 +32,15 @@ def main():
3032
sz = 12.345678
3133
px = 1.2345678
3234
coin = "OP"
35+
max_decimals = 6 # change to 8 for spot
3336

3437
# If you use these directly, the exchange will return an error, so we round them.
35-
# First we round px to 5 significant figures and 6 decimals
36-
px = round(float(f"{px:.5g}"), 6)
38+
# First we check if price is greater than 100k in which case we just need to round to an integer
39+
if px > 100_000:
40+
px = round(px)
41+
# If not we round px to 5 significant figures and max_decimals - szDecimals decimals
42+
else:
43+
px = round(float(f"{px:.5g}"), max_decimals - sz_decimals[coin])
3744

3845
# Next we round sz based on the sz_decimals map we created
3946
sz = round(sz, sz_decimals[coin])

0 commit comments

Comments
 (0)