基于 HyperLiquid Exchange 的现货量化交易系统指南

基于 HyperLiquid Exchange 的现货量化交易系统指南

  • 备注:PlatON 在此处作为平台收单链,Python 版本是 3.10.16

安装 SDK

pip install hyperliquid-python-sdk

如果遇到编译错误,提示工具链无法运行,执行修复命令

pip install --upgrade setuptools

安装必要的第三方库

pip install eth_account

创建 config.json

{
    "comments": "",
    "secret_key": "你的 HyperLiquid 交易所的 API 密钥",
    "account_address": "你的 HyperLiquid 交易所的账号地址",
}

创建 utils.py

import json
import os

import eth_account
from eth_account.signers.local import LocalAccount

from hyperliquid.exchange import Exchange
from hyperliquid.info import Info


def setup(base_url=None, skip_ws=False):
    config_path = os.path.join(os.path.dirname(__file__), "config.json")
    with open(config_path) as f:
        config = json.load(f)
    account: LocalAccount = eth_account.Account.from_key(config["secret_key"])
    address = config["account_address"]
    if address == "":
        address = account.address
    print("Running with account address:", address)
    if address != account.address:
        print("Running with agent address:", account.address)
    info = Info(base_url, skip_ws)
    user_state = info.user_state(address)
    spot_user_state = info.spot_user_state(address)
    margin_summary = user_state["marginSummary"]
    if float(margin_summary["accountValue"]) == 0 and len(spot_user_state["balances"]) == 0:
        print("Not running the example because the provided account has no equity.")
        url = info.base_url.split(".", 1)[1]
        error_string = f"No accountValue:\nIf you think this is a mistake, make sure that {address} has a balance on {url}.\nIf address shown is your API wallet address, update the config to specify the address of your account, not the address of the API wallet."
        raise Exception(error_string)
    exchange = Exchange(account, base_url, account_address=address)
    return address, info, exchange

创建 spot_order.py

import json

import utils as utils

from hyperliquid.utils import constants

PURR = "PURR/USDC"

def main():
    # 如果是测试网,base_url 修改为 constants.TESTNET_API_URL
    address, info, exchange = utils.setup(base_url=constants.MAINNET_API_URL, skip_ws=True)

    # 获取账号状态并输出信息
    spot_user_state = info.spot_user_state(address)
    if len(spot_user_state["balances"]) > 0:
        print("spot balances:")
        for balance in spot_user_state["balances"]:
            print(json.dumps(balance, indent=2))
    else:
        print("no available token balances")

    # 限价委托,挂单设置
    # exchange.order(<币对>, <True: 买入; False: 卖出> <数量>, <限价>, <挂单方式,Gtc 是指一直等到成交结束>)
    # 切记,最低挂单 10 USDC,即 数量*限价 >=10
    order_result = exchange.order(PURR, True, 30, 0.35, {"limit": {"tif": "Gtc"}})
    print(order_result)

    # 查询订单状态
    if order_result["status"] == "ok":
        status = order_result["response"]["data"]["statuses"][0]
        if "resting" in status:
            order_status = info.query_order_by_oid(address, status["resting"]["oid"])
            print("Order status by oid:", order_status)

    # 取消订单
    # if order_result["status"] == "ok":
    #     status = order_result["response"]["data"]["statuses"][0]
    #     if "resting" in status:
    #         cancel_result = exchange.cancel(PURR, status["resting"]["oid"])
    #         print(cancel_result)

if __name__ == "__main__":
    main()

有了上述的用例引导,相信社区写出网格交易也就不是什么难事了

另外,再提示一下,如果 exchange.order(“HYPE”, ……) 则是 prep 交易

虽然我看不懂 但是我感觉很厉害 哈哈

1 个赞