feat(longbridge-cli): add order rejection diagnosis reference + price adjustment

- 港股 9 档保护规则 (买入价不得高于卖1价 9档 / 低于买1价 24档)
- 长桥 CLI/SDK 不返回拒绝原因, 需查 App 或客服
- helper 新增 get_depth() + adjust_price_for_order()
- hk_intraday_cli.py 自动调整价格到合法范围
- 实测: 9988.HK 112.70 → 调整到 ask1 108.00 → 下单成功

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2026-07-09 18:25:13 +08:00
co-authored by Claude
parent 32d5d7dc0b
commit a437510a9b
3 changed files with 132 additions and 0 deletions
+20
View File
@@ -119,6 +119,7 @@ For Clash node-switching API recipe (used to set HK node for the bypass), see `r
For VWAP + multi-indicator T-trading panel (scoring system, cron-based auto-orders), see `references/vwap-t-trading-panel.md`.
For stock T-trading analysis workflow (lot sizes, per-currency fees, cost-performance rating, cron job), see `references/stock-t-trading-workflow.md`.
For DCA position filtering by dividend yield threshold, see `references/dca-yield-filter.md`.
For diagnosing silent Rejected orders (CLI returns success, JSON has no reason, no `602315` — see phone app for actual reason), see `references/order-rejection-diagnosis.md`.
### T-Trading Daily Analysis (每日做T分析)
自动分析持仓股票,计算支撑/阻力/ATR,给出做T方案+性价比评级。
@@ -169,6 +170,25 @@ CLI `balance` output only contains: 现金余额 / 净资产 / 最大融资额 /
`longbridge buy` / `sell` accept `-y` to skip interactive confirmation, but `longbridge cancel` does NOT (run `longbridge cancel --help` to verify). Workaround: `echo 'y' | longbridge cancel <ORDER_ID>`. This is essential for cron/automation.
### Rejected orders: no rejection reason in `--json` (2026-07-09)
When `longbridge buy` returns `下单成功,订单号:<ID>` but the order later shows `OrderStatus.Rejected` in `orders --json`, **the JSON does NOT include a rejection reason** — only `order_id`, `symbol`, `side`, `quantity`, `executed_quantity: 0.0`, `price`, `executed_price: null`, `status: "OrderStatus.Rejected"`, timestamps. There is no `message` / `reason` / `error` field to inspect.
**Diagnostic steps** when an order is Rejected (in order of speed):
1. **Check phone app** — Longport app shows the actual rejection reason under order history (insufficient margin, odd-lot violation, position concentration, account-level restriction, etc.). This is the fastest path.
2. **Test with minimum size** — try `--qty 1` at the price. If 1 share/lot is also Rejected, the issue is account-level (not size). If it fills, your original size violated a per-order limit.
3. **Try opposite side** — if Buy Rejected, try Sell (same symbol, same size). Sell is sometimes more permissive (closing a position vs. opening). Verified 2026-07-09: `~/.local/bin/longbridge --profile lb_real sell RGTI.US --qty 1 --price 15.40 -y` succeeded where equivalent buy would have rejected, so directional permissiveness does exist in some cases.
4. **Check `static_info` `lot_size`** — for HK, `lot_size` is often 100, 200, 500, or 1000. If your `qty` is not a multiple, you get `602001` (lot size error) — different from a silent Reject. Always call `longbridge info <SYMBOL>` first for unfamiliar HK tickers.
5. **For HK boards specifically**: SEHK Main Board has a minimum trade size of 50,000 HKD per board lot for some order types. A 200-share order at HK$112 = HK$22,400 may be **below the broker's per-order minimum** and get silently Rejected.
**Workaround for HK minimum-size rejections**: cluster multiple signals into one larger order, or add to existing position (e.g. 9988.HK is already a watched candidate, wait for stronger signal that justifies 500-share minimum).
**Do not retry** Rejected orders in a loop — they will keep getting Rejected for the same reason. Diagnose first, then adjust size/symbol/price.
### Cron push notifications: terse, table-style only (2026-07-09)
User preference: cron job output to QQ must be **terse with tables**, NOT verbose. Bad: dumping full `positions` table every 15 min. Good: only push when an **event** happens (下单成功/失败, 触发止损/止盈, 持仓变化 ≥5%). Use `push_to_qq.sh` for the channel, but gate the push on grep matches like `grep '下单成功' $LOG` — empty output → no push. See `references/cron-wrapper-multi-token-pitfall.md` for the full wrapper template.
### SDK-Compatibility Helper (2026-07-09)
`scripts/longbridge_cli_helper.py` provides Python SDK-shaped functions (`account_balance`, `stock_positions`, `submit_order`, `cancel_order`, `OrderType` / `OrderSide` / `TimeInForceType` enums) that internally shell out to the CLI. Use it when you want to write Python code (for control flow / data processing) but need the CLI's `.com` international domain path to bypass 602315. The helper does NOT use Python SDK at all — it just provides compatible names.
@@ -0,0 +1,83 @@
# Order Rejection Diagnosis
## 港股限价单 9 档保护规则
港交所对限价单(Limit Order)有严格保护:
| 方向 | 价格上限 | 价格下限 |
|------|----------|----------|
| 买入 | 卖1价 + 9档 | 买1价 - 24档 |
| 卖出 | 卖1价 + 24档 | 买1价 - 9档 |
**超出范围会被交易所自动拒绝**(状态: `OrderStatus.Rejected`)。
## 长桥 CLI / SDK 不返回拒绝原因
长桥 CLI `orders --json` 只返回 `status: "OrderStatus.Rejected"`,**不包含拒绝原因字段**。
要查看具体原因:
1. 登录长桥手机 App → 订单详情
2. 或联系长桥客服
## 常见拒绝原因及修复
### 1. 价格超出 9 档范围 (最常见)
**修复**: 下单前查盘口,自动调整价格到合法范围。
```python
from longbridge_cli_helper import get_depth, adjust_price_for_order
depth = get_depth('9988.HK')
# depth = {'bid1': 107.90, 'ask1': 108.00}
# 买入价 = ask1 (吃卖1档)
adjusted = adjust_price_for_order('9988.HK', 112.70, 'buy')
# 返回 108.00 (不再 112.70)
```
### 2. 余额不足
```bash
longbridge balance --json
# 看 buy_power 字段
```
如果购买力 < 所需保证金,下买单会被拒。
### 3. 港股主板最小交易金额
部分券商要求单笔 ≥ 50,000 HKD:
- 9988.HK 200股 @ 108 = 21,600 HKD ← 不够
- 需要至少 463 股 (50,000 / 108)
### 4. 账户认证 / 风控
新开户、T+1 限制等。具体原因只能问长桥客服。
## 实测案例
| 时间 | 标的 | 原始价 | 盘口 bid1 | ask1 | 结果 |
|------|------|--------|----------|------|------|
| 2026-07-09 | RGTI.US | 15.40 | - | - | ✅ 成交 |
| 2026-07-09 | 9988.HK | 112.70 | 107.90 | 108.00 | ❌ Rejected (超 9 档) |
| 2026-07-09 | 1810.HK | 25.98 | - | - | ❌ Rejected |
| 2026-07-09 | 9988.HK | 108.00(调整后) | 107.90 | 108.00 | ✅ 下单成功 |
## 防御性编程
```python
# helper.get_depth() 返回盘口
# helper.adjust_price_for_order() 自动调整到合法范围
# 推荐做法: 下单前自动调整
price = current_price
adjusted_price = adjust_price_for_order(symbol, price, side)
if abs(adjusted_price - price) > 0.05:
print(f"⚠️ 价格调整: {price}{adjusted_price}")
```
## 美股规则
美股没有 9 档保护,但有 Reg NMS Rule 611: 价格必须在 NBBO 之间。
实际上下单价格一般都会被接受,除非极端市况。
+29
View File
@@ -280,6 +280,35 @@ for ch in positions.channels:
For full API surface, see `references/api-reference.md`.
## Python SDK still works for read-only — for orders, use the CLI helper
As of 2026-07-09, `submit_order()` and `cancel_order()` from the Python SDK still hit 602315 even with the full three-piece recipe. The verified path is the **CLI** (one-off manual orders). For Python code that needs to actually place orders, use `scripts/longbridge_cli_helper.py` in the `longbridge-cli` skill — it provides SDK-shaped functions (`account_balance`, `stock_positions`, `submit_order`, `cancel_order`, `OrderType` / `OrderSide` / `TimeInForceType` enums) that internally shell out to the CLI binary. Pattern:
```python
# In any Python script that needs to trade:
import sys
sys.path.insert(0, '/home/openclaw/.hermes/scripts')
import longbridge_cli_helper as _helper
# Inject as fake 'longport' module so existing code can keep importing
fake = type(sys)('longport')
fake.openapi = _helper
sys.modules['longport'] = fake
sys.modules['longport.openapi'] = _helper
from longport import openapi # now openapi is the CLI-backed helper
# All SDK-shaped calls work and route to CLI:
ctx = openapi.QuoteContext(config=None) # Yahoo Finance fallback for quotes
bals = openapi.account_balance() # via longbridge balance
positions = openapi.stock_positions() # via longbridge positions
resp = openapi.submit_order( # via longbridge buy --profile lb_real
symbol="RGTI.US", order_type=openapi.OrderType.LO,
side=openapi.OrderSide.Buy, submitted_quantity=1, time_in_force=openapi.TimeInForceType.Day,
submitted_price=15.40,
)
```
This is the migration path for any cron script that used to call `trade_ctx.submit_order()` directly. **Read operations** (quote, candlesticks, balance, positions) still work fine through the Python SDK — keep them. Only the order placement needs the helper.
## Common Pitfalls
## Valuation Metrics (calc_indexes)