tq-bridge 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 atorber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,382 @@
1
+ Metadata-Version: 2.4
2
+ Name: tq-bridge
3
+ Version: 0.1.0
4
+ Summary: 通达信 tqcenter 跨平台远程调用:Windows 网关 + 远程 SDK
5
+ Author: atorber
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://atorber.github.io/tq-bridge/
8
+ Project-URL: Repository, https://github.com/atorber/tq-bridge
9
+ Project-URL: Issues, https://github.com/atorber/tq-bridge/issues
10
+ Keywords: tqcenter,tdx,tongdaxin,bridge,rpc
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Programming Language :: Python :: 3.9
16
+ Classifier: Programming Language :: Python :: 3.10
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
19
+ Classifier: Topic :: Office/Business :: Financial :: Investment
20
+ Requires-Python: >=3.9
21
+ Description-Content-Type: text/markdown
22
+ License-File: LICENSE
23
+ Requires-Dist: pandas>=1.5.0
24
+ Requires-Dist: websocket-client>=1.6.0
25
+ Requires-Dist: fastapi>=0.110.0
26
+ Requires-Dist: uvicorn[standard]>=0.27.0
27
+ Requires-Dist: pydantic>=2.0.0
28
+ Dynamic: license-file
29
+
30
+ # tq-bridge
31
+
32
+ [![architecture](website/public/architecture.png)](https://atorber.github.io/tq-bridge/guide/architecture)
33
+
34
+ 通达信 `tqcenter` 跨平台远程调用方案:在一台已登录通达信的 Windows 机器上运行网关,其它电脑(Windows / macOS / Linux)用几乎相同的 Python API 取数。
35
+
36
+ 官网:https://atorber.github.io/tq-bridge/
37
+
38
+ ```bash
39
+ pip install tq-bridge
40
+ ```
41
+
42
+ ### Agent Skill(一句话安装)
43
+
44
+ 让各类 Agent(Cursor / Claude Code / Codex 等)直接调用远程 `tqcenter` 全套接口:
45
+
46
+ ```bash
47
+ npx skills add atorber/tq-bridge --skill tqcenter_remote -a '*' -g -y
48
+ ```
49
+
50
+ - `-a '*'`:安装到本机已支持的全部 Agent
51
+ - `-g`:用户级全局(各 Agent 用户目录);去掉则装到当前项目
52
+ - `-y`:跳过确认
53
+
54
+ 安装后还需:本机 `pip install tq-bridge`、配置 `TQ_BRIDGE_URL`(或 `tq_bridge.json`),且 Windows 网关在线。用法见官网 [Agent Skill](https://atorber.github.io/tq-bridge/guide/skill) 或仓库 `skills/tqcenter_remote/SKILL.md`。
55
+
56
+ 发布到 PyPI:在 GitHub 创建 Release(或手动跑 **Publish to PyPI** Action)。首次需在 PyPI 项目启用 Trusted Publisher(GitHub:`atorber/tq-bridge`,workflow:`publish-pypi.yml`,environment:`pypi`)。
57
+
58
+ ## 架构
59
+
60
+ ```
61
+ 业务脚本
62
+ from tqcenter_remote import tq
63
+ │
64
+ │ HTTP /rpc + WebSocket /ws/sub
65
+ ▼
66
+ Windows: tq_bridge (FastAPI)
67
+ │
68
+ ▼
69
+ 本机 tqcenter.tq → TPythClient.dll → 通达信客户端
70
+ ```
71
+
72
+ | 组件 | 说明 |
73
+ |------|------|
74
+ | `tq_bridge/` | Windows 网关:包装真实 `tqcenter`,对外提供 HTTP / WebSocket |
75
+ | `tqcenter_remote/` | 跨平台远程 SDK,API 与官方 `tqcenter` 对齐 |
76
+ | `tqcenter.py` | 官方本机 SDK(仅 Windows + 通达信客户端) |
77
+ | `tqserver.py` | 云端 TdxAiData 封装(与本方案独立) |
78
+ | `tdxdata_test.py` | 官方本机用法示例 |
79
+ | `tdxdata_test_remote.py` | 远程 SDK 冒烟示例 |
80
+
81
+ 业务侧只需改包名:
82
+
83
+ ```python
84
+ # 本机(Windows + 已登录通达信)
85
+ from tqcenter import tq, tqconst
86
+
87
+ # 远程(任意平台,需网关在线)
88
+ from tqcenter_remote import tq, tqconst
89
+ ```
90
+
91
+ ---
92
+
93
+ ## 一、Windows 网关(必须先启动)
94
+
95
+ ### 1. 前置条件
96
+
97
+ 1. 安装 64 位通达信,并**保持客户端登录**。
98
+ 2. 本仓库放在可导入官方 SDK 的位置,例如通达信目录下的 `PYPlugins/user/`,保证能执行:
99
+
100
+ ```python
101
+ from tqcenter import tq
102
+ ```
103
+
104
+ 3. Python 3.9+(与通达信插件环境一致即可),建议 64 位。
105
+
106
+ ### 2. 安装依赖
107
+
108
+ 推荐:
109
+
110
+ ```bat
111
+ pip install tq-bridge
112
+ ```
113
+
114
+ 或从源码安装网关依赖:
115
+
116
+ ```bat
117
+ pip install -r tq_bridge\requirements.txt
118
+ ```
119
+
120
+ ### 3. 配置环境变量
121
+
122
+ | 变量 | 必填 | 默认 | 说明 |
123
+ |------|------|------|------|
124
+ | `TQ_BRIDGE_TOKEN` | 建议 | 空(不鉴权) | Bearer Token,局域网也建议设置 |
125
+ | `TQ_BRIDGE_HOST` | 否 | `0.0.0.0` | 监听地址 |
126
+ | `TQ_BRIDGE_PORT` | 否 | `18787` | 监听端口 |
127
+ | `TQ_BRIDGE_INIT_PATH` | 否 | 网关自身路径 | 传给 `tq.initialize(path)` 的路径 |
128
+ | `TQ_BRIDGE_ALLOW_TRADE` | 否 | `1` | 设为 `0` 禁用下单/撤单 |
129
+
130
+ 示例(cmd):
131
+
132
+ ```bat
133
+ cd /d c:\new_tdx64\PYPlugins\user
134
+ set TQ_BRIDGE_TOKEN=your_secret
135
+ set TQ_BRIDGE_PORT=18787
136
+ set TQ_BRIDGE_INIT_PATH=%CD%\tdxdata_test.py
137
+ set TQ_BRIDGE_ALLOW_TRADE=0
138
+ python -m tq_bridge
139
+ ```
140
+
141
+ PowerShell:
142
+
143
+ ```powershell
144
+ $env:TQ_BRIDGE_TOKEN="your_secret"
145
+ $env:TQ_BRIDGE_PORT="18787"
146
+ $env:TQ_BRIDGE_INIT_PATH="$PWD\tdxdata_test.py"
147
+ $env:TQ_BRIDGE_ALLOW_TRADE="0"
148
+ python -m tq_bridge
149
+ ```
150
+
151
+ 启动成功后终端会打印监听地址;同时放行防火墙入站 TCP(默认 `18787`)。
152
+
153
+ ### 4. 健康检查
154
+
155
+ 浏览器或命令行访问:
156
+
157
+ ```text
158
+ http://<windows-ip>:18787/health
159
+ ```
160
+
161
+ 正常时大致返回:
162
+
163
+ ```json
164
+ {
165
+ "ok": true,
166
+ "initialized": true,
167
+ "run_id": 1,
168
+ "allow_trade": false
169
+ }
170
+ ```
171
+
172
+ `ok=false` 时优先检查:通达信是否登录、`tqcenter` 能否本机初始化、`TQ_BRIDGE_INIT_PATH` 是否有效。
173
+
174
+ ---
175
+
176
+ ## 二、远程客户端(Windows / macOS / Linux)
177
+
178
+ ### 1. 安装依赖
179
+
180
+ ```bash
181
+ pip install tq-bridge
182
+ # 或:pip install -r tqcenter_remote/requirements.txt
183
+ ```
184
+
185
+ 主要依赖:`pandas`、`websocket-client`(行情订阅需要);网关另需 FastAPI / uvicorn。
186
+
187
+ ### 2. 配置网关地址
188
+
189
+ 任选一种方式(优先级:环境变量 > 配置文件)。
190
+
191
+ **方式 A:环境变量**
192
+
193
+ ```bash
194
+ # macOS / Linux
195
+ export TQ_BRIDGE_URL=http://192.168.1.100:18787
196
+ export TQ_BRIDGE_TOKEN=your_secret
197
+ ```
198
+
199
+ ```bat
200
+ REM Windows
201
+ set TQ_BRIDGE_URL=http://192.168.1.100:18787
202
+ set TQ_BRIDGE_TOKEN=your_secret
203
+ ```
204
+
205
+ **方式 B:配置文件 `tq_bridge.json`**
206
+
207
+ 可放在:脚本同目录、当前工作目录、用户目录 `~/.tq_bridge.json`、或 `tqcenter_remote/` 包目录。
208
+
209
+ ```json
210
+ {
211
+ "url": "http://192.168.1.100:18787",
212
+ "token": "your_secret"
213
+ }
214
+ ```
215
+
216
+ 可参考:`tqcenter_remote/tq_bridge.json.example`。
217
+
218
+ ### 3. 最小示例
219
+
220
+ ```python
221
+ from tqcenter_remote import tq, tqconst
222
+
223
+ tq.initialize(__file__) # 签名与官方一致;实际读取网关配置
224
+
225
+ df = tq.get_market_data(
226
+ field_list=[],
227
+ stock_list=["688318.SH", "600519.SH"],
228
+ start_time="20250101",
229
+ end_time="",
230
+ count=5,
231
+ dividend_type="front", # none / front / back
232
+ period="1d",
233
+ fill_data=True,
234
+ )
235
+ print(df)
236
+
237
+ snap = tq.get_market_snapshot(stock_code="688318.SH", field_list=[])
238
+ print(snap)
239
+
240
+ tq.close()
241
+ ```
242
+
243
+ 一键冒烟:
244
+
245
+ ```bash
246
+ python tdxdata_test_remote.py
247
+ ```
248
+
249
+ ---
250
+
251
+ ## 三、常用接口示例
252
+
253
+ 下列写法与官方 `tqcenter` / `tdxdata_test.py` 保持一致,远程侧只改 import。
254
+
255
+ ### K 线
256
+
257
+ ```python
258
+ df = tq.get_market_data(
259
+ stock_list=["688318.SH"],
260
+ period="1d", # 1m / 5m / 15m / 30m / 1h / 1d / 1w ...
261
+ count=100,
262
+ dividend_type="front",
263
+ )
264
+ ```
265
+
266
+ ### 快照 / 基础信息
267
+
268
+ ```python
269
+ tq.refresh_cache(force=True)
270
+ snap = tq.get_market_snapshot(stock_code="688318.SH")
271
+ info = tq.get_stock_info(stock_code="688318.SH")
272
+ ```
273
+
274
+ ### 财务 / 板块
275
+
276
+ ```python
277
+ fd = tq.get_financial_data(
278
+ stock_list=["688318.SH"],
279
+ start_time="20230101",
280
+ end_time="",
281
+ report_type="report_time",
282
+ )
283
+
284
+ sectors = tq.get_sector_list(list_type=1)
285
+ stocks = tq.get_stock_list_in_sector("880201.SH")
286
+ ```
287
+
288
+ ### 订阅行情(WebSocket)
289
+
290
+ ```python
291
+ def on_hq(data_str: str):
292
+ print("hq:", data_str[:200])
293
+
294
+ tq.subscribe_hq(stock_list=["688318.SH"], callback=on_hq)
295
+ print(tq.get_subscribe_hq_stock_list())
296
+
297
+ # ...
298
+ tq.unsubscribe_hq(stock_list=["688318.SH"])
299
+ ```
300
+
301
+ > 订阅依赖客户端已安装 `websocket-client`,且网关 WebSocket `/ws/sub` 可达。
302
+
303
+ ### 文件类接口
304
+
305
+ - `tq.send_file(path)`:远程会把本地文件上传到 Windows 临时目录,再交给通达信。
306
+ - `tq.print_to_tdx(...)`:结果写在 **Windows 通达信目录**,不是客户端本机目录。
307
+ - `tq.download_file(...)`:走网关转发官方下载逻辑。
308
+
309
+ ### 交易类(默认可用,建议关闭)
310
+
311
+ ```python
312
+ # 网关侧建议:set TQ_BRIDGE_ALLOW_TRADE=0
313
+ account = tq.stock_account(account="", account_type="STOCK")
314
+ # tq.order_stock(...); tq.cancel_order_stock(...)
315
+ ```
316
+
317
+ 常量与官方一致,例如:
318
+
319
+ ```python
320
+ from tqcenter_remote import tqconst
321
+ tqconst.STOCK_BUY, tqconst.STOCK_SELL, tqconst.PRICE_SJ
322
+ ```
323
+
324
+ ---
325
+
326
+ ## 四、两种模式怎么选
327
+
328
+ | 场景 | 用法 |
329
+ |------|------|
330
+ | 就在通达信那台 Windows 上跑策略 | `from tqcenter import tq`,参考 `tdxdata_test.py` |
331
+ | 在 Mac / Linux / 其它 Windows 上开发 | `from tqcenter_remote import tq` + 常开网关 |
332
+ | 只要云端 AI 数据、不依赖本机通达信 | 使用 `tqserver.py` / `tqs`(另一条链路) |
333
+
334
+ ---
335
+
336
+ ## 五、常见问题
337
+
338
+ 1. **`网关未就绪` / `health.ok=false`**
339
+ 通达信未登录、DLL 加载失败、或 `TQ_BRIDGE_INIT_PATH` 无效。先在网关机器上直接 `python -c "from tqcenter import tq; tq.initialize(r'你的路径')"` 验证。
340
+
341
+ 2. **客户端连不上**
342
+ 检查 `TQ_BRIDGE_URL`、防火墙端口、Token 是否一致;可用 `curl http://ip:18787/health` 测连通。
343
+
344
+ 3. **`subscribe_hq` 无回调**
345
+ 确认已安装 `websocket-client`,且初始化后 WebSocket 已连上(客户端日志不应持续报 WS 错误)。
346
+
347
+ 4. **`print_to_tdx` 找不到文件**
348
+ 文件落在 Windows 通达信目录,请到网关那台机器查看。
349
+
350
+ 5. **代码格式**
351
+ 证券代码需带市场后缀,如 `600519.SH`、`000001.SZ`、`688318.SH`。
352
+
353
+ ---
354
+
355
+ ## 六、目录结构
356
+
357
+ ```text
358
+ .
359
+ ├── README.md
360
+ ├── LICENSE
361
+ ├── tq_bridge/ # Windows 网关
362
+ │ ├── server.py
363
+ │ ├── codec.py
364
+ │ └── requirements.txt
365
+ ├── tqcenter_remote/ # 跨平台远程 SDK
366
+ │ ├── client.py
367
+ │ ├── tqconst.py
368
+ │ ├── tq_bridge.json.example
369
+ │ └── requirements.txt
370
+ ├── tqcenter.py # 官方本机 SDK(参考)
371
+ ├── tqserver.py # 云端数据封装(独立)
372
+ ├── tdxdata_test.py # 本机示例
373
+ ├── tdxdata_test_remote.py # 远程冒烟
374
+ ├── skills/tqcenter_remote/ # Agent Skill(全 Agent 可用)
375
+ └── website/ # VitePress 官网(首页 + docs)
376
+ ```
377
+
378
+ 更细的网关说明也可看 [tq_bridge/README.md](tq_bridge/README.md)。
379
+
380
+ ## License
381
+
382
+ MIT(官方 `tqcenter.py` / DLL 仍遵循通达信相关许可与使用条款)