Skip to content

[Tqsdk-Ci] Update Version 3.8.3 #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2025

Conversation

shinny-hongyan
Copy link
Collaborator

@shinny-hongyan shinny-hongyan commented May 25, 2025

Automated changes by create-pull-request GitHub action

Summary by CodeRabbit

  • 新功能

    • 文档新增了 TqSdk 在 AI 代码编辑器 Cursor 下的详细使用指南。
    • 支持多策略历史结算信息查询。
  • 修复

    • 修复了主连合约查询接口在部分情况下的合约不一致问题。
    • 解决了 pandas 2.3.0 版本下初始化报错的问题。
    • 修复了模拟账户被加入实盘账户列表的问题。
  • 文档

    • 优化并修正了多个文档示例代码和格式。
    • 文档目录新增 tqsdk_cursor 指南,移除 tqsdk_llm。
    • 更新了版本变更日志,补充了新版本功能与修复说明。
    • 修正了 Jupyter Notebook 安装文档链接格式。
  • 其他

    • 版本号升级至 3.8.3。

Copy link

coderabbitai bot commented May 25, 2025

📝 Walkthrough

Walkthrough

本次更新将版本号从3.8.1提升至3.8.3,完善了Sphinx文档配置以支持新的Google Analytics扩展,新增了关于在Cursor AI代码编辑器中使用TqSdk的详细指南,调整了文档标题下划线长度和目录结构,修正了部分文档链接格式,扩展了股指期权查询参数校验范围,优化了WebSocket连接处理函数,改进了Pandas版本判断逻辑,并对示例代码和异常提示进行了细节调整。

Changes

文件/分组 变更摘要
PKG-INFO, setup.py, tqsdk/version.py, doc/conf.py 版本号由3.8.1更新为3.8.3,Sphinx配置增加sphinxcontrib.googleanalytics扩展,移除旧主题选项,新增Google Analytics相关变量。
doc/tqsdk_cursor.rst 新增文档,详细介绍如何在Cursor AI代码编辑器中高效学习和使用TqSdk。
doc/index.rst 目录中添加tqsdk_cursor.rst,移除tqsdk_llm.rst
doc/demo/base.rst, doc/tq_trading_unit.rst 调整文档标题及章节下划线长度,tq_trading_unit.rst新增版本更新内容。
doc/usage/jupyter.rst 修正Jupyter Notebook安装文档中的链接格式和URL。
doc/version.rst 更新3.8.3版本日志,修正文档历史中的引用和描述。
tqsdk/api.py 使用packaging.version模块替代手动版本比较,示例代码补充datetime导入,扩展股指期权查询参数校验范围,优化错误提示及文档说明。
tqsdk/tqwebhelper.py WebSocket连接处理函数末尾新增return ws,完善连接生命周期控制。
doc/advanced/backtest.rst 修改多进程回测示例中账户余额访问方式,改用get_account()方法。
tqsdk/calendar.py 优化连续合约表日期过滤逻辑,避免非交易日数据导致行数不匹配问题。
tqsdk/demo/tutorial/t72.py 简化get_trading_status函数注释,去除专业版许可提示。
tqsdk/lib/target_pos_task.py 异常提示信息更详细,增加示例说明不支持最小开仓量大于1的合约。
tqsdk/tradeable/mixin.py 示例代码中增加TqAccount导入,完善文档示例。

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant TqWebHelper
    participant WebSocket

    Client->>TqWebHelper: 发起WebSocket连接请求
    TqWebHelper->>WebSocket: 建立连接
    TqWebHelper->>Client: 发送初始数据
    loop 消息处理
        Client->>TqWebHelper: 发送消息
        TqWebHelper->>Client: 响应消息
    end
    TqWebHelper-->>Client: 关闭连接并返回ws对象
Loading

Poem

兔儿跳跃新版本,
三八三号闪光现。
文档扩展添新篇,
Cursor助力学无边。
代码润色更细腻,
连接返回更完善。
祝君开发皆顺利,
兔耳轻摇乐满天!

((\
( -.-)
o_(")(")

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • JIRA integration encountered authorization issues. Please disconnect and reconnect the integration in the CodeRabbit UI.
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch create-pull-request/patch-62335b6
  • Post Copyable Unit Tests in Comment

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
tqsdk/api.py (1)

3072-3084: 统一错误提示中的参数名称
当前错误提示使用了 exercise_date 参数,但是该接口实际接收的参数名为 nearbys。建议将提示修改为:

if underlying_symbol in ["SSE.000300", "SSE.000852", "SSE.000016"]:
    if any(i not in [0, 1, 2, 3, 4, 5] for i in nearbys):
        raise Exception(f"股指期权标的为:{underlying_symbol},nearbys 参数应在 [0, 1, 2, 3, 4, 5] 之间。")
else:
    if any(i not in [0, 1, 2, 3] for i in nearbys):
        raise Exception(f"ETF 期权标的为:{underlying_symbol},nearbys 参数应在 [0, 1, 2, 3] 之间。")

这样可保持文档、参数和错误提示的一致性。

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 62335b6 and b5b435f.

📒 Files selected for processing (12)
  • PKG-INFO (1 hunks)
  • doc/conf.py (3 hunks)
  • doc/demo/base.rst (30 hunks)
  • doc/index.rst (1 hunks)
  • doc/tq_trading_unit.rst (4 hunks)
  • doc/tqsdk_cursor.rst (1 hunks)
  • doc/usage/jupyter.rst (1 hunks)
  • doc/version.rst (4 hunks)
  • setup.py (1 hunks)
  • tqsdk/__version__.py (1 hunks)
  • tqsdk/api.py (3 hunks)
  • tqsdk/tqwebhelper.py (1 hunks)
🔇 Additional comments (19)
tqsdk/tqwebhelper.py (1)

290-290: 修复 WebSocket 处理器的返回值

正确添加了 return ws 语句,这符合 aiohttp WebSocket 处理器的标准模式。之前缺少返回值可能会导致框架无法正确处理 WebSocket 响应对象。

PKG-INFO (1)

3-3: 版本号更新正确

版本从 3.8.1 更新到 3.8.2,与项目的版本发布一致。

tqsdk/__version__.py (1)

1-1:

✅ Verification successful

版本字符串更新正确

版本号已正确更新为 '3.8.2',与其他文件中的版本信息保持一致。

运行以下脚本验证版本号在整个项目中的一致性:


🏁 Script executed:

#!/bin/bash
# 描述:检查项目中所有版本号引用是否一致
# 预期:所有版本号都应该是 3.8.2

echo "检查版本号一致性:"
echo "=== __version__.py ==="
cat tqsdk/__version__.py

echo "=== PKG-INFO ==="
grep "^Version:" PKG-INFO

echo "=== setup.py ==="
rg -A 3 -B 3 "version.*=" setup.py

echo "=== 其他可能的版本引用 ==="
rg "3\.8\.[0-9]" --type py --type rst --type md

Length of output: 930


版本号一致性验证通过

项目中所有主要版本引用均已正确更新为 3.8.2:

  • tqsdk/version.py

  • PKG-INFO

  • setup.py

  • doc/conf.py

  • doc/version.rst 中保留了历史版本记录(3.8.2、3.8.1、3.8.0),无需修改。

  • doc/tq_trading_unit.rst 中的最低版本要求 >= 3.8.0 对于 3.8.2 依然适用,无需调整。

✅ 批准合并。

doc/index.rst (1)

24-24:

✅ Verification successful

文档索引更新正确

在目录树中添加了 tqsdk_cursor.rst,这应该是新增的 Cursor AI 代码编辑器使用指南。

验证新添加的文档文件是否存在:


🏁 Script executed:

#!/bin/bash
# 描述:检查新添加的文档文件是否存在
# 预期:tqsdk_cursor.rst 文件应该存在

echo "检查文档文件:"
if [ -f "doc/tqsdk_cursor.rst" ]; then
    echo "✓ doc/tqsdk_cursor.rst 存在"
    echo "文件大小:$(wc -l < doc/tqsdk_cursor.rst)"
else
    echo "✗ doc/tqsdk_cursor.rst 不存在"
fi

echo ""
echo "检查是否有其他相关的 LLM 或 Cursor 文档:"
fd -e rst . doc/ | grep -i -E "(llm|cursor|ai)"

Length of output: 402


文档索引更新已验证

  • 已确认 doc/tqsdk_cursor.rst 文件存在(178 行)
  • 原有的 doc/tqsdk_llm.rst 仍在,新增文档与现有 LLM 文档可并存
    批准合并,无其他问题。
setup.py (1)

11-11: 版本号更新 已将版本号从3.8.1更新至3.8.2,与 __version__.pydoc/conf.py 等保持一致,符合发布流程。

doc/tq_trading_unit.rst (1)

8-8: 统一格式调整:标题下划线 已将各段落标题的下划线长度调整为与标题文本长度一致,保持文档格式统一。

Also applies to: 81-81, 93-93, 106-106, 113-113

doc/usage/jupyter.rst (1)

28-29: 修复链接格式 已移除冗余冒号和空格,修正为正确的 reST 链接语法,确保文档引用链接准确可用。

doc/demo/base.rst (1)

4-4: 统一格式调整:标题下划线 已将演示文档中所有标题的下划线长度统一为与标题文本长度一致,提升可读性和一致性。

Also applies to: 12-12, 21-21, 30-30, 39-39, 48-48, 57-57, 66-66, 74-74, 82-82, 89-89, 96-96, 103-103, 110-110, 117-117, 124-124, 131-131, 138-138, 145-145, 152-152, 159-159, 165-165, 173-173, 180-180, 187-187, 194-194, 201-201, 208-208

tqsdk/api.py (2)

870-871: 在示例中显式导入 datetime 以避免运行错误
添加 from datetime import datetime 导入,使示例能正确识别 datetime 类型,确保示例代码可运行。


942-943: 在示例中显式导入 datetime 以避免运行错误
添加 from datetime import datetime 导入,使示例能正确识别 datetime 类型,确保示例代码可运行。

doc/version.rst (1)

5-10: 版本变更日志条目格式正确

新增的3.8.2版本条目格式规范,内容简洁明了,符合项目的变更日志标准。修复内容和文档改进的描述都很清晰。

doc/conf.py (3)

17-17: 正确添加Google Analytics扩展

在extensions列表中正确添加了sphinxcontrib.googleanalytics扩展,这将为文档站点提供访问分析功能。


51-53: 版本号更新一致

版本号从3.8.1正确更新为3.8.2,与其他文件中的版本更新保持一致。


82-84: Google Analytics配置合理

Google Analytics的配置设置合理:

  • 使用了GA4格式的跟踪ID (G-9SPL7ERCMF)
  • 默认启用分析功能
  • 配置简洁明了
doc/tqsdk_cursor.rst (5)

1-36: 优秀的文档概述和介绍部分

文档开头部分内容全面且结构清晰:

  • 明确说明了文档目的和适用对象
  • 对Cursor的介绍准确且有针对性
  • 详细列出了在TqSdk开发中使用Cursor的具体好处
  • 内容组织逻辑性强,便于用户理解

37-77: 实用的安装和配置指南

安装和初始配置部分提供了清晰的步骤指导:

  • 安装步骤简明扼要
  • 包含了重要的配置建议(如推荐购买会员)
  • Python环境配置说明详细实用
  • 验证安装的命令很有帮助

78-108: 极佳的源码集成建议

将TqSdk源码添加到工作区的建议非常有价值:

  • 强调了这种做法的重要性
  • 提供了两种获取源码的方法,适应不同用户需求
  • 详细说明了操作步骤
  • 清楚阐释了这样做的技术优势

这是一个非常实用的最佳实践建议。


109-173: 全面的AI交互指南

文档的核心部分内容丰富且实用:

  • 提供了具体的使用快捷键和操作方法
  • 包含了大量实际的提问示例,覆盖不同使用场景
  • 调试指导简洁实用
  • 高效提问技巧部分很有价值,能帮助用户更好地利用AI功能

174-179: 恰当的总结部分

文档结尾总结恰当:

  • 重申了Cursor对TqSdk开发的价值
  • 鼓励用户积极使用关键功能
  • 语调积极正面,有助于用户采用

@shinny-hongyan shinny-hongyan changed the title [Tqsdk-Ci] Update Version 3.8.2 [Tqsdk-Ci] Update Version 3.8.3 Jun 17, 2025
@shinny-hongyan shinny-hongyan force-pushed the create-pull-request/patch-62335b6 branch from b5b435f to 373476e Compare June 17, 2025 05:54
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b5b435f and 373476e.

📒 Files selected for processing (17)
  • PKG-INFO (1 hunks)
  • doc/advanced/backtest.rst (1 hunks)
  • doc/conf.py (3 hunks)
  • doc/demo/base.rst (30 hunks)
  • doc/index.rst (1 hunks)
  • doc/tq_trading_unit.rst (4 hunks)
  • doc/tqsdk_cursor.rst (1 hunks)
  • doc/usage/jupyter.rst (1 hunks)
  • doc/version.rst (4 hunks)
  • setup.py (1 hunks)
  • tqsdk/__version__.py (1 hunks)
  • tqsdk/api.py (13 hunks)
  • tqsdk/calendar.py (1 hunks)
  • tqsdk/demo/tutorial/t72.py (1 hunks)
  • tqsdk/lib/target_pos_task.py (1 hunks)
  • tqsdk/tqwebhelper.py (1 hunks)
  • tqsdk/tradeable/mixin.py (4 hunks)
✅ Files skipped from review due to trivial changes (6)
  • tqsdk/demo/tutorial/t72.py
  • setup.py
  • doc/usage/jupyter.rst
  • doc/demo/base.rst
  • tqsdk/lib/target_pos_task.py
  • tqsdk/tradeable/mixin.py
🚧 Files skipped from review as they are similar to previous changes (8)
  • PKG-INFO
  • tqsdk/version.py
  • doc/index.rst
  • tqsdk/tqwebhelper.py
  • doc/tq_trading_unit.rst
  • doc/version.rst
  • doc/conf.py
  • tqsdk/api.py
🔇 Additional comments (1)
doc/tqsdk_cursor.rst (1)

1-179: 请检查主索引文件是否已包含此章节
已新增 doc/tqsdk_cursor.rst 文档,需在 doc/index.rst 中加入对应条目,否则文档生成时不会展示此部分。

#!/bin/bash
grep -R "tqsdk_cursor\." doc/index.rst

Comment on lines +101 to 105
# 由于主连表没有承诺主力合约在交易日更新,所以在主连表中非交易日可能也会有主力合约,比如 DCE.v1401
# 所以 merge_result 中可能会有 _cst_date 在交易日之外的日期,导致和 self.df 行数对不齐
# 所以 merge_result 只保留 self.df 中的交易日
s = merge_result.loc[merge_result['_cst_date'].isin(set(self.df['_cst_date'])), 'underlying']
self.df[cont] = pd.Series(s.values)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

❓ Verification inconclusive

建议改用索引对齐避免隐含的长度一致性假设

当前实现:

s = merge_result.loc[merge_result['_cst_date'].isin(set(self.df['_cst_date'])), 'underlying']
self.df[cont] = pd.Series(s.values)

问题

  1. s.valuesself.df 行数必须完全一致,否则会触发 ValueError: Length of values does not match length of index。一旦 merge_result 中出现重复 _cst_date(主连表数据质量问题即可触发)或排序不一致,便会造成崩溃。
  2. 依赖「长度对齐」而非「索引对齐」,可读性和健壮性都较差。
  3. set(self.df['_cst_date']) 每次调用都会创建临时集合,成本不大但完全可以避免。

更稳健的实现方式(利用索引映射):

-        s = merge_result.loc[merge_result['_cst_date'].isin(set(self.df['_cst_date'])), 'underlying']
-        self.df[cont] = pd.Series(s.values)
+        # 先将 merge_result 以 _cst_date 设为索引,保证唯一索引后再映射
+        underlying_map = merge_result.set_index('_cst_date')['underlying']
+        # 基于 _cst_date 精确对齐,无需假设行数一致
+        self.df[cont] = self.df['_cst_date'].map(underlying_map)

优势

  • 按日期索引精准对齐,完全消除长度不匹配风险。
  • 可读性更高,意图更直观。
  • 避免不必要的 set 构造,提高微观性能。

建议评估并替换现有实现。


建议改用索引对齐避免隐含的长度一致性假设

当前实现:

s = merge_result.loc[merge_result['_cst_date'].isin(set(self.df['_cst_date'])), 'underlying']
self.df[cont] = pd.Series(s.values)

问题

  1. s.valuesself.df 行数必须完全一致,否则会触发 ValueError: Length of values does not match length of index。一旦 merge_result 中出现重复 _cst_date(主连表数据质量问题即可触发)或排序不一致,便会造成崩溃。
  2. 依赖「长度对齐」而非「索引对齐」,可读性和健壮性都较差。
  3. set(self.df['_cst_date']) 每次调用都会创建临时集合,成本不大但完全可以避免。

更稳健的实现方式(利用索引映射):

-        s = merge_result.loc[merge_result['_cst_date'].isin(set(self.df['_cst_date'])), 'underlying']
-        self.df[cont] = pd.Series(s.values)
+        # 先将 merge_result 以 _cst_date 设为索引,保证唯一索引后再映射
+        underlying_map = merge_result.set_index('_cst_date')['underlying']
+        # 基于 _cst_date 精确对齐,无需假设行数一致
+        self.df[cont] = self.df['_cst_date'].map(underlying_map)

优势

  • 按日期索引精准对齐,完全消除长度不匹配风险。
  • 可读性更高,意图更直观。
  • 避免不必要的 set 构造,提高微观性能。

建议评估并替换现有实现。

🤖 Prompt for AI Agents
In tqsdk/calendar.py around lines 101 to 105, the current code assigns values
from merge_result to self.df based on matching '_cst_date' values but relies on
length alignment which can cause ValueError if lengths mismatch or duplicates
exist. To fix this, replace the assignment with an index-based alignment
approach that uses '_cst_date' as the index to map 'underlying' values directly
to self.df, ensuring robust and clear alignment without creating temporary sets.
This will prevent length mismatch errors and improve code readability and
performance.

@@ -66,7 +66,7 @@ TqSdk 并不提供专门的参数优化机制. 您可以按照自己的需求,
target_pos.set_target_volume(3)
except BacktestFinished:
api.close()
print("SHORT=", SHORT, "最终权益=", acc.account.balance) # 每次回测结束时, 输出使用的参数和最终权益
print("SHORT=", SHORT, "最终权益=", acc.get_account().balance) # 每次回测结束时, 输出使用的参数和最终权益
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

修正示例代码的方法调用错误
并发回测示例中使用了 acc.get_account().balance,但 accTqSim 实例,不具备 get_account() 方法。建议改为:

-      print("SHORT=", SHORT, "最终权益=", acc.get_account().balance)
+      print("SHORT=", SHORT, "最终权益=", api.get_account().balance)

并与单进程示例保持一致。

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
print("SHORT=", SHORT, "最终权益=", acc.get_account().balance) # 每次回测结束时, 输出使用的参数和最终权益
print("SHORT=", SHORT, "最终权益=", api.get_account().balance) # 每次回测结束时, 输出使用的参数和最终权益
🤖 Prompt for AI Agents
In doc/advanced/backtest.rst at line 69, the code incorrectly calls
acc.get_account().balance, but acc is a TqSim instance which does not have
get_account() method. Replace acc.get_account().balance with acc.get_balance()
to match the single-process example and correctly retrieve the account balance.

@shinny-chenli shinny-chenli merged commit c75b0ed into master Jun 18, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants