Hummingbot 第3章:快速上手
本章将带你从零开始配置 Hummingbot,连接交易所,创建并运行第一个交易策略。
首次启动配置
Docker 启动方式
# 启动容器并进入交互模式
docker start hummingbot
docker attach hummingbot
源码启动方式
# 激活环境并启动
conda activate hummingbot
hummingbot
初始化向导
首次启动时,Hummingbot 会引导你完成基础配置:
>>> Hummingbot Initialization Wizard <<<
Enter your exchange API key (press Enter to skip):
Enter your exchange secret key (press Enter to skip):
Enter your Telegram bot token (press Enter to skip):
Configuration saved to conf_client.yml
客户端配置文件
配置文件位于 conf/conf_client.yml,包含全局客户端设置:
# conf_client.yml
client_id: c0a1b2c3-d4e5-6789-0123-456789abcdef
# 日志配置
log_level: INFO
debug_console: false
# 策略配置文件路径
strategy_file_path: ./conf/
# Telegram 通知(可选)
telegram_mode: disabled
# telegram_token:
# telegram_chat_id:
# API 服务(可选)
api_server_host: 127.0.0.1
api_server_port: 15871
api_server_enabled: false
# 汇率数据源
rate_oracle_source: binance
交易所连接
获取交易所 API 凭证
以 Binance 为例,需要在交易所后台创建 API Key:
# 登录 Binance 账户
# 进入 API Management -> Create API
# 权限勾选: Spot & Margin Trading(现货交易)
# 限制: 建议只允许交易,不允许提现
连接交易所
在 Hummingbot 终端中连接交易所:
>>> connect binance
Enter your binance API key: your_api_key_here
Enter your binance API secret: your_api_secret_here
You are now connected to binance.
验证连接
>>> status
# 输出示例:
# ====== Status ======
# Connected to exchanges:
# - binance: OK
# Balances:
# - USDT: 1000.00
# - BTC: 0.05
# No strategy is currently running
创建第一个策略
创建策略配置
使用 Hummingbot 内置的配置向导创建 Pure Market Making 策略:
>>> create
Enter a new strategy name: pmm_btc
Select strategy type:
1. pure_market_making
2. cross_exchange_market_making
3. arbitrage
4. amm_arb
> 1
Configuring pure_market_making strategy...
Enter exchange: binance
Enter trading pair: BTC-USDT
Enter bid spread (in %): 0.5
Enter ask spread (in %): 0.5
Enter order amount (in base asset): 0.01
Enter order refresh time (in seconds): 60
Configuration saved to conf/pmm_btc.yml
手动编辑策略配置
也可直接编辑策略 YAML 文件:
# conf/pmm_btc.yml
strategy: pure_market_making
exchange: binance
trading_pair: BTC-USDT
# 订单参数
bid_spread: 0.5
ask_spread: 0.5
order_amount: 0.01
order_refresh_time: 60
# 高级参数
max_order_age: 300
order_refresh_tolerance_pct: 0.2
inventory_skew_enabled: false
filled_order_delay: 60
# 止损设置
stop_loss_spread: 5.0
price_ceiling: 100000
price_floor: 10000
运行机器人
导入并启动策略
>>> import pmm_btc.yml
Strategy imported: pmm_btc.yml
>>> start
##################################################
# Strategy pmm_btc.yml started #
# Exchange: binance #
# Trading Pair: BTC-USDT #
# Bids: 1 @ 50000.0 #
# Asks: 1 @ 50500.0 #
##################################################
监控运行状态
机器人运行中可以使用以下命令查看状态:
>>> status
# 输出示例:
# ===============================================
# Status
# ===============================================
# Active orders:
# - BUY 0.01 BTC @ 50000.0 (Created: 60s ago)
# - SELL 0.01 BTC @ 50500.0 (Created: 60s ago)
#
# Filled orders (24h):
# - Total: 5 trades
# - Volume: 0.05 BTC
# - Fees: 0.00025 BTC
#
# PnL (24h):
# - Total PnL: 12.50 USDT
# - Return %: 0.125%
# ===============================================
基本操作命令
核心命令速查
| 命令 | 说明 | 示例 |
|---|---|---|
connect | 连接或断开交易所 | connect binance |
create | 创建新策略配置 | create |
import | 导入策略配置文件 | import pmm_btc.yml |
start | 启动当前策略 | start |
stop | 停止当前策略 | stop |
status | 查看运行状态 | status |
history | 查看交易历史 | history --days 1 |
balance | 查看账户余额 | balance |
config | 查看或修改配置 | config |
exit | 退出 Hummingbot | exit |
实战操作示例
# 完整工作流程示例
1. connect binance # 连接交易所
2. create # 创建策略(按向导配置)
3. import pmm_btc.yml # 导入策略文件
4. start # 启动机器人
5. status # 监控运行状态
6. history --days 7 # 查看7天交易历史
7. stop # 停止机器人
8. exit # 退出
下一步
成功运行第一个策略后,可以继续学习以下内容:
- 策略参数调优:优化价差、订单数量和刷新频率
- 多策略管理:同时运行多个交易机器人
- 脚本开发:使用 Python 编写自定义交易逻辑
- 回测验证:在历史数据上测试策略效果