hyperliqbot/examples/basic_market_order.py
Cipher Zero Protocol a586f695ad cmt
2025-06-07 00:36:47 +03:00

42 lines
1.3 KiB
Python

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()