hyperliquid-trading-bot
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHyperliquid Trading Bot
Hyperliquid 交易机器人
Skill by ara.so — Daily 2026 Skills collection.
A configurable grid strategy runner for Hyperliquid DEX. Places layered buy/sell orders around a price range and enforces risk rules (stop loss, take profit, drawdown limits, rebalancing). Primary stack is TypeScript on Node.js 20.19+; a legacy Python tree exists for reference and learning examples.
由 ara.so 开发的Skill——2026每日技能合集。
一款适用于Hyperliquid DEX的可配置网格策略运行器。可在指定价格区间内挂分层买卖订单,并执行风险规则(止损、止盈、最大回撤限制、再平衡)。核心技术栈为Node.js 20.19+ 上的TypeScript;同时提供了遗留Python版本作为参考和学习示例。
Installation
安装
bash
git clone https://github.com/PolyPulse-Analytics/hyperliquid-trading-bot.git
cd hyperliquid-trading-bot
npm installbash
git clone https://github.com/PolyPulse-Analytics/hyperliquid-trading-bot.git
cd hyperliquid-trading-bot
npm installRequirements
环境要求
- Node.js 20.19+
- A Hyperliquid wallet private key (use a dedicated testnet key to start)
git
Optional for Python examples: uv
Environment Setup
环境配置
bash
cp .env.example .envEdit — minimum required keys:
.envenv
undefinedbash
cp .env.example .env编辑文件——至少需要配置以下密钥:
.envenv
undefinedTestnet (recommended to start)
测试网(建议初始使用)
HYPERLIQUID_TESTNET_PRIVATE_KEY=0xYOUR_TESTNET_PRIVATE_KEY_HERE
HYPERLIQUID_TESTNET=true
HYPERLIQUID_TESTNET_PRIVATE_KEY=0xYOUR_TESTNET_PRIVATE_KEY_HERE
HYPERLIQUID_TESTNET=true
Mainnet (real funds — set carefully)
主网(涉及真实资金——谨慎设置)
HYPERLIQUID_MAINNET_PRIVATE_KEY=0xYOUR_MAINNET_PRIVATE_KEY_HERE
HYPERLIQUID_MAINNET_PRIVATE_KEY=0xYOUR_MAINNET_PRIVATE_KEY_HERE
HYPERLIQUID_TESTNET=false
HYPERLIQUID_TESTNET=false
**Never commit `.env` or share your private key.**
---
**绝对不要提交`.env`文件或分享你的私钥。**
---Key Commands
核心命令
| Command | Purpose |
|---|---|
| Run bot using first |
| Validate YAML config structure (no private key needed) |
| Run automated tests (grid math, etc.) |
| TypeScript type check |
| Run with explicit config file |
| 命令 | 用途 |
|---|---|
| 使用 |
| 验证YAML配置结构(无需私钥) |
| 运行自动化测试(如网格计算等) |
| TypeScript类型检查 |
| 指定配置文件运行机器人 |
Explicit config path (recommended for multi-bot setups)
指定配置文件路径(多机器人场景推荐)
bash
npx tsx ts/src/runBot.ts bots/btc_conservative.yamlbash
npx tsx ts/src/runBot.ts bots/btc_conservative.yamlGraceful shutdown
优雅停机
Press Ctrl+C — the engine attempts to cancel all open orders before exiting. Always review logs to confirm cancellation.
按下Ctrl+C——引擎会尝试在退出前取消所有未成交订单。请务必查看日志确认取消状态。
Bot Configuration (YAML)
机器人配置(YAML)
Config files live in . Only one file should have when using auto-discovery via .
bots/*.yamlactive: truenpm start配置文件存放在目录下。使用自动发现配置时,仅应有一个文件设置。
bots/*.yamlnpm startactive: trueFull annotated example
完整带注释示例
yaml
name: "btc_grid_testnet"
active: true
exchange:
type: "hyperliquid"
testnet: true # Set false for mainnet — also set HYPERLIQUID_TESTNET=false in .env
account:
max_allocation_pct: 10.0 # Max % of account balance to allocate to this grid
grid:
symbol: "BTC" # Hyperliquid market symbol
levels: 10 # Number of buy/sell levels on each side
price_range:
mode: "auto" # "auto" or "manual"
auto:
range_pct: 5.0 # Spread ±5% from current price
# manual:
# lower: 90000
# upper: 100000
risk_management:
stop_loss_enabled: false
stop_loss_pct: 8.0 # Trigger if price drops X% from entry
take_profit_enabled: false
take_profit_pct: 15.0 # Trigger if price rises X% from entry
max_drawdown_pct: 15.0 # Max allowed drawdown before halting
max_position_size_pct: 40.0 # Max position as % of allocated capital
rebalance:
price_move_threshold_pct: 12.0 # Rebalance grid if price moves outside this %
monitoring:
log_level: "INFO" # DEBUG, INFO, WARN, ERRORyaml
name: "btc_grid_testnet"
active: true
exchange:
type: "hyperliquid"
testnet: true # 主网请设为false——同时需在.env中设置HYPERLIQUID_TESTNET=false
account:
max_allocation_pct: 10.0 # 分配给该网格的账户余额最大百分比
grid:
symbol: "BTC" # Hyperliquid市场交易对符号
levels: 10 # 买卖两侧的分层订单数量
price_range:
mode: "auto" # 可选"auto"或"manual"
auto:
range_pct: 5.0 # 基于当前价格上下浮动±5%
# manual:
# lower: 90000
# upper: 100000
risk_management:
stop_loss_enabled: false
stop_loss_pct: 8.0 # 价格较入场价下跌X%时触发止损
take_profit_enabled: false
take_profit_pct: 15.0 # 价格较入场价上涨X%时触发止盈
max_drawdown_pct: 15.0 # 允许的最大回撤比例,超过则暂停机器人
max_position_size_pct: 40.0 # 持仓占分配资金的最大比例
rebalance:
price_move_threshold_pct: 12.0 # 价格波动超过该比例时重新平衡网格
monitoring:
log_level: "INFO" # 可选DEBUG、INFO、WARN、ERRORManual price range example
手动价格区间示例
yaml
grid:
symbol: "ETH"
levels: 8
price_range:
mode: "manual"
manual:
lower: 3000
upper: 3600yaml
grid:
symbol: "ETH"
levels: 8
price_range:
mode: "manual"
manual:
lower: 3000
upper: 3600Common Patterns
常见使用场景
Running multiple bots with different configs
运行多个不同配置的机器人
Do not use — call each explicitly:
active: truebash
undefined不要设置——直接指定每个配置文件运行:
active: truebash
undefinedTerminal 1
终端1
npx tsx ts/src/runBot.ts bots/btc_conservative.yaml
npx tsx ts/src/runBot.ts bots/btc_conservative.yaml
Terminal 2
终端2
npx tsx ts/src/runBot.ts bots/eth_aggressive.yaml
undefinednpx tsx ts/src/runBot.ts bots/eth_aggressive.yaml
undefinedValidate before running (CI/pre-flight)
运行前验证(CI/预检查)
bash
npm run validate && npm startbash
npm run validate && npm startTestnet workflow before going live
上线前的测试网流程
- Set in
HYPERLIQUID_TESTNET=true.env - Set in your YAML
exchange.testnet: true - Fund via Hyperliquid testnet faucet
- Run then
npm run validatenpm start - Watch logs — verify orders appear in testnet UI
- Only then flip both flags to for mainnet
false
- 在中设置
.envHYPERLIQUID_TESTNET=true - 在YAML配置中设置
exchange.testnet: true - 通过Hyperliquid测试网水龙头获取测试资金
- 运行后执行
npm run validatenpm start - 查看日志——验证订单是否在测试网界面显示
- 确认无误后再将两个参数都改为切换到主网
false
Python Learning Examples
Python学习示例
bash
undefinedbash
undefinedInstall Python deps
安装Python依赖
uv sync
uv sync
Validate Python bot config
验证Python机器人配置
uv run src/run_bot.py --validate
uv run src/run_bot.py --validate
Run Python bot (legacy)
运行Python版机器人(遗留版本)
uv run src/run_bot.py
uv run src/run_bot.py
Educational scripts
教学脚本
uv run learning_examples/01_websockets/realtime_prices.py
uv run learning_examples/02_market_data/get_all_prices.py
uv run learning_examples/04_trading/place_limit_order.py
undefineduv run learning_examples/01_websockets/realtime_prices.py
uv run learning_examples/02_market_data/get_all_prices.py
uv run learning_examples/04_trading/place_limit_order.py
undefinedPython: Fetch real-time prices via WebSocket
Python:通过WebSocket获取实时价格
python
undefinedpython
undefinedlearning_examples/01_websockets/realtime_prices.py pattern
learning_examples/01_websockets/realtime_prices.py 示例代码
import asyncio
from hyperliquid.websocket_manager import WebsocketManager
async def on_message(msg):
print(msg)
async def main():
ws = WebsocketManager(base_url="https://api.hyperliquid-testnet.xyz")
await ws.subscribe({"type": "allMids"}, on_message)
await asyncio.sleep(30)
asyncio.run(main())
undefinedimport asyncio
from hyperliquid.websocket_manager import WebsocketManager
async def on_message(msg):
print(msg)
async def main():
ws = WebsocketManager(base_url="https://api.hyperliquid-testnet.xyz")
await ws.subscribe({"type": "allMids"}, on_message)
await asyncio.sleep(30)
asyncio.run(main())
undefinedPython: Place a limit order (testnet)
Python:挂限价单(测试网)
python
undefinedpython
undefinedlearning_examples/04_trading/place_limit_order.py pattern
learning_examples/04_trading/place_limit_order.py 示例代码
import os
from hyperliquid.exchange import Exchange
from hyperliquid.utils import constants
import eth_account
private_key = os.environ["HYPERLIQUID_TESTNET_PRIVATE_KEY"]
account = eth_account.Account.from_key(private_key)
exchange = Exchange(account, constants.TESTNET_API_URL)
import os
from hyperliquid.exchange import Exchange
from hyperliquid.utils import constants
import eth_account
private_key = os.environ["HYPERLIQUID_TESTNET_PRIVATE_KEY"]
account = eth_account.Account.from_key(private_key)
exchange = Exchange(account, constants.TESTNET_API_URL)
Place a limit buy for 0.001 BTC at $90,000
以90,000美元的价格挂0.001 BTC的限价买单
result = exchange.order(
"BTC",
is_buy=True,
sz=0.001,
limit_px=90000,
order_type={"limit": {"tif": "Gtc"}},
)
print(result)
---result = exchange.order(
"BTC",
is_buy=True,
sz=0.001,
limit_px=90000,
order_type={"limit": {"tif": "Gtc"}},
)
print(result)
---Troubleshooting
故障排查
Bot exits immediately without placing orders
机器人启动后立即退出,未下单
- Run first — check for YAML syntax errors
npm run validate - Confirm is set (or pass config path explicitly)
active: true - Verify in
HYPERLIQUID_TESTNETmatches.envin YAMLexchange.testnet
- 先运行——检查YAML语法错误
npm run validate - 确认配置文件中设置了(或明确传入配置文件路径)
active: true - 验证中的
.env与YAML中的HYPERLIQUID_TESTNET参数一致exchange.testnet
Orders placed but immediately cancelled
订单已挂单但立即被取消
- Testnet account may have insufficient balance — refund via faucet
- may be too low for minimum order sizes on the symbol
max_allocation_pct
- 测试网账户余额不足——通过水龙头补充资金
- 设置过低,无法满足该交易对的最小下单量要求
max_allocation_pct
TypeScript compilation errors
TypeScript编译错误
bash
npx tsc --noEmitCheck Node.js version — must be 20.19+:
bash
node --versionbash
npx tsc --noEmit检查Node.js版本——必须为20.19+:
bash
node --versionPython uv
not found
uvPython uv
未找到
uvbash
pip install uvbash
pip install uvor
或
curl -LsSf https://astral.sh/uv/install.sh | sh
undefinedcurl -LsSf https://astral.sh/uv/install.sh | sh
undefinedPrivate key errors / authentication failures
私钥错误/认证失败
- Confirm the key in matches the funded testnet wallet
.env - Key must include prefix
0x - Never use a mainnet key on testnet configs — keep separate keys
- 确认中的密钥与已充值的测试网钱包一致
.env - 密钥必须包含前缀
0x - 绝对不要在测试网配置中使用主网密钥——请分开使用不同密钥
Grid not rebalancing when price moves
价格波动时网格未重新平衡
Check — default is . Lower it if you want more frequent rebalancing (increases gas/fees).
risk_management.rebalance.price_move_threshold_pct12.0检查——默认值为。若需要更频繁的再平衡可降低该值(会增加Gas/手续费)。
risk_management.rebalance.price_move_threshold_pct12.0Project Structure
项目结构
hyperliquid-trading-bot/
├── bots/ # YAML strategy configs
│ └── btc_conservative.yaml
├── ts/src/
│ └── runBot.ts # Main TypeScript entrypoint
├── src/
│ └── run_bot.py # Legacy Python entrypoint
├── learning_examples/ # Educational Python scripts
│ ├── 01_websockets/
│ ├── 02_market_data/
│ └── 04_trading/
├── .env.example # Environment variable template
├── package.json
└── pyproject.tomlhyperliquid-trading-bot/
├── bots/ # YAML策略配置文件
│ └── btc_conservative.yaml
├── ts/src/
│ └── runBot.ts # TypeScript主入口文件
├── src/
│ └── run_bot.py # Python遗留版本入口文件
├── learning_examples/ # Python教学脚本
│ ├── 01_websockets/
│ ├── 02_market_data/
│ └── 04_trading/
├── .env.example # 环境变量模板
├── package.json
└── pyproject.tomlRisk Reminder
风险提示
- Always start on testnet with a dedicated key
- Use to limit exposure
max_allocation_pct - Enable as a safety net before enabling stop loss/take profit
max_drawdown_pct - Review open orders in the Hyperliquid UI after every bot restart
- This software is provided "as is" — you are responsible for your capital
- 始终使用专用密钥从测试网开始
- 使用限制资金暴露
max_allocation_pct - 在启用止损/止盈前,先开启作为安全保障
max_drawdown_pct - 每次机器人重启后,在Hyperliquid界面检查未成交订单
- 本软件按“原样”提供——你需自行对资金负责