The Hyperliquid API is a powerful tool for building trading bots, but its WebSocket feeds and order management can be tricky to navigate. I've spent countless hours working with the API, and I've learned a thing or two about how to get the most out of it. In this post, I'll share some of my experiences and provide a guide to using the Hyperliquid API for building a trading bot.
Introduction to the Hyperliquid API
The Hyperliquid API provides real-time market data and allows you to place orders directly on the exchange. It's a WebSocket-based API, which means you need to establish a persistent connection to receive updates. The API uses a standard WebSocket protocol, but it has some quirks that can catch you off guard. For example, the reconnect timeout is 90 minutes, which means your bot will need to handle disconnections and reconnects gracefully.
Setting up the WebSocket Connection
To establish a WebSocket connection, you'll need to use a library like websocket-client. Here's an example of how to set up a basic connection:
import websocket
import json
def on_open(ws):
print("Connected to Hyperliquid API")
subscribe_message = {
"type": "subscribe",
"channels": ["ticker", "orderBookL2"],
"markets": ["ETHUSDT"]
}
ws.send(json.dumps(subscribe_message))
def on_message(ws, message):
print(f"Received message: {message}")
def on_error(ws, error):
print(f"Error: {error}")
def on_close(ws, close_status_code, close_msg):
print(f"Disconnected from Hyperliquid API")
ws = websocket.WebSocketApp("wss://api.hyperliquid.io/ws",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
This code sets up a basic WebSocket connection and subscribes to the ticker and orderBookL2 channels for the ETHUSDT market.
Handling Orders and Positions
Once you have a WebSocket connection established, you can start placing orders and managing positions. The Hyperliquid API uses a standard REST API for placing orders, but you can also use the WebSocket API to place orders and cancel existing ones. One thing to watch out for is the 400 Bad Request error code, which can occur if your order is invalid or if you're trying to place an order that would exceed your account balance.
Putting it all Together
I've spent a lot of time working with the Hyperliquid API, and I've built a number of trading bots that use it. I actually packaged this into a tool called hyperliquid perpetual futures trading bot if you want the full working version. This bot supports long and short positions, up to 50x leverage, and includes built-in risk management features like trailing stop-loss. It's been a game-changer for my own trading, and I think it could be useful for others as well.
Next Steps
Now that you have a basic understanding of the Hyperliquid API and how to use it, you can start building your own trading bot. One thing to keep in mind is that the API is still evolving, and new features are being added all the time. I've found that it's a good idea to keep an eye on the official documentation and to stay up-to-date with the latest changes. You can also use tools like websocket-client to handle the underlying WebSocket connection, which can save you a lot of time and hassle. With the right tools and a bit of practice, you should be able to build a trading bot that can help you navigate the complex world of perpetual futures trading... and I'm still trying to figure out how to optimize the bot's performance, maybe by using a more advanced strategy or by refining the risk management parameters, but that's a topic for another time, and I'm not sure where to start, maybe I'll try using a genetic algorithm to evolve the strategy, or maybe I'll just stick with the simple momentum-based approach, either way, it's going to be a challenge...
Also available on Payhip with instant PayPal checkout.
If you need a server to run your bots 24/7, I use DigitalOcean — $200 free credit for new accounts.
Top comments (0)