TqSdk Python Library

repository·master·Indexed 26 days ago

https://github.com/shinnytech/tqsdk-python

An open-source Python library for quantitative trading strategy development. TqSdk provides a complete solution for futures, options, and stocks, covering historical and real-time data, tick-level and K-line backtesting, simulated trading, and live execution. It features multi-account support, connectivity via CTP and various asset management gateways, and an optimized in-memory database for low-latency access to market data using pandas and numpy.

Tokens
63.5K
Snippets
96
Records
332
Agent score
89%

What's inside tqsdk-python

  1. Overview of TqSdk features

    master

    TqSdk provides a full suite of solutions for quantitative trading, including:

    • Data: Access to all Tick and K-line data for tradable contracts since listing.
    • Multi-account support: Simultaneous trading with multiple live or simulated accounts.
    • Asset classes: Support for Futures, Options, and Stocks.
    • Connectivity: Supports CTP direct connection, and various asset management gateways like Zhongqi, Ronghang, Jies, Yida, and ctpmini.
    • Backtesting: Tick-level and K-line level backtesting for complex strategies.
    • Technical Indicators: Includes nearly 100 technical indicator functions with source code.
    • Performance: Uses in-memory databases for low-latency access to market and trading data; optimized for pandas and numpy.
  2. Understand TqSdk data flow and TqChan

    master

    TqSdk components are connected via unidirectional data flow pipelines called TqChan (which are essentially asyncio.Queue instances). One component pushes data packets into the TqChan, and another component pulls them out sequentially.

    Data Flow Channels

    • api_send_chan: Used by TqApi to send data packets (e.g., orders) to the account component.
    • api_recv_chan: Used by TqApi to receive data packets (e.g., market data) from the account component.
    • td_send_chan: Used by TqAccount to send transaction-related data to the trading gateway.
    • td_recv_chan: Used by TqAccount to receive transaction-related data from the trading gateway.
    • md_send_chan: Used to send market data requests/commands.
    • md_recv_chan: Used to receive market data packets from the market gateway.
  3. Understand TqSdk design principles

    master

    TqSdk is designed to be a flexible, low-overhead toolkit rather than a rigid framework. Key design principles include:

    • No Presupposed Strategy Models: TqSdk does not force a specific strategy structure on you. It provides general resources and capabilities instead of strategy templates. You can fetch data and issue instructions arbitrarily within a single program and use multiple TqApi instances in one program.
    • Code Simplicity: The library aims to keep user code aligned with their actual requirements. To achieve this, it avoids multi-threading (preventing synchronization issues) and avoids callback models (preventing complex state machine management).
    • Verifiable Behavior: TqSdk uses a data-flow architecture and logs all received data packets to ensure that issues can be reproduced and debugged using the logs as test cases.
    • Seamless Mode Switching: Switching between Live (实盘), Simulation (模拟), and Backtesting (回测) modes typically requires only a single-point modification in your code.
  4. Options Tutorial Roadmap

    master

    The following tutorials are available in the tqsdk/demo/option_tutorial/ directory to help you master different aspects of options trading:

    • Market Data & Querying:

      • o10.py: Get real-time options market data.
      • o20.py: Query options that meet specific requirements.
      • o71.py: Get a group of options and their corresponding strike prices.
    • Classification & Greeks:

      • o30.py: Query ITM/ATM/OTM options.
      • o40.py: Calculate option Greeks.
      • o41.py: Calculate implied volatility (IV) and historical volatility.
      • o72.py & o73.py: Methods for classifying options by ITM/ATM/OTM relative to the underlying asset.
    • Advanced Strategies & Analytics:

      • o60.py: Get the options volatility surface.
      • o70.py: Implement option arbitrage strategies.
      • o74.py: Locally calculate margin for opening ETF option short positions.
  5. Understand the TqSdk data flow architecture

    master

    TqSdk uses a data flow architecture where components are connected via TqChan objects. A TqChan is essentially an asyncio.Queue used as a unidirectional data pipe: one component puts data packets into the channel, and another component retrieves them sequentially.

    Data Flow Directions

    Upstream (e.g., Placing an Order)

    1. The user program calls a function in TqApi (e.g., TqApi.insert_order).
    2. TqApi generates a data packet and places it into api_send_chan.
    3. TqAccount retrieves the packet from api_send_chan and, based on the aid field, places it into td_send_chan.
    4. The Websocket Client connected to the trading gateway retrieves the packet from td_send_chan and sends it over the network.

    Downstream (e.g., Receiving Market Data)

    1. The Websocket Client connected to the market data gateway receives a packet from the network and places it into md_recv_chan.
    2. TqAccount retrieves the packet from md_recv_chan and places it into api_recv_chan.
    3. TqApi retrieves the packet from api_recv_chan and merges the market data contained in the packet into its in-memory storage.

    Switching Operating Modes

    You can switch between live trading, simulation, and backtesting by replacing specific components in the data flow:

    • Live Trading: Uses TqAccount to bridge the API and the trading gateways.
    • Simulation: Replace TqAccount with TqSim to simulate trading behavior.
    • Backtesting: Uses TqBacktest instead of TqAccount, utilizing backtest_recv_chan and backtest_send_chan to manage data flow between TqApi and the backtesting engine.
  6. Key Features of TqSdk

    master

    TqSdk supports a wide range of quantitative trading needs:

    • Data: Access to all Tick and K-line data for all tradable contracts since listing.
    • Trading: Supports live trading with dozens of futures companies and simulated trading via TqKq.
    • Backtesting: Supports Tick-level and K-line level backtesting for complex strategies.
    • Indicators: Provides nearly 100 technical indicator functions and their source code.
    • Performance: Uses an in-memory database for market and trading data to ensure zero access latency. No manual database maintenance is required.
    • Integration: Optimized support for pandas and numpy.
    • Flexibility: No forced framework structure; supports arbitrary strategy complexity and multi-symbol trading within a single program.
  7. Understand the TqSdk Architecture vs vn.py

    master

    Unlike vn.py, which uses a centralized runner that calls user-defined strategy classes (subclasses of CtaTemplate), TqSdk follows an inverted architecture. In TqSdk, the user's strategy code is the caller, and the TqSdk library is the callee.

    Each strategy in TqSdk is implemented as a standalone Python script/process. This design provides several advantages:

    • Multi-core utilization: Running multiple strategies allows for full use of multi-CPU computing power.
    • Isolation: Each strategy can be started, stopped, debugged, or modified independently without affecting other running strategies.
    • Flexibility: Users can freely combine any functions from the TqSdk package, such as using multiple contracts, different timeframes (K-lines), or combining Tick and Orderbook data within a single strategy.
    • Integration: Easier to integrate with other third-party libraries or frameworks.
  8. Understand the TqSdk file structure

    master

    The TqSdk library is organized into several core modules that handle API interaction, data structures, and trading utilities. Key files include:

    • api.py: The main entry point for the TqApi interface.
    • objs.py: Definitions for primary business data structures.
    • sim.py: Local simulation trading capabilities.
    • backtest.py: Support for backtesting strategies.
    • ta.py & tafunc.py: Technical analysis indicators and functions.
    • tqhelper.py: Auxiliary code for TqApi.
    • exception.py: Definitions for exception types.
    • lib.py: General trading auxiliary tools.
    • ctpse/*: Modules for collecting penetration-style regulatory information.
    • demo/*: Example programs for implementation guidance.
  9. TqSdk System Architecture

    master

    TqSdk operates using a gateway-based architecture connected via the Diff protocol.

    • Open Md Gateway (行情网关): Provides real-time market data and historical data.
    • Open Trade Gateway (交易中继网关): Connects to futures company trading systems (e.g., CTP, FEMAS, UFX).
    • TqSdk: Connects to both gateways using the Diff protocol to implement market data and trading functionalities.
  10. Quickstart with TqSdk

    master

    TqSdk is an open-source Python library for quantitative trading. It provides a full solution including historical and real-time data, development debugging, backtesting, simulated trading, live trading, monitoring, and risk management.

    To use TqSdk, you typically follow this workflow:

    1. Initialize a TqApi instance with a TqAccount and TqAuth.
    2. Subscribe to market data using api.get_quote(symbol).
    3. Use TargetPosTask to manage position adjustments.
    4. Run a loop using api.wait_update() to process data updates and execute logic.
    from tqsdk import TqApi, TqAuth, TqAccount, TargetPosTask
    
    # Create TqApi instance and specify trading account
    api = TqApi(TqAccount("H宏源期货", "4003242", "123456"), auth=TqAuth("快期账户", "账户密码"))
    
    # Subscribe to near-month contract quote
    q_2610 = api.get_quote("SHFE.rb2610")
    # Create position adjustment tool for near-month contract
    t_2610 = TargetPosTask(api, "SHFE.rb2610")
    
    # Subscribe to far-month contract quote
    q_2701 = api.get_quote("SHFE.rb2701")
    # Create position adjustment tool for far-month contract
    t_2701 = TargetPosTask(api, "SHFE.rb2701")
    
    while True:
        api.wait_update()  # Wait for data update
        spread = q_2610.last_price - q_2701.last_price  # Calculate spread
        print("当前价差:", spread)
        
        if spread > 250:
            print("价差过高: 空近月,多远月")
            t_2610.set_target_volume(-1)  # Set target to short 1 lot
            t_2701.set_target_volume(1)   # Set target to long 1 lot
        elif spread < 200:
            print("价差回复: 清空持仓")
            t_2610.set_target_volume(0)
            t_2701.set_target_volume(0)
  11. Getting started with TqSdk

    master

    If you are new to TqSdk, it is recommended to follow this learning path to understand the SDK effectively:

    1. Introduction: Understand the core concepts.
    2. Quickstart: Get your first environment running and execute basic tasks.
    3. Framework & Account: Learn about the framework and shinny_account modules.
    4. Core Modules: Explore mddatas (market data), trade (trading), and targetpostask (target post ask).
    5. Advanced Topics: Dive into specialized features once the basics are mastered.