VBrokers Trader
Automate trading on VBrokers (华盛通 VCL HK) via the OpenAPI Gateway running locally.
Prerequisites
- - OpenAPI Gateway must be running locally (GUI app:
华盛通OpenAPIGateway.app) - Gateway URL: INLINECODE1
- AES key for password encryption: provided during account setup (see references/api-reference.md)
- Python packages:
pycryptodome (pip install pycryptodome)
Quick Start
Copy scripts/vbrokers_client.py to your project and import it:
CODEBLOCK0
Key Concepts
Request Format (Critical)
All HTTP requests must use nested
params:
CODEBLOCK1
Exchange Types
| Code | Market |
|---|
| INLINECODE6 | US Stocks |
| INLINECODE7 |
HK Stocks |
|
v | 深股通 |
|
t | 沪股通 |
Session Types (for orders)
| Value | Meaning |
|---|
| INLINECODE10 | Regular hours only |
| INLINECODE11 |
Extended (pre + post market) — use for US stocks |
mktTmType (for real-time quotes)
| Value | Segment | Beijing Time |
|---|
| INLINECODE12 | Regular (盘中) | 22:30–05:00 |
| INLINECODE13 |
Pre-market (盘前) | 17:00–22:30 |
|
-2 | After-hours (盘后) | 05:00–09:00 |
|
-3 | Night session (夜盘) | 09:00–17:00 |
| omit | Default (last close) | — |
⚠️ Always specify mktTmType for real-time prices — omitting it returns the previous close.
Password Encryption
Trading password must be AES-ECB encrypted before login:
CODEBLOCK2
Common Workflows
Stop-Loss / Take-Profit Monitor
CODEBLOCK3
Batch Quotes with Time Segment
CODEBLOCK4
Cancel All Orders
CODEBLOCK5
API Reference
For complete endpoint documentation, parameters, and response schemas:
→ See references/api-reference.md
For the full verified client implementation:
→ See scripts/vbrokers_client.py
VBrokers Trader
通过本地运行的OpenAPI网关,在华盛通(VCL HK)上实现自动化交易。
前置条件
- - OpenAPI网关 必须在本地运行(GUI应用:华盛通OpenAPIGateway.app)
- 网关地址:http://127.0.0.1:11111
- 用于密码加密的 AES密钥:在账户设置时提供(参见 references/api-reference.md)
- Python包:pycryptodome(pip install pycryptodome)
快速开始
将 scripts/vbrokers_client.py 复制到您的项目中并导入:
python
import sys
sys.path.insert(0, /path/to/skill/scripts)
import vbrokers_client as vb
1. 登录(网关重启后必需)
vb.trade_login(您的交易密码)
2. 查询账户
funds = vb.get
accountfunds(P) # P=美股, K=港股
3. 获取实时行情(使用正确的mktTmType对应时段)
quote = vb.get
quotesbatch([AAPL], session=-1) # -1=盘前
4. 下单
result = vb.place_order(AAPL, P, 1, 1, 180.00) # 买入1股限价$180
5. 查询持仓
positions = vb.get_positions(P)
核心概念
请求格式(关键)
所有HTTP请求必须使用嵌套的 params:
json
{timeout_sec: 10, params: {exchangeType: P, ...}}
交易所类型
港股 |
| v | 深股通 |
| t | 沪股通 |
交易时段类型(用于订单)
延长时段(盘前+盘后)— 适用于美股 |
mktTmType(用于实时行情)
盘前 | 17:00–22:30 |
| -2 | 盘后 | 05:00–09:00 |
| -3 | 夜盘 | 09:00–17:00 |
| 省略 | 默认(上次收盘价) | — |
⚠️ 获取实时价格时务必指定 mktTmType — 省略将返回上次收盘价。
密码加密
交易密码在登录前必须进行AES-ECB加密:
python
已在 vbrokersclient.py 中通过 encryptpassword() 处理
密钥:base64编码的24字节AES密钥(账户设置时提供)
常见工作流程
止损/止盈监控
python
result = vb.check
stoploss(AAPL, P, cost_price=150.0,
stop
losspct=0.08, take
profitpct=0.10)
返回:{action: hold/stoploss/takeprofit, currentprice: ..., pnlpct: ...}
if result[action] == stop_loss:
vb.place
order(AAPL, P, 2, qty, 0, entrusttype=5) # 市价卖出
带时段参数的批量行情
python
from datetime import datetime, timezone, timedelta
bj_hour = (datetime.now(tz=timezone.utc) + timedelta(hours=8)).hour
根据北京时间确定正确的mktTmType
session = 1 if (bj
hour >= 22 or bjhour <= 4) else -1 if bj
hour >= 17 else -2 if bjhour <= 8 else -3
quotes = vb.get
quotesbatch([AAPL, TSLA, NVDA], session=session)
取消所有订单
python
vb.cancel
allorders(P) # 取消所有待处理的美股订单
API参考
完整的端点文档、参数和响应模式:
→ 参见 references/api-reference.md
完整的已验证客户端实现:
→ 参见 scripts/vbrokers_client.py