Skip to content
All library documents

Python Fundamentals for Quantitative Trading Beginners

Article TqSdk

Summary

This beginner guide introduces Python syntax and core programming constructs that are useful when starting quantitative strategy research. It covers indentation, comments, assignment, imports, basic types and arithmetic, comparisons, and conditional logic. It then explains how to work with lists, tuples, strings, dictionaries, and sets, including indexing, slicing, mutation, lookup, and common operations.

The later sections introduce functions, default and keyword arguments, multiple return values, lambda expressions, loops, and list, set, and dictionary comprehensions. Short examples demonstrate the behavior of each concept, such as sorting by a custom key or filtering values during a comprehension. The guide recommends Python 3 and positions the material as a starting point for practice in a strategy development environment. It is a broad syntax primer rather than a treatment of market data, statistical methods, portfolio construction, or backtest design; readers will need further material to apply these programming basics responsibly to trading research.

Key ideas

  • Python uses indentation to mark code blocks, with colons introducing many compound statements.
  • Built-in collections differ in ordering, mutability, uniqueness, and access patterns.
  • Functions support parameters, defaults, return values, and compact lambda expressions.
  • Loops and comprehensions provide ways to process iterable data and build collections.
  • The guide recommends Python 3 for beginners.

Tags

Full text
# nl research


.. _nl_research:

自然语言投研:EDB 数据服务 + 扣子(Coze)
================================================================================

本主题介绍如何把 `天勤 EDB 数据服务(HTTP) <https://edb.shinnytech.com/docs/index.html#>`_ 与 `扣子(Coze) <https://www.coze.cn/>`_ 结合起来,用自然语言完成“取数 → 验证想法 → 回测 → 产出图表/报告/代码”的投研闭环。

推荐使用的方式是:先在扣子中添加天勤 EDB 数据 SKILL,再直接提研究问题。无需再手动复制 EDB 文档给扣子。

你可以把扣子当作“自然语言的研究助理”,把 EDB 数据服务当作“可被调用的数据接口”。添加 SKILL 后,扣子可以继续生成:

- 可复用的 Python 脚本(pandas / matplotlib 等)
- 研究结论与报告结构
- 可视化图表(收益曲线、价差曲线、因子检验等)

案例链接(扣子分享)
--------------------------------

- `股指期货收益来源可视化分析 <https://www.coze.cn/s/yvzxmWYMS3E/>`_
- `ag2608 au2608 价差曲线分析 <https://www.coze.cn/s/okMcnINx3Po/>`_
- `黄金2604策略逻辑及收益曲线绘制 <https://www.coze.cn/s/8nzZbuCrsXE/>`_


前置准备
--------------------------------

1. **添加扣子 SKILL(推荐)**

   - SKILL 添加地址:`天勤 EDB 数据 SKILL <https://www.coze.cn/?skill_share_pid=7602130347391959083>`_
   - 添加后即可在对话中直接让扣子获取历史数据,并继续完成想法验证或策略回测。
   
   .. figure:: ../images/coze_skill.png
   
      扣子 SKILL 页面示意(点击“使用”完成添加)。

2. **EDB 数据服务**

   - 行情历史服务(CSV 流式):文档:`行情历史服务 <https://edb.shinnytech.com/docs/md_server.html#>`__
   - Token 服务(JWT):文档:`Token 服务 <https://edb.shinnytech.com/docs/token_server.html#>`__
   - EDB 指标服务(JSON):文档:`非价量数据服务 <https://edb.shinnytech.com/docs/data_server.html#>`__

3. **扣子账号**

   - 访问:`扣子(Coze) <https://www.coze.cn/>`_
   - 当每天使用频率较高时,可能需要购买扣子的月费会员(以扣子平台规则为准)。

数据权限与范围说明
--------------------------------

- **免费访问(无需 token)**
  - 日线数据(``period=86400``):任意历史区间
  - 1 分钟线(``period=60``):最近 1 年
- **专业版访问(需 token)**
  - 1 分钟线/日线:不限制历史长度
  - EDB 指标目录与数值查询(宏观/基本面等)

在扣子里直接提问(推荐)
--------------------------------

完成 SKILL 添加后,直接把“你的需求 + 数据口径”说清楚即可。

下面是一段推荐的提问模板(把 ``XXXXX`` 替换成你的目标):

.. code-block:: text

   我已经添加了天勤 EDB 数据 SKILL,请帮我获取历史数据并实现 XXXXX。
   建议的约束与输出要求:
   1) 明确用哪个 symbol、period、起止时间
   2) 输出可直接运行的 Python 代码
   3) 给出结论摘要,可视化结果与可复现说明(数据口径、假设、风险点)

.. figure:: ../images/coze_skill_use_teplate.png

   添加 SKILL 后,可直接按模板在输入框中描述需求并发送。

.. note::

   如果你需要 1 分钟线的更长历史区间或 EDB 指标数据,扣子可能会提示你需要 token(专业版权限)。


对话提示词模板(建议)
--------------------------------

为了让扣子更稳定地产出“可运行的 Python 代码 + 可复现的数据口径”,建议在提问时把关键信息说全:

- 合约/指标(symbol 或 指标关键词/ID)
- 周期(``period=60`` 或 ``period=86400``)
- 起止时间(建议给到日期或日期时间)
- 输出字段(例如 ``open,high,low,close,volume``)
- 研究目标(检验/回测逻辑、输出图表类型、需要的指标)

示例 1:隔夜 vs 日内收益拆分
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: text

   我已经添加了天勤 EDB 数据 SKILL,请获取 KQ.m@CFFEX.IF,KQ.m@CFFEX.IC,KQ.m@CFFEX.IH 的日线数据,区间 2021-01-01 到 2026-01-31。
   计算并可视化:
   1) 隔夜收益(当日 open / 前日 close - 1)的累计收益
   2) 日内收益(当日 close / 当日 open - 1)的累计收益
   给出结论摘要,并输出可直接运行的 Python 代码。

示例 2:价差曲线分析
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: text

   我已经添加了天勤 EDB 数据 SKILL,请获取 SHFE.ag2608 与 SHFE.au2608 的日线 close,计算价差 ag-au 并画出价差曲线;
   额外输出:价差的滚动均值/标准差(例如 20 日),并给出异常波动区间的解释思路。

示例 3:策略回测脚本生成
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: text

   我已经添加了天勤 EDB 数据 SKILL,请获取 period=60 的 SHFE.au2608 历史数据,并做一个简单策略回测:
   - 指标:20/60 均线金叉死叉
   - 交易规则:金叉开多、死叉平仓
   - 输出:权益曲线、最大回撤、年化收益
   如果免费权限不够,请提示我需要 token 并告诉我如何配置。

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.