pytdx数据获取:在线获取和离线获取(8年前的东西,还能用吗?)

文章目录


前言

pytdx是一个神奇的python库。我们可以在这个地方看到它的github:
https://github.com/rainx/pytdx

不过点进去是一个空的项目。

只有一个5年前的readme,别的空无一物。

然后作者说在github上archive这个项目,咱也不知道啥意思。~

另一个github地址

https://github.com/leegb/pytdx

8年前的玩意儿,还能用吗?

说实话,第一次看到这玩意儿,我也是这么想的。

配置文件

复制代码
[tdx]
local_path = "E:\\new_tdx\\"
api_host = "124.71.163.106"
api_port = 7709
[workspace]
path = "C:\\mcp-workspace\\pytdx"

离线获取数据

离线获取数据,需要安装客户端。

然后就可以:

复制代码
import os
import toml
from pytdx.reader import TdxLCMinBarReader

config_path = os.path.join(os.path.dirname(__file__), '../config.toml')
with open(config_path, 'r') as f:
    config = toml.load(f)

tdx_install_path = config['tdx']['local_path']

# 构建数据目录路径(示例股票代码:sh510300)
stock_code = "sh510300"
data_dir = os.path.join(tdx_install_path, "vipdoc", "sh", "minline")
data_file = os.path.join(data_dir, f"{stock_code}.lc1")

# 验证路径有效性
if not os.path.exists(data_file):
    raise FileNotFoundError(f"数据文件 {data_file} 不存在")

# 使用pytdx读取分钟线数据
reader = TdxLCMinBarReader()
df = reader.get_df(data_file)
print(df.tail())

成功。(.lc1是1分钟)

在线数据获取

复制代码
from pytdx.hq import TdxHq_API
import os
import toml

config_path = os.path.join(os.path.dirname(__file__), '../config.toml')
with open(config_path, 'r') as f:
    config = toml.load(f)

api = TdxHq_API()
api.connect(config['tdx']['api_host'], config['tdx']['api_port'])
data = api.get_security_bars(7, 60, '510300', 0, 100)
df = api.to_df(data)
print(df)
data = api.get_security_bars(7, 60, '510300', 100, 100)
df = api.to_df(data)
print(df)
api.disconnect()
# with api.connect(config['tdx']['api_host'], config['tdx']['api_port']):
#     # 参数说明:周期类型(7=分钟线),分钟数(60),股票代码,起始位置,数量
#     data = api.get_security_bars(7, 60, '000001', 0, 100)
#     df = api.to_df(data)

# print(df)

也是可以的~

相关推荐
大神君Bob2 小时前
【AI办公自动化】教你使用Pytho让Word文档处理自动化
python
轻竹办公PPT3 小时前
2025实测!AI生成PPT工具全总结
人工智能·python·powerpoint
彼岸花开了吗3 小时前
构建AI智能体:八十一、SVD模型压缩的艺术:如何科学选择K值实现最佳性能
人工智能·python·llm
dagouaofei3 小时前
2026 年工作计划 PPT 制作方式对比:AI 与传统方法差异
人工智能·python·powerpoint
虚拟搬运工3 小时前
xformers造成comfyu启动失败
python·comfyui
Hello.Reader3 小时前
PyFlink DataStream Operators 算子分类、函数写法、类型系统、链路优化(Chaining)与工程化踩坑
前端·python·算法
Learner3 小时前
Python函数
开发语言·python
万行3 小时前
机器学习&第五章生成式生成器
人工智能·python·算法·机器学习
_李小白3 小时前
【Android FrameWork】延伸阅读:AMS 的 handleApplicationCrash
android·开发语言·python
万行4 小时前
机器学习&第一章
人工智能·python·机器学习·flask·计算机组成原理