Skip to content
All library documents

TqSdk Design Principles for Flexible, Simple, Verifiable Trading Programs

Article TqSdk

Summary

This documentation explains design choices behind TqSdk, a Python trading software development kit. It aims to avoid imposing a strategy model: users can fetch data and issue orders freely, while examples demonstrate possible applications instead of prescribing strategy templates. The library also permits multiple API instances within one program.

A second goal is to keep user code close to the intended workflow. The design avoids threads and callbacks, provides a position-adjustment utility, and allows switching among live, simulated, backtest, and replay modes with a localized code change. For reproducibility, the SDK uses data flow between components, logs received data packets, and can use those logs as inputs to its unit testing framework. The document also identifies developer prerequisites, including financial domain knowledge, Python asyncio, the Diff communication protocol, and Pandas or NumPy. These are architectural principles and implementation notes, not a trading strategy or evidence of market performance; the logging section is marked as unfinished.

Key ideas

  • TqSdk is designed to support varied user strategies rather than require a fixed strategy template.
  • Its synchronous-looking workflow avoids threads and callbacks to reduce complexity in user code.
  • Live, simulated, backtest, and replay modes are intended to be switched with a small code change.
  • Complete packet logging supports issue reproduction and use of recorded inputs in tests.
  • Understanding the SDK internals requires familiarity with asyncio, its communication protocol, and numerical data libraries.

Tags

Full text
# general


.. _dev_general:

原则与规范
====================================================

TqSdk设计原则
----------------------------------------------------

不预设用户策略模型
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我们深刻认识到, 量化交易是一个充满竞争与创新的领域,成功的用户总是在不断构思和尝试全新的理念与模型. 在这方面, 用户比我们知道得更多, 走得也更快. 因此, 我们在设计TqSdk时, 总是尽力避免对用户的模型结构做限定, 而是专注于为用户提供通用性的资源和能力. 

我们的以下设计决策遵循了此原则:

* 不提供策略类模板, 只以示例程序方式展示各类策略应用
* 一个策略程序中可以任意获取数据和发出指令
* 允许用户在一个程序中使用任意多个TqApi实例


保持用户代码简单
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我们对TqSdk的一个设计目标是尽量让用户的代码与需求方案保持一致(顺序相同, 篇幅相当). 

我们的以下设计决策遵循了此原则:

* 不使用多线程, 避免用户处理线程同步问题
* 不使用回调模型, 避免用户维护状态机状态变量
* 提供专门的调仓工具
* 实盘/模拟/回测/复盘 几种不同运行模式切换, 只需要在代码中做单点修改


行为可验证
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
作为TqSdk库质量管控的关键措施, 我们要求 TqSdk 在运行时可以记录完整的输入信息, 以确保发生问题时可以稳定重现问题和定位原因. 我们做了这些决定:

* 以数据流衔接库中的各组件
* TqSdk 的日志文件完整记录收到的全部数据包
* 专门构建了一个单元测试框架, 可以直接用日志作为测试用例输入


相关知识与技能
----------------------------------------------------
TqSdk 的开发涉及以下知识点,您可能需要先学习它们,才能更好的理解和改进TqSdk的代码

* 金融相关业务知识

    TqSdk是用于金融交易领域的专用软件包。我们假定用户和开发者都已经具备相应的基础知识,在TqSdk的文档中不再详加解释。
    
* python asyncio

    TqSdk 的代码大量依赖 python asyncio 机制. asyncio 的编程模型与传统 python 程序差异很大. 我们对于TqSdk的使用者尽量隐藏了 asyncio 相关概念, 允许用户在不了解 asyncio 的情况下实现绝大多数业务需求. 但是对于开发者, 若不了解 asyncio, 在理解 TqSdk 内部代码实现时会非常困难。
    
* Diff协议

    TqSdk 并不是一个 all-in-one 的包, 它的能力有赖于一系列后台服务的支持. DIFF协议是 TqSdk 与后台服务间通讯的主要协议, 开发者需对 DIFF 有所理解, 才能掌握 TqSdk 的内部实现
    
* Pandas/Numpy
    
    Pandas/Numpy 是非常优秀的 python 数值计算库. TqSdk 利用这些库完成K线序列数据的存储和操作. 


代码风格
----------------------------------------------------
TqSdk的代码风格遵循 PEP8 规范.


日志规范
----------------------------------------------------
@todo: 待写

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.