Skip to content
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

[Tqsdk-Ci] Update Version 3.7.9 #503

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shinny-hongyan
Copy link
Collaborator

@shinny-hongyan shinny-hongyan commented Mar 11, 2025

Automated changes by create-pull-request GitHub action

Summary by CodeRabbit

  • Chores
    • 软件版本已由 3.7.8 升级至 3.7.9。
  • Documentation
    • 更新了文档中的版本信息和示例说明,新增了多个教程章节和合约代码展示。
    • 调整了天勤 AI 助手的访问链接,确保用户获取最新入口。
  • New Features
    • 新增了多个交易示例脚本,覆盖期权、期货、行情查询等典型操作,帮助用户更直观地了解系统功能。

Copy link

coderabbitai bot commented Mar 11, 2025

📝 Walkthrough

Walkthrough

此次变更主要包括将软件相关版本号从 3.7.8 更新至 3.7.9。多个文件(如 PKG-INFO、doc/conf.py、setup.py、tqsdk/version.py 和 doc/version.rst)中的版本信息均已更新,同时文档中新增了版本记录。部分文档中 TqSdk AI 助手的链接已统一调整为 https://www.shinnytech.com/tianqin/,并增加了新的合约代码条目。多个教程章节和 Python 示例脚本也被添加,用于展示不同的 API 调用和数据查询方式,同时对 TqApi 类的示例和参数说明作了相应调整。

Changes

文件 更改摘要
PKG-INFO, doc/conf.py, setup.py, tqsdk/version.py, doc/version.rst 更新版本号从 3.7.8 到 3.7.9,并新增版本记录及日期信息
doc/quickstart.rst, doc/tqsdk_llm.rst 更新 TqSdk AI 助手链接,统一指向 https://www.shinnytech.com/tianqin/
doc/usage/mddatas.rst 添加新的合约代码条目:SSE.588000、SSE.588080 及 SZSE.159915
doc/demo/base.rst 新增多个教程章节(tutorial-t50 至 tutorial-t59),引用对应 Python 脚本
tqsdk/api.py 修改 TqApi 类示例代码,更换部分用法并调整参数说明(如将 days 参数标注由必填变为可选)
tqsdk/demo/tutorial/t50.py, t51.py, t52.py, t53.py, t54.py, t55.py, t56.py, t57.py, t58.py, t59.py 添加多个新教程脚本,展示不同的 API 调用和数据查询方式

Sequence Diagram(s)

sequenceDiagram
    participant 用户
    participant TqApi
    participant 交易服务器
    用户->>TqApi: 初始化 API 实例并传入认证信息
    TqApi->>交易服务器: 发送连接请求
    交易服务器-->>TqApi: 返回市场数据
    TqApi->>用户: 返回查询结果及示例输出
    用户->>TqApi: 调用关闭连接
    TqApi-->>用户: 确认释放资源
Loading

Possibly related PRs

Suggested reviewers

  • shinny-chenli

Poem

我是一只快乐的小兔子,
在代码花园里欢快跳跃,
版本升级伴随着晨曦的光辉,
新教程如星辰点缀文档夜空,
每一行代码都跳动着希望的旋律,
技术路途,我们一同奔向明天! 🐰✨

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

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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: 11

🔭 Outside diff range comments (1)
PKG-INFO (1)

99-99: 💡 Verification agent

🧩 Analysis chain

确认 AI 助手链接是否需要更新

根据 AI 生成摘要,天勤 AI 助手的链接应该统一更新为 https://www.shinnytech.com/tianqin/,但当前代码中的链接仍是 https://udify.app/chat/im02prcHNEOVbPAx。请确认是否需要同步更新此链接。


🏁 Script executed:

#!/bin/bash
# 检查是否有其他文件已更新了 AI 助手链接
rg -l "https://www.shinnytech.com/tianqin/" --type-add 'doc:*.{md,rst,txt}'

Length of output: 286


请将 PKG-INFO 中的旧链接更新为最新的 AI 助手链接

经过验证,发现项目中多数文件(如 tqsdk/web/js/app.* 以及 doc 目录下的文件)已经使用了新版链接
https://www.shinnytech.com/tianqin/。而在 PKG-INFO 的第 99 行中,仍然保留着旧链接
https://udify.app/chat/im02prcHNEOVbPAx

  • 请将该文件中第 99 行的链接更新为新版链接,以确保全项目中的 URL 统一。
🧹 Nitpick comments (3)
tqsdk/demo/tutorial/t57.py (1)

1-20: 演示了期权按价值状态分类查询的基本用法

这个示例清晰地展示了如何使用query_all_level_options方法查询不同价值状态(实值、平值、虚值)的期权,代码结构合理,适合作为教程示例。建议考虑添加更多注释说明参数含义,特别是第三个参数"CALL"表示看涨期权。

-in_money_options, at_money_options, out_of_money_options = api.query_all_level_options("SHFE.au2504", quote.last_price, "CALL")
+# 查询SHFE.au2504的看涨期权,根据当前价格划分实值、平值和虚值期权
+# 参数说明:合约代码,标的物价格,期权类型("CALL"表示看涨期权,"PUT"表示看跌期权)
+in_money_options, at_money_options, out_of_money_options = api.query_all_level_options("SHFE.au2504", quote.last_price, "CALL")
tqsdk/demo/tutorial/t53.py (1)

21-22: 使用finally块确保资源释放

当前的资源释放方式在发生异常时可能不会执行,导致资源泄漏。

建议使用try-except-finally结构或者with语句来确保资源始终被正确释放:

-# 关闭api,释放相应资源
-api.close()

注意:这部分建议与上一条建议中的finally块重叠,如果采纳了上一条建议,则无需重复修改。

tqsdk/demo/tutorial/t55.py (1)

33-35: 过期的示例日期

代码中使用了2020年12月的行权日期,这是一个已经过去的日期,对于新的教程示例来说不太合适。

建议使用未来的日期作为示例:

-# 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权
-ls = api.query_options("SSE.510300", exercise_year=2020, exercise_month=12)
+# 上交所沪深300etf期权, 限制条件 2025 年 12 月份行权
+ls = api.query_options("SSE.510300", exercise_year=2025, exercise_month=12)
 print(ls)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 99411cd and fc212ce.

📒 Files selected for processing (20)
  • PKG-INFO (1 hunks)
  • doc/conf.py (1 hunks)
  • doc/demo/base.rst (1 hunks)
  • doc/quickstart.rst (1 hunks)
  • doc/tqsdk_llm.rst (2 hunks)
  • doc/usage/mddatas.rst (1 hunks)
  • doc/version.rst (1 hunks)
  • setup.py (1 hunks)
  • tqsdk/__version__.py (1 hunks)
  • tqsdk/api.py (2 hunks)
  • tqsdk/demo/tutorial/t50.py (1 hunks)
  • tqsdk/demo/tutorial/t51.py (1 hunks)
  • tqsdk/demo/tutorial/t52.py (1 hunks)
  • tqsdk/demo/tutorial/t53.py (1 hunks)
  • tqsdk/demo/tutorial/t54.py (1 hunks)
  • tqsdk/demo/tutorial/t55.py (1 hunks)
  • tqsdk/demo/tutorial/t56.py (1 hunks)
  • tqsdk/demo/tutorial/t57.py (1 hunks)
  • tqsdk/demo/tutorial/t58.py (1 hunks)
  • tqsdk/demo/tutorial/t59.py (1 hunks)
🔇 Additional comments (15)
tqsdk/__version__.py (1)

1-1: 版本号更新正确

版本号已从 '3.7.8' 更新至 '3.7.9',与 PR 标题中的版本更新一致。

PKG-INFO (1)

3-3: 版本号更新正确

在 PKG-INFO 文件中的版本号已正确从 '3.7.8' 更新至 '3.7.9',与其他文件的版本更新保持一致。

setup.py (1)

11-11: 版本号更新正确

setup.py 文件中的版本号已正确从 "3.7.8" 更新至 "3.7.9",与其他文件的版本更新保持一致。

tqsdk/demo/tutorial/t54.py (1)

1-14: 教程示例代码规范且实用

这个新增的教程示例展示了如何查询股票期权信息,代码结构清晰:

  1. 正确导入所需的 TqApi 和 TqAuth 类
  2. 创建并认证 API 实例
  3. 演示了如何查询特定条件的期权(未过期的上证50ETF看涨期权)
  4. 获取并打印期权的详细信息
  5. 最后妥善关闭 API 连接释放资源

这是一个很好的教学示例,对学习如何使用 tqsdk 查询期权数据很有帮助。

tqsdk/demo/tutorial/t50.py (1)

1-20: 代码展示了查询结算价信息的基本用法,简洁明了

这个示例演示了如何使用query_symbol_settlement方法查询合约的结算价信息,包括最近3天的数据和指定日期范围的查询方式,代码结构清晰,适合作为教程示例。注意第9行的注释很有价值,提醒用户这个函数返回的对象不会自动更新。

doc/quickstart.rst (1)

284-284: 更新了TqSdk AI助手的链接地址

链接从原来的特定聊天服务地址更改为天勤的官方网站地址,使文档中的链接更加统一和可维护。

doc/conf.py (1)

51-53: 版本号从3.7.8更新到3.7.9

文档配置文件中的版本号已正确更新,短版本和完整版本号都已同步修改。

doc/version.rst (1)

5-9: 版本号及修改记录清晰

新版本3.7.9的更新内容清晰,主要包括修正天勤AI的指向地址和增加ETF标的合约代码示例。版本号格式和发布日期符合项目的一贯风格。

doc/usage/mddatas.rst (1)

66-68: 合适的ETF合约代码新增

这些新增的ETF合约代码(上交所华夏科创50、上交所易方达科创50和深交所创业板)与版本更新记录中提到的"增加上交所深交所ETF标的合约代码示例"相符,丰富了用户可以查询的合约种类。

doc/tqsdk_llm.rst (2)

5-5: 天勤AI助手链接添加得当

在文档顶部增加天勤AI助手的使用链接,方便用户快速访问,提升了用户体验。


26-26: 链接统一更新为官方地址

将原本可能是临时或测试地址的链接更新为正式的官方地址,确保了文档的一致性和专业性。这与版本更新记录中提到的"修正天勤AI的指向地址"变更相符。

doc/demo/base.rst (1)

54-142: 文档更新正确引用了新增的教程示例

这部分变更在文档中添加了新的教程部分,从t50到t59,每个部分都包含了一个明确的标识符(如.. _tutorial-t50:)和标题,以及对应的Python脚本引用。文档结构遵循了现有的模式,保持了一致性。

新增的教程内容涵盖了查询交易所合约每日结算价、合约成交排名/持仓排名、根据条件查询合约、主连合约对应的标的合约列表、合约信息、期权合约、根据条件查询期权、查询实值/平值/虚值期权、ETF期权和股指期权查询等内容,以及查询期权希腊指标。

这些变更扩展了文档的覆盖范围,为用户提供了更多的实用示例。文档引用和格式都是正确的,没有发现问题。

tqsdk/api.py (3)

188-190: 示例代码更新提供了更好的使用指导

这个变更将示例2从使用快期账户改为展示如何使用simnow账户连接行情和交易服务器。这个更新很有价值,因为simnow是期货交易者常用的模拟交易平台,提供这样的示例能帮助用户更好地理解如何与实际交易环境连接。


194-196: 原有快期模拟账户示例被合理保留

将原来的快期模拟账户连接示例保留为示例3是个很好的做法。这确保了用户仍然可以了解如何使用TqKq类型的账户进行连接,保持了文档的完整性和连续性。这种方式既增加了新内容,又不会让依赖原有示例的用户感到困惑。


2364-2364: 参数注释更新为可选更加清晰准确

query_symbol_ranking方法中days参数的注释从"[必填]"更新为"[可选]",与实际实现更加一致。因为该参数在函数签名中已有默认值days: int = 1,所以它确实是可选参数。这个更新修正了文档与代码实现之间的不一致,提高了API文档的准确性。

Comment on lines +1 to +19
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'

from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 注意:该函数返回的对象不会更新,不建议在循环内调用该方法

# 最近 1 天持仓排名信息,以成交量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="VOLUME")
print(df.to_string())

# 最近 3 天持仓排名信息,以多头持仓量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="LONG",days=3)
print(df.to_string())

# 关闭api,释放相应资源
api.close()
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

脚本演示了如何查询期货合约排名信息

该示例脚本展示了如何使用TqApi.query_symbol_ranking函数获取期货合约的成交量排名和持仓量排名信息,对于想要了解市场参与者情况的用户很有帮助。

安全隐患:代码中包含明文凭证

代码中使用了明文的"快期账户"和"账户密码"作为身份验证参数,这存在安全风险。在实际使用中,应该避免在代码中硬编码凭证。

建议修改为:

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 方案1:使用环境变量
+import os
+api = TqApi(auth=TqAuth(os.environ.get("TQ_USERNAME"), os.environ.get("TQ_PASSWORD")))
+
+# 方案2:从配置文件读取
+# from configparser import ConfigParser
+# config = ConfigParser()
+# config.read('config.ini')
+# api = TqApi(auth=TqAuth(config['tqsdk']['username'], config['tqsdk']['password']))
📝 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 注意:该函数返回的对象不会更新,不建议在循环内调用该方法
# 最近 1 天持仓排名信息,以成交量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="VOLUME")
print(df.to_string())
# 最近 3 天持仓排名信息,以多头持仓量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="LONG",days=3)
print(df.to_string())
# 关闭api,释放相应资源
api.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
# 方案1:使用环境变量
import os
api = TqApi(auth=TqAuth(os.environ.get("TQ_USERNAME"), os.environ.get("TQ_PASSWORD")))
# 注意:该函数返回的对象不会更新,不建议在循环内调用该方法
# 最近 1 天持仓排名信息,以成交量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="VOLUME")
print(df.to_string())
# 最近 3 天持仓排名信息,以多头持仓量排序
df = api.query_symbol_ranking("SHFE.ag2504", ranking_type="LONG", days=3)
print(df.to_string())
# 关闭api,释放相应资源
api.close()


from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

硬编码凭证存在安全风险

在生产代码或者共享示例中,直接硬编码账户凭证是一个严重的安全问题。建议使用环境变量或配置文件来存储这些敏感信息,尤其是在托管到公共代码库时。

建议修改为:

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 使用环境变量或配置文件读取凭证
+import os
+username = os.getenv("TQ_USERNAME", "快期账户")  # 默认值仅用于示例
+password = os.getenv("TQ_PASSWORD", "账户密码")  # 默认值仅用于示例
+api = TqApi(auth=TqAuth(username, password))
📝 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
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 使用环境变量或配置文件读取凭证
import os
username = os.getenv("TQ_USERNAME", "快期账户") # 默认值仅用于示例
password = os.getenv("TQ_PASSWORD", "账户密码") # 默认值仅用于示例
api = TqApi(auth=TqAuth(username, password))

Comment on lines +9 to +13
quote = api.get_quote("SSE.510050")
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
print(in_money_options) # 实值期权列表
print(at_money_options) # 平值期权列表
print(out_of_money_options) # 虚值期权列表
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

缺少错误处理和详细的注释说明

代码中调用API获取行情和查询期权数据时没有任何错误处理机制。同时,对于股票代码"SSE.510050"和"SSE.510300"缺乏详细说明,不利于初学者理解。

建议添加错误处理和更详细的注释:

+# 获取上证50ETF(SSE.510050)的最新行情
 quote = api.get_quote("SSE.510050")
+
+try:
     in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
+    # 根据上证50ETF最新价格,查询沪深300ETF(SSE.510300)的认购期权
     print(in_money_options)  # 实值期权列表
     print(at_money_options)  # 平值期权列表
     print(out_of_money_options)  # 虚值期权列表
+except Exception as e:
+    print(f"查询期权数据时发生错误: {e}")
📝 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
quote = api.get_quote("SSE.510050")
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
print(in_money_options) # 实值期权列表
print(at_money_options) # 平值期权列表
print(out_of_money_options) # 虚值期权列表
# 获取上证50ETF(SSE.510050)的最新行情
quote = api.get_quote("SSE.510050")
try:
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
# 根据上证50ETF最新价格,查询沪深300ETF(SSE.510300)的认购期权
print(in_money_options) # 实值期权列表
print(at_money_options) # 平值期权列表
print(out_of_money_options) # 虚值期权列表
except Exception as e:
print(f"查询期权数据时发生错误: {e}")

Comment on lines +9 to +20
# 全部主连合约对应的标的合约
ls = api.query_cont_quotes()
print(ls)

# 大商所主连合约对应的标的合约
ls = api.query_cont_quotes(exchange_id="DCE")
print(ls)

# jd 品种主连合约对应的标的合约
ls = api.query_cont_quotes(product_id="jd")
print(ls)

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

缺少错误处理机制

代码中多次调用API查询数据,但没有任何错误处理机制。如果API调用失败,程序将直接崩溃而没有提供有用的错误信息。

建议添加try-except块来处理可能的异常:

+try:
     # 全部主连合约对应的标的合约
     ls = api.query_cont_quotes()
     print(ls)

     # 大商所主连合约对应的标的合约
     ls = api.query_cont_quotes(exchange_id="DCE")
     print(ls)

     # jd 品种主连合约对应的标的合约
     ls = api.query_cont_quotes(product_id="jd")
     print(ls)
+except Exception as e:
+    print(f"查询合约数据时发生错误: {e}")
+finally:
+    # 关闭api,释放相应资源
+    api.close()
📝 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
# 全部主连合约对应的标的合约
ls = api.query_cont_quotes()
print(ls)
# 大商所主连合约对应的标的合约
ls = api.query_cont_quotes(exchange_id="DCE")
print(ls)
# jd 品种主连合约对应的标的合约
ls = api.query_cont_quotes(product_id="jd")
print(ls)
try:
# 全部主连合约对应的标的合约
ls = api.query_cont_quotes()
print(ls)
# 大商所主连合约对应的标的合约
ls = api.query_cont_quotes(exchange_id="DCE")
print(ls)
# jd 品种主连合约对应的标的合约
ls = api.query_cont_quotes(product_id="jd")
print(ls)
except Exception as e:
print(f"查询合约数据时发生错误: {e}")
finally:
# 关闭api,释放相应资源
api.close()


from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

硬编码凭证存在安全风险

在代码中直接硬编码账户凭证是不安全的做法,特别是在示例代码中,这可能导致用户无意中提交敏感信息到代码仓库。

建议修改为:

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 使用环境变量或配置文件读取凭证
+import os
+username = os.getenv("TQ_USERNAME", "快期账户")  # 默认值仅用于示例
+password = os.getenv("TQ_PASSWORD", "账户密码")  # 默认值仅用于示例
+api = TqApi(auth=TqAuth(username, password))
📝 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
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 使用环境变量或配置文件读取凭证
import os
username = os.getenv("TQ_USERNAME", "快期账户") # 默认值仅用于示例
password = os.getenv("TQ_PASSWORD", "账户密码") # 默认值仅用于示例
api = TqApi(auth=TqAuth(username, password))

Comment on lines +8 to +23
quote = api.get_quote("SHFE.au2504")

# 预计输出的为以au2504现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空
# ['SHFE.au2504C680']
ls = api.query_atm_options("SHFE.au2504", quote.last_price, 0, "CALL")
print(ls)

# 预计输出的为以au2504开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
# ['SHFE.au2504C680', 'SHFE.au2504C688', 'SHFE.au2504C696']
ls = api.query_atm_options("SHFE.au2504", quote.open, [1,0,-1], "CALL")
print(ls)

# 预计输出沪深300股指期权,2025年12月的虚值1档期权
ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2025, exercise_month=12)
print(ls)

Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

缺少错误处理机制和代码结构优化

代码中多次调用API查询数据,但没有任何错误处理机制。另外,代码结构可以进一步优化。

建议添加错误处理并优化代码结构:

-quote = api.get_quote("SHFE.au2504")
+try:
+    # 获取黄金期货合约行情
+    quote = api.get_quote("SHFE.au2504")
+    
+    # 预计输出的为以au2504现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空
+    # ['SHFE.au2504C680']
+    ls = api.query_atm_options("SHFE.au2504", quote.last_price, 0, "CALL")
+    print(f"以最新价比对的认购平值期权: {ls}")
+    
+    # 预计输出的为以au2504开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
+    # ['SHFE.au2504C680', 'SHFE.au2504C688', 'SHFE.au2504C696']
+    ls = api.query_atm_options("SHFE.au2504", quote.open, [1,0,-1], "CALL")
+    print(f"以开盘价比对的实值、平值、虚值期权: {ls}")
+    
+    # 预计输出沪深300股指期权,2025年12月的虚值1档期权
+    ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2025, exercise_month=12)
+    print(f"沪深300股指期权2025年12月虚值1档期权: {ls}")
+except Exception as e:
+    print(f"查询期权数据时发生错误: {e}")
+finally:
+    # 关闭api,释放相应资源
+    api.close()
-
-# 预计输出的为以au2504现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空
-# ['SHFE.au2504C680']
-ls = api.query_atm_options("SHFE.au2504", quote.last_price, 0, "CALL")
-print(ls)
-
-# 预计输出的为以au2504开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
-# ['SHFE.au2504C680', 'SHFE.au2504C688', 'SHFE.au2504C696']
-ls = api.query_atm_options("SHFE.au2504", quote.open, [1,0,-1], "CALL")
-print(ls)
-
-# 预计输出沪深300股指期权,2025年12月的虚值1档期权
-ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2025, exercise_month=12)
-print(ls)
-
-# 关闭api,释放相应资源
-api.close()
📝 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
quote = api.get_quote("SHFE.au2504")
# 预计输出的为以au2504现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空
# ['SHFE.au2504C680']
ls = api.query_atm_options("SHFE.au2504", quote.last_price, 0, "CALL")
print(ls)
# 预计输出的为以au2504开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
# ['SHFE.au2504C680', 'SHFE.au2504C688', 'SHFE.au2504C696']
ls = api.query_atm_options("SHFE.au2504", quote.open, [1,0,-1], "CALL")
print(ls)
# 预计输出沪深300股指期权,2025年12月的虚值1档期权
ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2025, exercise_month=12)
print(ls)
try:
# 获取黄金期货合约行情
quote = api.get_quote("SHFE.au2504")
# 预计输出的为以au2504现在最新价来比对的认购的平值期权,当没有符合的平值期权时返回为空
# ['SHFE.au2504C680']
ls = api.query_atm_options("SHFE.au2504", quote.last_price, 0, "CALL")
print(f"以最新价比对的认购平值期权: {ls}")
# 预计输出的为以au2504开盘价来比对的认购的实值1档,平值期权,虚值1档,如果没有符合要求的期权则对应栏返回为None,如果有则返回格式例如
# ['SHFE.au2504C680', 'SHFE.au2504C688', 'SHFE.au2504C696']
ls = api.query_atm_options("SHFE.au2504", quote.open, [1, 0, -1], "CALL")
print(f"以开盘价比对的实值、平值、虚值期权: {ls}")
# 预计输出沪深300股指期权,2025年12月的虚值1档期权
ls = api.query_atm_options("SSE.000300", quote.last_price, -1, "CALL", exercise_year=2025, exercise_month=12)
print(f"沪深300股指期权2025年12月虚值1档期权: {ls}")
except Exception as e:
print(f"查询期权数据时发生错误: {e}")
finally:
# 关闭api,释放相应资源
api.close()


from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

硬编码凭证存在安全风险

在示例代码中直接硬编码账户凭证是不安全的做法。这种方式容易导致凭证泄露,尤其是当代码被提交到公共仓库时。

建议修改为:

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 使用环境变量或配置文件读取凭证
+import os
+username = os.getenv("TQ_USERNAME", "快期账户")  # 默认值仅用于示例
+password = os.getenv("TQ_PASSWORD", "账户密码")  # 默认值仅用于示例
+api = TqApi(auth=TqAuth(username, password))
📝 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
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 使用环境变量或配置文件读取凭证
import os
username = os.getenv("TQ_USERNAME", "快期账户") # 默认值仅用于示例
password = os.getenv("TQ_PASSWORD", "账户密码") # 默认值仅用于示例
api = TqApi(auth=TqAuth(username, password))

Comment on lines +9 to +38
# 标的为 "SHFE.au2504" 的所有期权
ls = api.query_options("SHFE.au2504")
print(ls)

# 标的为 "SHFE.au2504" 的看跌期权
ls = api.query_options("SHFE.au2504", option_class="PUT")
print(ls)

# 标的为 "SHFE.au2504" 的看跌期权, 未下市的
ls = api.query_options("SHFE.au2504", option_class="PUT", expired=False)
print(ls)

# 标的为 "SHFE.au2504" 、行权价为 340 的期权
ls = api.query_options("SHFE.au2504", strike_price=340)
print(ls)

# 中金所沪深300股指期权
ls = api.query_options("SSE.000300")
print(ls)

# 上交所沪深300etf期权
ls = api.query_options("SSE.510300")
print(ls)

# 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权
ls = api.query_options("SSE.510300", exercise_year=2020, exercise_month=12)
print(ls)

# 关闭api,释放相应资源
api.close()
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

缺少错误处理和代码结构优化

代码中多次调用API查询数据,但没有任何错误处理机制。同时,可以对代码结构进行优化,增强可读性和健壮性。

建议添加错误处理并改进代码结构:

+try:
     # 标的为 "SHFE.au2504" 的所有期权
     ls = api.query_options("SHFE.au2504")
-    print(ls)
+    print(f"黄金期货期权列表: {ls}")
     
     # 标的为 "SHFE.au2504" 的看跌期权
     ls = api.query_options("SHFE.au2504", option_class="PUT")
-    print(ls)
+    print(f"黄金期货看跌期权: {ls}")
     
     # 标的为 "SHFE.au2504" 的看跌期权, 未下市的
     ls = api.query_options("SHFE.au2504", option_class="PUT", expired=False)
-    print(ls)
+    print(f"黄金期货未下市看跌期权: {ls}")
     
     # 标的为 "SHFE.au2504" 、行权价为 340 的期权
     ls = api.query_options("SHFE.au2504", strike_price=340)
-    print(ls)
+    print(f"行权价为340的黄金期权: {ls}")
     
     # 中金所沪深300股指期权
     ls = api.query_options("SSE.000300")
-    print(ls)
+    print(f"沪深300股指期权: {ls}")
     
     # 上交所沪深300etf期权
     ls = api.query_options("SSE.510300")
-    print(ls)
+    print(f"沪深300ETF期权: {ls}")
     
     # 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权
     ls = api.query_options("SSE.510300", exercise_year=2020, exercise_month=12)
-    print(ls)
+    print(f"2020年12月份行权的沪深300ETF期权: {ls}")
+except Exception as e:
+    print(f"查询期权数据时发生错误: {e}")
+finally:
+    # 关闭api,释放相应资源
+    api.close()
-
-# 关闭api,释放相应资源
-api.close()
📝 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
# 标的为 "SHFE.au2504" 的所有期权
ls = api.query_options("SHFE.au2504")
print(ls)
# 标的为 "SHFE.au2504" 的看跌期权
ls = api.query_options("SHFE.au2504", option_class="PUT")
print(ls)
# 标的为 "SHFE.au2504" 的看跌期权, 未下市的
ls = api.query_options("SHFE.au2504", option_class="PUT", expired=False)
print(ls)
# 标的为 "SHFE.au2504" 、行权价为 340 的期权
ls = api.query_options("SHFE.au2504", strike_price=340)
print(ls)
# 中金所沪深300股指期权
ls = api.query_options("SSE.000300")
print(ls)
# 上交所沪深300etf期权
ls = api.query_options("SSE.510300")
print(ls)
# 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权
ls = api.query_options("SSE.510300", exercise_year=2020, exercise_month=12)
print(ls)
# 关闭api,释放相应资源
api.close()
try:
# 标的为 "SHFE.au2504" 的所有期权
ls = api.query_options("SHFE.au2504")
print(f"黄金期货期权列表: {ls}")
# 标的为 "SHFE.au2504" 的看跌期权
ls = api.query_options("SHFE.au2504", option_class="PUT")
print(f"黄金期货看跌期权: {ls}")
# 标的为 "SHFE.au2504" 的看跌期权, 未下市的
ls = api.query_options("SHFE.au2504", option_class="PUT", expired=False)
print(f"黄金期货未下市看跌期权: {ls}")
# 标的为 "SHFE.au2504" 、行权价为 340 的期权
ls = api.query_options("SHFE.au2504", strike_price=340)
print(f"行权价为340的黄金期权: {ls}")
# 中金所沪深300股指期权
ls = api.query_options("SSE.000300")
print(f"沪深300股指期权: {ls}")
# 上交所沪深300etf期权
ls = api.query_options("SSE.510300")
print(f"沪深300ETF期权: {ls}")
# 上交所沪深300etf期权, 限制条件 2020 年 12 月份行权
ls = api.query_options("SSE.510300", exercise_year=2020, exercise_month=12)
print(f"2020年12月份行权的沪深300ETF期权: {ls}")
except Exception as e:
print(f"查询期权数据时发生错误: {e}")
finally:
# 关闭api,释放相应资源
api.close()

Comment on lines +1 to +16
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'

from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))

quote = api.get_quote("SSE.510300")
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
ls = in_money_options + at_money_options + out_of_money_options # 期权列表
df = api.query_option_greeks(ls)
print(df.to_string()) # 显示期权希腊指标

# 关闭api,释放相应资源
api.close()
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

代码示例展示了期权希腊指标查询功能,但存在几个可改进之处

这段代码展示了如何使用TqSdk查询期权希腊指标的功能,逻辑是完整的,但有以下几个需要注意的问题:

  1. 代码中使用了硬编码的凭证,这在示例代码中可能导致安全问题
  2. 缺少异常处理机制,API调用可能会失败
  3. 没有足够的注释说明代码各部分的功能和目的
  4. 对于可能的边缘情况(如找不到期权)没有处理

建议对代码进行以下修改:

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'chaos'

from tqsdk import TqApi, TqAuth

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 创建API实例,请在运行时填入有效的账户和密码
+api = TqApi(auth=TqAuth("您的账户", "您的密码"))

+try:
    # 获取上证50ETF的行情
    quote = api.get_quote("SSE.510300")
    # 根据当前价格查询认购期权,并按实值、平值、虚值分类
    in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
    ls = in_money_options + at_money_options + out_of_money_options  # 期权列表
+    # 如果找到了期权
+    if ls:
        df = api.query_option_greeks(ls)
        print(df.to_string())  # 显示期权希腊指标
+    else:
+        print("未找到符合条件的期权")
+except Exception as e:
+    print(f"发生错误: {e}")
+finally:
    # 关闭api,释放相应资源
    api.close()
-
📝 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
quote = api.get_quote("SSE.510300")
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys = 1)
ls = in_money_options + at_money_options + out_of_money_options # 期权列表
df = api.query_option_greeks(ls)
print(df.to_string()) # 显示期权希腊指标
# 关闭api,释放相应资源
api.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
# 创建API实例,请在运行时填入有效的账户和密码
api = TqApi(auth=TqAuth("您的账户", "您的密码"))
try:
# 获取上证50ETF的行情
quote = api.get_quote("SSE.510300")
# 根据当前价格查询认购期权,并按实值、平值、虚值分类
in_money_options, at_money_options, out_of_money_options = api.query_all_level_finance_options("SSE.510300", quote.last_price, "CALL", nearbys=1)
ls = in_money_options + at_money_options + out_of_money_options # 期权列表
# 如果找到了期权
if ls:
df = api.query_option_greeks(ls)
print(df.to_string()) # 显示期权希腊指标
else:
print("未找到符合条件的期权")
except Exception as e:
print(f"发生错误: {e}")
finally:
# 关闭api, 释放相应资源
api.close()

Comment on lines +1 to +50
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'

from tqsdk import TqApi, TqAuth

api = TqApi(auth=TqAuth("快期账户", "账户密码"))

# 不推荐使用以下方式获取符合某种条件的合约列表,推荐使用接口来完成此功能。
# ls = [k for k,v in api._data["quotes"].items() if k.startswith("KQ.m")]
# print(ls)

# au 品种的全部合约,包括已下市以及未下市合约
ls = api.query_quotes(ins_class="FUTURE", product_id="au")
print(ls)

# au、cu 品种的全部未下市合约合约
ls = api.query_quotes(ins_class=["FUTURE"], product_id=["au", "cu"], expired=False)
print(ls)

# au 品种指数合约
ls = api.query_quotes(ins_class="INDEX", product_id="au")
print(ls)

# 全部主连合约
ls = api.query_quotes(ins_class="CONT")
print(ls)

# au 品种主连合约
ls = api.query_quotes(ins_class="CONT", product_id="au")
print(ls)

# 上期所带夜盘的期货合约列表
ls = api.query_quotes(ins_class="FUTURE", exchange_id="SHFE", has_night=True)
print(ls)

# au 品种的全部未下市合约、指数、主连
ls = api.query_quotes(product_id="au", expired=False)
print(ls)

# 上海交易所股票代码列表
ls = api.query_quotes(ins_class="STOCK", exchange_id="SSE", expired=False)
print(ls)

# 上海交易所基金代码列表
ls = api.query_quotes(ins_class="FUND", exchange_id="SSE", expired=False)
print(ls)

# 关闭api,释放相应资源
api.close()
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

示例展示了多种合约查询功能,但存在安全和健壮性问题

这段代码很好地展示了使用query_quotes方法查询不同类型金融工具的各种方式,但有以下几个需要改进的地方:

  1. 与t59.py类似,使用了硬编码的凭证信息
  2. 缺少异常处理机制
  3. 注释中说明了不推荐的方式,但没有详细解释原因
  4. 每个查询后直接打印结果,但没有格式化输出,可能导致大量数据难以阅读

建议进行以下改进:

#!/usr/bin/env python
#  -*- coding: utf-8 -*-
__author__ = 'chaos'

from tqsdk import TqApi, TqAuth

-api = TqApi(auth=TqAuth("快期账户", "账户密码"))
+# 创建API实例,请在运行时填入有效的账户和密码
+api = TqApi(auth=TqAuth("您的账户", "您的密码"))

+try:
    # 不推荐使用以下方式获取符合某种条件的合约列表,推荐使用接口来完成此功能。
    # 原因:直接访问内部数据结构不稳定,而且可能导致意外行为。
    # ls = [k for k,v in api._data["quotes"].items() if k.startswith("KQ.m")]
    # print(ls)

    # au 品种的全部合约,包括已下市以及未下市合约
    ls = api.query_quotes(ins_class="FUTURE", product_id="au")
-    print(ls)
+    print("黄金品种全部合约:", ls)
+    print("-" * 50)

    # au、cu 品种的全部未下市合约合约
    ls = api.query_quotes(ins_class=["FUTURE"], product_id=["au", "cu"], expired=False)
-    print(ls)
+    print("黄金和铜品种的未下市合约:", ls)
+    print("-" * 50)

    # au 品种指数合约
    ls = api.query_quotes(ins_class="INDEX", product_id="au")
-    print(ls)
+    print("黄金品种指数合约:", ls)
+    print("-" * 50)

    # 全部主连合约
    ls = api.query_quotes(ins_class="CONT")
-    print(ls)
+    print("全部主连合约:", ls)
+    print("-" * 50)

    # au 品种主连合约
    ls = api.query_quotes(ins_class="CONT", product_id="au")
-    print(ls)
+    print("黄金品种主连合约:", ls)
+    print("-" * 50)

    # 上期所带夜盘的期货合约列表
    ls = api.query_quotes(ins_class="FUTURE", exchange_id="SHFE", has_night=True)
-    print(ls)
+    print("上期所带夜盘的期货合约:", ls)
+    print("-" * 50)

    # au 品种的全部未下市合约、指数、主连
    ls = api.query_quotes(product_id="au", expired=False)
-    print(ls)
+    print("黄金品种的全部未下市合约、指数、主连:", ls)
+    print("-" * 50)

    # 上海交易所股票代码列表
    ls = api.query_quotes(ins_class="STOCK", exchange_id="SSE", expired=False)
-    print(ls)
+    print(f"上海交易所股票代码列表 (数量: {len(ls)}):", ls[:10], "...")  # 只打印前10个,避免输出过多
+    print("-" * 50)

    # 上海交易所基金代码列表
    ls = api.query_quotes(ins_class="FUND", exchange_id="SSE", expired=False)
-    print(ls)
+    print(f"上海交易所基金代码列表 (数量: {len(ls)}):", ls[:10], "...")  # 只打印前10个,避免输出过多
+except Exception as e:
+    print(f"发生错误: {e}")
+finally:
    # 关闭api,释放相应资源
    api.close()
📝 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
api = TqApi(auth=TqAuth("快期账户", "账户密码"))
# 不推荐使用以下方式获取符合某种条件的合约列表,推荐使用接口来完成此功能。
# ls = [k for k,v in api._data["quotes"].items() if k.startswith("KQ.m")]
# print(ls)
# au 品种的全部合约,包括已下市以及未下市合约
ls = api.query_quotes(ins_class="FUTURE", product_id="au")
print(ls)
# au、cu 品种的全部未下市合约合约
ls = api.query_quotes(ins_class=["FUTURE"], product_id=["au", "cu"], expired=False)
print(ls)
# au 品种指数合约
ls = api.query_quotes(ins_class="INDEX", product_id="au")
print(ls)
# 全部主连合约
ls = api.query_quotes(ins_class="CONT")
print(ls)
# au 品种主连合约
ls = api.query_quotes(ins_class="CONT", product_id="au")
print(ls)
# 上期所带夜盘的期货合约列表
ls = api.query_quotes(ins_class="FUTURE", exchange_id="SHFE", has_night=True)
print(ls)
# au 品种的全部未下市合约、指数、主连
ls = api.query_quotes(product_id="au", expired=False)
print(ls)
# 上海交易所股票代码列表
ls = api.query_quotes(ins_class="STOCK", exchange_id="SSE", expired=False)
print(ls)
# 上海交易所基金代码列表
ls = api.query_quotes(ins_class="FUND", exchange_id="SSE", expired=False)
print(ls)
# 关闭api,释放相应资源
api.close()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'chaos'
from tqsdk import TqApi, TqAuth
# 创建API实例,请在运行时填入有效的账户和密码
api = TqApi(auth=TqAuth("您的账户", "您的密码"))
try:
# 不推荐使用以下方式获取符合某种条件的合约列表,推荐使用接口来完成此功能。
# 原因:直接访问内部数据结构不稳定,而且可能导致意外行为。
# ls = [k for k,v in api._data["quotes"].items() if k.startswith("KQ.m")]
# print(ls)
# au 品种的全部合约,包括已下市以及未下市合约
ls = api.query_quotes(ins_class="FUTURE", product_id="au")
print("黄金品种全部合约:", ls)
print("-" * 50)
# au、cu 品种的全部未下市合约合约
ls = api.query_quotes(ins_class=["FUTURE"], product_id=["au", "cu"], expired=False)
print("黄金和铜品种的未下市合约:", ls)
print("-" * 50)
# au 品种指数合约
ls = api.query_quotes(ins_class="INDEX", product_id="au")
print("黄金品种指数合约:", ls)
print("-" * 50)
# 全部主连合约
ls = api.query_quotes(ins_class="CONT")
print("全部主连合约:", ls)
print("-" * 50)
# au 品种主连合约
ls = api.query_quotes(ins_class="CONT", product_id="au")
print("黄金品种主连合约:", ls)
print("-" * 50)
# 上期所带夜盘的期货合约列表
ls = api.query_quotes(ins_class="FUTURE", exchange_id="SHFE", has_night=True)
print("上期所带夜盘的期货合约:", ls)
print("-" * 50)
# au 品种的全部未下市合约、指数、主连
ls = api.query_quotes(product_id="au", expired=False)
print("黄金品种的全部未下市合约、指数、主连:", ls)
print("-" * 50)
# 上海交易所股票代码列表
ls = api.query_quotes(ins_class="STOCK", exchange_id="SSE", expired=False)
print(f"上海交易所股票代码列表 (数量: {len(ls)}):", ls[:10], "...")
print("-" * 50)
# 上海交易所基金代码列表
ls = api.query_quotes(ins_class="FUND", exchange_id="SSE", expired=False)
print(f"上海交易所基金代码列表 (数量: {len(ls)}):", ls[:10], "...")
except Exception as e:
print(f"发生错误: {e}")
finally:
# 关闭api,释放相应资源
api.close()

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.

1 participant