-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathbasic_market_order.py
41 lines (30 loc) · 1.27 KB
/
basic_market_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import time
import example_utils
from hyperliquid.utils import constants
def main():
address, info, exchange = example_utils.setup(constants.TESTNET_API_URL, skip_ws=True)
coin = "ETH"
is_buy = False
sz = 0.05
print(f"We try to Market {'Buy' if is_buy else 'Sell'} {sz} {coin}.")
order_result = exchange.market_open(coin, is_buy, sz, None, 0.01)
if order_result["status"] == "ok":
for status in order_result["response"]["data"]["statuses"]:
try:
filled = status["filled"]
print(f'Order #{filled["oid"]} filled {filled["totalSz"]} @{filled["avgPx"]}')
except KeyError:
print(f'Error: {status["error"]}')
print("We wait for 2s before closing")
time.sleep(2)
print(f"We try to Market Close all {coin}.")
order_result = exchange.market_close(coin)
if order_result["status"] == "ok":
for status in order_result["response"]["data"]["statuses"]:
try:
filled = status["filled"]
print(f'Order #{filled["oid"]} filled {filled["totalSz"]} @{filled["avgPx"]}')
except KeyError:
print(f'Error: {status["error"]}')
if __name__ == "__main__":
main()