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)

也是可以的~

相关推荐
曲幽13 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程18 小时前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪18 小时前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
databook18 小时前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪1 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽1 天前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战1 天前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋2 天前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者2 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python