本次修正重點(Rev 1 → Rev 2)
- 針對
seq=64272 說明的 Android 舊路(自訂技術指標輸出對上 N 支時間+OHLCV+成交額),拆解桌面版如何無縫延續同一套自訂指標並擺脫截圖 OCR 誤差。
- 補入 免 OpenD、直接攞取 K 線 Data Feed 的三條桌面版原生/Guest 旁路(Local Cache、CEF CDP WebSocket、Guest
SSL_read+Protobuf 解包),免除左上角指標字數截斷與 N 支上限。
③ 桌面版配合「自訂指標輸出對上 N 支 K 線」舊做法(回應 seq 64272)
| 環節 | 做法與注意事項 | 相比 Android 爬蟲的優勢 |
| 1. 沿用自訂技術指標 |
原有富途自訂指標公式(輸出對上 5 / 10 支 K 棒之 時間, O, H, L, C, Vol, 成交額)可直接於桌面版圖表套用。桌面版左上角指標文字欄有單行截斷上限,建議改用精簡格式(例 B0:T,O,H,L,C,V,A)或拆成 2–3 條輸出變數,避免第 5–10 支被 ... 截走。 |
無須重寫指標選棒與時間對齊邏輯,舊爬蟲解析器幾乎可直接沿用。 |
| 2. 文字讀取層(三級由優到簡) |
① AT-SPI2 無障礙樹:開 QT_ACCESSIBILITY=1(Qt5)或 --force-renderer-accessibility(CEF),以 pyatspi 直接讀文字節點。
② Guest LD_PRELOAD 攔繪字(最精準):寫輕量 x86_64 .so hook QPainter::drawText/FreeType/XDrawString,依前綴濾出指標文字直寫 /dev/shm JSONL。
③ 固定小框 ROI OCR(保底):只截左上角指標矩形(XGetImage 進 /dev/shm)+ 2–3× 放大二值化 + tesseract 數字白名單。
|
①② 完全免 OCR 誤判(零 8/B、小數點錯位)且不搶佔 FEX CPU;③ 亦只掃極小區域(<15 ms),遠快於全螢幕抓圖。 |
④ 更好、直接攞到 K 線 Raw Data(免指標字數截斷、免 OpenD)
| 優先序 | 直接 Data Feed 路徑 | 適用架構(承 A25 63935) | 優點與實作要點 |
| A(最推薦) |
Guest LD_PRELOAD 攔 SSL_read+Protobuf 解 K 線 Push |
libMainApp.so(原生 Qt5 行情連線) |
富途桌面版行情走加密 TCP/Protobuf Push。喺 FEX guest 層 hook OpenSSL/BoringSSL SSL_read 取得明文封包,解出 K 線 Cmd 即可直接吐出任意 N 支完整 ts, O, H, L, C, V, Turnover——完全不受視窗遮擋、指標截斷或只拿 5–10 支的限制。 |
| B(若屬網頁圖表) |
CEF 本機 CDP 攔截 WebSocket/Chart Store |
FTWebRender(libcef.so) |
啟動加 --remote-debugging-port=9222 --remote-debugging-address=127.0.0.1,監聽 Network.webSocketFrameReceived 或直接讀前端 K 線 Array,一次取全量歷史與實時增量。 |
| C(零注入旁路) |
本機磁碟快取/SQLite 讀取 |
~/.futu 用戶資料目錄 |
檢查本地已落盤之歷史 K 線檔(先確認最新未收盤 bar 是否僅存記憶體未即時 flush)。 |
建議雙軌落地次序(今日即可切換)
1. 短期保底(當日上線):保留自訂指標輸出對上 5–10 支(注意精簡欄位免截斷),先查 AT-SPI2 / QPainter::drawText LD_PRELOAD(或固定左上角小框 ROI OCR)直接接回現有爬蟲格式。
2. 並行升級(徹底脫離 UI):花 5 分鐘分別探測 FTWebRender CDP 與 Guest SSL_read 明文結構;一旦解出 K 線 Protobuf/WS 欄位,即切換為純 Data Feed 直吐,一次拿對上 50–200 支且零 UI 渲染負載。
Rev 2 補充紀錄摘要(Markdown)
# Turn Log 64272 — Desktop Compatibility for Custom-Indicator N-Bar DUMP & Direct Data Feed Routes (A38)
- Target Seq: `64272` (`sender=dj_host1`, `sha12=497fde0c42f7`)
- Context:
- Prior Android crawler read a custom technical indicator on the K-line chart displaying the past N bars (e.g., 5 or 10 bars: timestamp, Open, High, Low, Close, Volume, Turnover).
- Desktop architecture (`A25` `seq=63935`): `FTNN` launcher -> `libMainApp.so` (Qt5) + `FTWebRender` (`libcef.so` + `libX11` + `libQt5Core`).
- `seq=64265`: OpenD rejected (zero-cost desktop route only). `seq=64269`: FEX confirmed working for main UI + live stream.
## Q1: Adapting the Android Custom-Indicator N-Bar DUMP Workflow to Desktop (FEX + Linux X11)
1. **Reuse the Exact Same Futu Custom Indicator Script**:
- The Futu custom indicator formula outputting the last `N` bars (`T,O,H,L,C,V,Amt`) carries over directly to the desktop chart.
- Because desktop chart top-left overlay lines have character truncation limits, format compactly (e.g., `B0:T,O,H,L,C,V,A;B1:...`) or split across 2–3 indicator output variables so 5–10 bars do not get ellipsized (`...`).
2. **Replace Mobile Screenshot/Accessibility Scraping with Cleaner Desktop Extraction**:
- **Check 1 (Qt Accessibility / AT-SPI2)**: Test `QT_ACCESSIBILITY=1` + `pyatspi` (or `--force-renderer-accessibility` for CEF). If the top-left indicator label exposes an accessible node, read text directly with zero OCR and negligible FEX overhead.
- **Check 2 (Guest `LD_PRELOAD` Text-Draw Interception — Best Exact Drop-in)**: Load a tiny x86_64 `.so` via `LD_PRELOAD` inside the FEX guest to hook `QPainter::drawText` (Qt5 `libMainApp.so`) or FreeType/HarfBuzz/XDrawString, filter by indicator prefix/coordinates, and stream structured JSON lines to `/dev/shm/futu_ind.jsonl` or a local FIFO. Zero OCR errors, immune to overlap/DPI scaling.
- **Check 3 (Targeted Top-Left ROI OCR Fallback)**: Only grab the small top-left rectangle (`XGetImage` / MIT-SHM into `/dev/shm`), upscale 2–3x + binarize, and run `tesseract` with digit/delimiter whitelist (`< 15 ms` per poll, minimal impact on FEX).
## Q2: Better / Direct Data-Feed Methods (Zero-Cost, No OpenD, No Indicator String Truncation)
Ranked by intrusiveness & reliability:
1. **Local Cache / Memory-Mapped Files Inspection (Lowest Overhead)**:
- Inspect FTNN's user data directory (`~/.futu` / local SQLite / binary cache files) for persisted K-line chunks; note live forming bars may remain in RAM until bar close/flush.
2. **CEF Remote Debugging / WebSocket Frame Interception (If Chart/Quotes Use `FTWebRender`)**:
- Launch with `--remote-debugging-port=9222 --remote-debugging-address=127.0.0.1` and subscribe to `Network.webSocketFrameReceived` or query the chart JS store via CDP. Directly receives full bar arrays (`N` bars at once, full precision) without UI layout limits.
3. **Guest `LD_PRELOAD` TLS Plaintext (`SSL_read` / `QSslSocket`) + Protobuf Decode (Most Complete Direct Feed)**:
- Futu desktop receives encrypted TCP/Protobuf market pushes. Hooking OpenSSL/BoringSSL `SSL_read` (or `SSL_write` for requests) inside the guest yields raw unencrypted Futu protocol frames on `/dev/shm` or a local socket; decoding the K-line push `Cmd` gives full history/live bars directly—independent of chart zoom, indicator truncation, or UI rendering stalls.
## Recommended Rollout Order
- **Immediate (Day-1 parity)**: Keep the custom N-bar indicator -> probe AT-SPI2 -> fall back to `QPainter::drawText` `LD_PRELOAD` (or small top-left ROI OCR) so the existing crawler pipeline switches over immediately.
- **Parallel Upgrade**: Run a 5-minute probe on `FTWebRender` CDP and `SSL_read` plaintext dumps; once the K-line Protobuf/WS payload is verified, switch the crawler to direct feed consumption and eliminate UI scraping completely.
以下為 第 1 版(Rev 1,seq=64269)完整原文,依修正保留規範完整留存供對照:
① 現階段 FEX 效能優化方案(針對 ARM64 跑 FTNN/FTWebRender)
| 優先序 | 優化層面 | 具體做法 | 預期作用與驗證重點 |
| 1 |
TSO 記憶體屏障減稅(最大槓桿) |
於 FEXConfig / 環境變數啟用 FEX_HalfBarrierTSOEnabled=1、FEX_VectorTSOEnabled=0、FEX_MemcpySetTSOEnabled=0;視穩定度再實測 FEX_TSOEnabled=0(先以只讀圖表實測無崩潰/無數據錯位)。 |
ARM64 無硬體 x86 TSO,CEF/Qt 多線程大量屏障係主要轉譯稅;放寬 TSO 通常可降顯著 CPU 開銷。 |
| 2 |
FEX Thunking 直通原生庫 |
開啟 Host/Guest Thunking(libX11、libxcb、libGL/libEGL、libasound),將 X11/繪圖調用轉交原生 aarch64 庫執行。 |
免去圖形與 X11 協議封裝的 x86 指令轉譯(與 64264 方向一致,屬可實測項)。 |
| 3 |
大核綁定 + JIT 快取 + UI 減繪 |
以 taskset 綁定 ARM64 大核;確保 /dev/shm 容量充足免 Qt/CEF 退回磁碟 IPC;只保留目標期貨圖表分頁、隱藏其他報價板與關掉無謂動畫;使用輕量 X11/Xvfb 免去合成器(compositor)。 |
減少跨核遷移與重繪像素面積,避免開市幀率搶佔數據處理。 |
② 納指期貨圖表左上角技術指標 DUMP 方案(由準確到最簡)
| 方案 | 適用前提(先花 1 分鐘探測) | 實作路徑 | 優缺點 |
| A. CEF CDP 遠端除錯(最乾淨、零 OCR 誤差) |
指標區或圖表由 FTWebRender(CEF)渲染(A25 63935 已核實有 libcef.so)。 |
啟動加 --remote-debugging-port=9222 --remote-debugging-address=127.0.0.1(只限本機),連 WebSocket CDP 讀 DOM/JS 變數或攔截 Network.webSocketFrameReceived。 |
直接拿到精準浮點數與 raw data feed;若指標畫在 <canvas> 上但有內部 JS 狀態亦可直接讀取。 |
B. Guest LD_PRELOAD 文字繪製攔截(無誤差通殺) |
圖表屬原生 Qt5(libMainApp.so)直接繪製,或 CEF Canvas 不露 DOM。 |
寫輕量 x86 .so(於 FEX guest 內 LD_PRELOAD),攔截 QPainter::drawText/XDrawString/FreeType・HarfBuzz glyph 呼叫,按左上角座標濾出指標字串。 |
100% 精準原文與數值,唔使逆向 Futu 加密 TCP 協議,亦無 OCR 誤讀(如 8/B、小數點)。 |
C. AT-SPI2 讀節點 → 定點 ROI OCR(最易落地兜底) |
唔想改啟動參數或注入 LD_PRELOAD。 |
① 先試 QT_ACCESSIBILITY=1+pyatspi 讀介面文字;② 若屬 Canvas/點陣自繪,僅對 X11 視窗左上角固定小框(例 400×80 px)用 XGetImage/mss 抓入 /dev/shm,放大 2–3× 二值化後跑 tesseract --psm 6(限定數字+符號白名單)。 |
全圖截圖慢,但僅截左上角 400×80 px 喺 ARM64 單次 OCR < 15 ms,可立即像 Android 爬蟲般讀出數值。 |
誠實邊界與建議驗證次序(1 分鐘判別)
1. 檢查圖表視窗係 FTWebRender (CEF) 定 libMainApp.so (Qt Widget):xwininfo -tree / ps aux | grep FT
2. 若見 FTWebRender:先試 --remote-debugging-port=9222(本機回環)睇有冇 WS/DOM 數據流(拿正 data feed)。
3. 若屬原生自繪:優先對左上角固定框做 ROI 截圖 OCR 立即先跑通 DUMP,再按需要補 guest LD_PRELOAD drawText 消除 OCR 誤差。
原始紀錄摘要(Markdown Turn Log)
# Turn Log 64269 — FEX Performance Optimization & NASDAQ Futures Chart Indicator DUMP (A38)
- Target Seq: 64269 (sender=dj_host1, sha12=831015175a7b)
- Context verified from inbox:
- seq=64265 (dj_host1): OpenD rejected (cost/limits); zero-cost data route only.
- seq=64269 (dj_host1): Box64 dropped (crashes at main UI); FEX succeeds (main UI + live data stream).
- seq=63935 (A25): FTNN launcher uses libc/libstdc++; libMainApp.so (Qt5) + FTWebRender (CEF/libcef.so + Qt5Core + libX11).
## Q1: FEX Performance Optimization on ARM64 (Beyond dropping Box64)
1. **TSO Barrier Reduction (Largest FEX multi-thread lever)**:
- ARM64 lacks native x86 TSO; FEX inserts DMB barriers around memory accesses. For CEF/Qt heavy multi-threading, test `FEX_HalfBarrierTSOEnabled=1` (safer first step) and `FEX_VectorTSOEnabled=0` / `FEX_MemcpySetTSOEnabled=0`, or per-app testing with `FEX_TSOEnabled=0` (fastest, ~15-30%+ uplift in memory-heavy threads, must verify no crash/corruption in Futu).
2. **FEX Library Thunking (Native ARM64 graphics/X11)**:
- Enable FEX host thunking for `libX11`, `libxcb`, `libGL`/`libEGL`, `libasound` (`FEXConfig` / `ThunkHostLibs` + `ThunkGuestLibs`) so X11/OpenGL/compositing execute as native aarch64 rather than emulated x86.
3. **Code Cache + Core Pinning + Lightweight Display**:
- Enable FEX JIT/AOT object-code cache if available in the installed FEX build; pin `FTNN`/`FTWebRender` to big/performance Cortex cores (`taskset`) and keep `/dev/shm` adequately sized for CEF/Qt IPC.
- Minimize UI redraw scope: keep only the target NASDAQ Futures chart window/tab visible, hide unused market tickers/news panels, run bare Xvfb/lightweight WM without compositor.
## Q2: DUMPing Top-Left Chart Technical Indicator Values (or Data Feed)
Ranked from quickest probe to cleanest exact extraction (all zero-cost, local):
1. **Probe 1 — CEF Remote Debugging / CDP (Exact Data Feed / DOM if chart is in FTWebRender)**:
- If the chart panel is rendered by `FTWebRender` (CEF), launch with `--remote-debugging-port=9222 --remote-debugging-address=127.0.0.1`. Query `http://127.0.0.1:9222/json` and attach via WebSocket CDP (`Runtime.evaluate` or `Network.webSocketFrameReceived`) to extract exact indicator values or raw chart feed JSON/WS frames with zero OCR error.
2. **Probe 2 — Guest `LD_PRELOAD` Text-Draw Hook (Exact String Capture if native Qt/Canvas)**:
- If the chart is native Qt5 (`libMainApp.so`) or CEF `<canvas>` without DOM nodes, an x86 `LD_PRELOAD` `.so` inside the FEX guest hooking `QPainter::drawText` (or `HarfBuzz`/`FreeType`/`XDrawString`/`pango`) intercepts the exact rendered indicator strings and `(x, y)` coordinates before rasterization—100% exact digits, no OCR, no Futu protocol decryption needed.
3. **Probe 3 — Accessibility Tree (`AT-SPI2`) vs Targeted ROI OCR (Simplest Non-Invasive Fallback)**:
- First check `QT_ACCESSIBILITY=1` + `pyatspi`/`accerciser` (or `--force-renderer-accessibility` for CEF); if exposed as accessible text nodes, read directly.
- If painted directly onto bitmap/canvas (invisible to AT-SPI2), crop ONLY the fixed top-left indicator ROI (e.g., ~400x80 px) from X11 (`XGetImage` / `mss` / `ffmpeg x11grab` into `/dev/shm` tmpfs), upscale 2-3x + grayscale threshold, and run `tesseract` (`--psm 6/7`) or `PaddleOCR` on that tiny ROI (<15 ms/frame on ARM64).
- Note: Direct Futu TCP/TLS feed reverse-engineering is higher-effort due to Futu's proprietary binary/encrypted framing compared to CDP or `drawText` hooking.