Skip to content

Commit 67177e4

Browse files
committed
Add example of rounding
1 parent 3d5cd6e commit 67177e4

File tree

8 files changed

+335
-47
lines changed

8 files changed

+335
-47
lines changed

api/components.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ components:
99
AssetPosition:
1010
type: object
1111
MarginSummary:
12-
type: object
12+
type: object
13+
L2Level:
14+
type: object

api/info/assetctxs.yaml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Active Trading Assets API
4+
version: 1.0.0
5+
servers:
6+
- url: https://api.hyperliquid.xyz
7+
paths:
8+
/info:
9+
post:
10+
summary: Retrieve metadata and context information for actively trading assets
11+
requestBody:
12+
content:
13+
application/json:
14+
schema:
15+
type: object
16+
properties:
17+
type:
18+
type: string
19+
enum: ["metaAndAssetCtxs"]
20+
example:
21+
type: "metaAndAssetCtxs"
22+
required: true
23+
responses:
24+
'200':
25+
description: Successful response
26+
content:
27+
application/json:
28+
schema:
29+
type: array
30+
items:
31+
anyOf:
32+
- type: object
33+
properties:
34+
universe:
35+
type: array
36+
items:
37+
type: object
38+
properties:
39+
name:
40+
type: string
41+
szDecimals:
42+
type: integer
43+
- type: array
44+
items:
45+
type: object
46+
properties:
47+
dayNtlVlm:
48+
type: string
49+
funding:
50+
type: string
51+
markPx:
52+
type: string
53+
openInterest:
54+
type: string
55+
oraclePx:
56+
type: string
57+
prevDayPx:
58+
type: string
59+
example:
60+
- universe:
61+
- name: "BTC"
62+
szDecimals: 5
63+
- - dayNtlVlm: "3559323.53447"
64+
funding: "-0.0000886"
65+
markPx: "28429.3"
66+
openInterest: "0.22168"
67+
oraclePx: "28445.0"
68+
prevDayPx: "29368.0"

api/info/candle.yaml

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Hyperliquid
4+
description: API for interacting with the Hyperliquid DEX
5+
version: '0.1'
6+
servers:
7+
- url: https://api.hyperliquid.xyz
8+
description: Mainnet
9+
paths:
10+
/info:
11+
post:
12+
summary: Retrieve candle snapshot.
13+
requestBody:
14+
required: true
15+
content:
16+
application/json:
17+
schema:
18+
type: object
19+
properties:
20+
type:
21+
type: string
22+
enum: [candleSnapshot]
23+
description: Type of request being made.
24+
req:
25+
type: object
26+
properties:
27+
coin:
28+
type: string
29+
description: The asset being queried.
30+
interval:
31+
type: string
32+
description: The time interval of the candles, e.g. "1m", "15m", "1h", "1d".
33+
startTime:
34+
type: integer
35+
description: The timestamp of the first candle in the snapshot.
36+
endTime:
37+
type: integer
38+
description: The timestamp of the last candle in the snapshot.
39+
required:
40+
- coin
41+
- interval
42+
- startTime
43+
- endTime
44+
example:
45+
coin: "BTC"
46+
interval: "15m"
47+
startTime: 1681923833000
48+
endTime: 1681923833000
49+
example:
50+
type: candleSnapshot
51+
req:
52+
coin: "BTC"
53+
interval: "15m"
54+
startTime: 1681923833000
55+
endTime: 1681923833000
56+
responses:
57+
'200':
58+
description: A successful response
59+
content:
60+
application/json:
61+
schema:
62+
type: array
63+
items:
64+
type: object
65+
properties:
66+
T:
67+
type: integer
68+
description: The timestamp of the end of the candle.
69+
c:
70+
type: string
71+
description: The closing price of the candle.
72+
h:
73+
type: string
74+
description: The highest price reached during the candle.
75+
i:
76+
type: string
77+
description: The time interval of the candle.
78+
l:
79+
type: string
80+
description: The lowest price reached during the candle.
81+
n:
82+
type: integer
83+
description: The number of trades that occurred during the candle.
84+
o:
85+
type: string
86+
description: The opening price of the candle.
87+
s:
88+
type: string
89+
description: The asset being queried.
90+
t:
91+
type: integer
92+
description: The timestamp of the beginning of the candle
93+
v:
94+
type: string
95+
description: The volume traded during the candle.
96+
example:
97+
- T: 1681924499999
98+
c: "29258.0"
99+
h: "29309.0"
100+
i: "15m"
101+
l: "29250.0"
102+
n: 189
103+
o: "29295.0"
104+
s: "BTC"
105+
t: 1681923600000
106+
v: "0.98639"
107+

api/info/l2book.yaml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
openapi: 3.0.1
2+
info:
3+
title: Hyperliquid
4+
description: API for interacting with the Hyperliquid DEX
5+
version: '0.1'
6+
servers:
7+
- url: https://api.hyperliquid.xyz
8+
description: Mainnet
9+
- url: https://api.hyperliquid-testnet.xyz
10+
description: Testnet
11+
- url: http://localhost:3001
12+
description: Local
13+
paths:
14+
/info:
15+
post:
16+
summary: Retrieve L2 book snapshot.
17+
description: Returns the top 10 bids and asks of the order book.
18+
requestBody:
19+
required: true
20+
content:
21+
application/json:
22+
schema:
23+
type: object
24+
properties:
25+
type:
26+
type: string
27+
enum: [l2Book]
28+
coin:
29+
type: string
30+
required:
31+
- type
32+
- coin
33+
example:
34+
type: l2Book
35+
coin: "BTC"
36+
responses:
37+
'200':
38+
description: A successful response
39+
content:
40+
application/json:
41+
schema:
42+
type: array
43+
items:
44+
type: array
45+
items:
46+
type: object
47+
properties:
48+
px:
49+
type: string
50+
sz:
51+
type: string
52+
n:
53+
type: integer
54+
example:
55+
[
56+
[
57+
{ "px": "19900", "sz": "1", "n": 1 },
58+
{ "px": "19800", "sz": "2", "n": 2 },
59+
{ "px": "19700", "sz": "3", "n": 3 }
60+
],
61+
[
62+
{ "px": "20100", "sz": "1", "n": 1 },
63+
{ "px": "20200", "sz": "2", "n": 2 },
64+
{ "px": "20300", "sz": "3", "n": 3 }
65+
]
66+
]
67+

examples/rounding.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
This example demonstrates how to round numbers when placing orders.
3+
Both Price (px) and Size (sz) have a maximum number of decimals that are accepted.
4+
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+
You can find the szDecimals for an asset by making a meta request to the info endpoint
9+
"""
10+
from eth_account.signers.local import LocalAccount
11+
import eth_account
12+
import json
13+
import utils
14+
15+
from hyperliquid.info import Info
16+
from hyperliquid.exchange import Exchange
17+
from hyperliquid.utils import constants
18+
19+
20+
def main():
21+
config = utils.get_config()
22+
account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
23+
print("Running with account address:", account.address)
24+
info = Info(constants.TESTNET_API_URL, skip_ws=True)
25+
26+
# Get the exchange's metadata and print it out
27+
meta = info.meta()
28+
print(json.dumps(meta, indent=2))
29+
30+
# create a szDecimals map
31+
sz_decimals = {}
32+
for info in meta["universe"]:
33+
sz_decimals[info["name"]] = info["szDecimals"]
34+
35+
# For demonstration purposes we'll start with a price and size that have too many digits
36+
sz = 12.345678
37+
px = 1.2345678
38+
coin = "OP"
39+
40+
# If you use these directly, the exchange will return an error, so we round them.
41+
# First we round px to 5 significant figures and 6 decimals
42+
px = round(float(f"{px:.5g}"), 6)
43+
44+
# Next we round sz based on the sz_decimals map we created
45+
sz = round(sz, sz_decimals[coin])
46+
47+
print(f"placing order with px {px} and sz {sz}")
48+
exchange = Exchange(account, constants.TESTNET_API_URL)
49+
order_result = exchange.order(coin, True, sz, px, {"limit": {"tif": "Gtc"}})
50+
print(order_result)
51+
52+
# Cancel the order
53+
if order_result["status"] == "ok":
54+
status = order_result["response"]["data"]["statuses"][0]
55+
if "resting" in status:
56+
cancel_result = exchange.cancel(coin, status["resting"]["oid"])
57+
print(cancel_result)
58+
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)