You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/rounding.py
+12-5Lines changed: 12 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,12 @@
1
1
"""
2
2
This example demonstrates how to round numbers when placing orders.
3
3
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.
4
9
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.
8
10
You can find the szDecimals for an asset by making a meta request to the info endpoint
9
11
"""
10
12
importjson
@@ -30,10 +32,15 @@ def main():
30
32
sz=12.345678
31
33
px=1.2345678
32
34
coin="OP"
35
+
max_decimals=6# change to 8 for spot
33
36
34
37
# 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
+
ifpx>100_000:
40
+
px=round(px)
41
+
# If not we round px to 5 significant figures and max_decimals - szDecimals decimals
0 commit comments