ai4trade-xtquant

repository·main·Indexed 19 days ago

https://github.com/ai4trade/xtquant

A Python wrapper for the XunTou QMT (Quantitative Management Terminal) system. It provides APIs for market data acquisition (Tick, Minute, Daily), historical data management with Clickhouse integration, and quantitative strategy research. Key features include real-time data subscription, technical indicator extraction via pandas_ta, news event quantization using BERT, and specialized tools for A-share, ETF, and convertible bond data synchronization.

Tokens
14.5K
Snippets
46
Records
58
Agent score
66%

What's inside ai4trade-xtquant

  1. Overview of XtQuant and QMT capabilities

    main

    XtQuant provides Python API wrappers for the XunTou QMT (Quantitative Management Terminal) system. It is designed to facilitate quantitative strategy trading by providing access to market data (行情), trading execution (交易), and strategy research (策略).

    Key functional areas include:

    • Market Data (行情): Accessing historical data (Tick, Minute, Daily) and real-time streaming data.
    • Data Management: Batch caching of historical data and transferring data to databases like Clickhouse.
    • Quantitative Research: Implementing news event analysis (NLP/BERT), technical indicator extraction (using pandas_ta), and cross-market correlation studies.
  2. XtData Interface Classifications

    main

    The xtdata module organizes its interfaces into several functional categories:

    Market Data (K-lines, Tick data)

    Interfaces are categorized by prefix:

    • subscribe_ / unsubscribe_: For subscribing to or unsubscribing from real-time data streams.
    • get_: For actively retrieving data from the current buffer.
    • download_: For downloading historical data to supplement the local cache.

    Other Data Types

    • Financial Data: Interfaces for accessing financial reports and statements.
    • Contract Basic Information: Information regarding contract specifications and types.
    • Static Information: Sector classifications and industry information.
  3. How XtQuant.XtData works with MiniQmt

    main

    The xtdata module provides market data (historical and real-time K-lines, tick data, financial data, etc.) by interacting with MiniQmt.

    Key operational logic:

    • Connection: xtdata establishes a connection to MiniQmt, which handles the actual market data requests and returns results to the Python layer. To check data availability or switch connections, you should operate directly on MiniQmt.
    • Data Availability: Before calling data retrieval interfaces, ensure MiniQmt has the required data. If data is missing, use the download_ interfaces to supplement it first.
    • Subscription Model: For real-time data, set a callback. When new data arrives, it is returned via the callback. It is recommended to save subscribed data locally, as you won't need to supplement the same data type repeatedly once subscribed.
    • Data Types:
      • Level 1: Use download_history_data for historical parts, subscribe_XXX for real-time parts, and get_XXX to retrieve the current buffer.
      • Level 2: Use subscribe_XXX for real-time parts and get_l2_XXX to retrieve them. Note that Level 2 functions do not store historical data and data is cleared after the trading day ends.
  4. Access and manage market data via XtQuant

    main

    The library supports several workflows for handling market data:

    1. Historical Data Download: Downloading historical market data via QMT interfaces.
    2. Batch Caching: Obtaining batches of stock codes and caching corresponding Tick, Minute, and Daily level historical data.
    3. Database Integration: Transferring cached local data (Tick and K-line data) into databases such as Clickhouse.
    4. Real-time Data Access: Using the xtquant Python API to tap into the full-push (全推) market data capability to build independent real-time market data services for processing Tick and 1-Minute granularity data.
  5. Setup XtQuant environment and dependencies

    main

    XtQuant is a Python strategy execution framework derived from MiniQMT. To use it, you must satisfy the following requirements:

    1. MiniQMT Client: You must have the MiniQMT client running before starting any XtQuant program.
    2. Python Versions: The library supports Python 3.6, 3.7, and 3.8. The library automatically switches versions during import based on the environment.
    3. Data Path: You need the userdata_mini path located within your MiniQMT client installation directory.
  6. XtData Request Limits and Best Practices

    main

    To ensure performance and avoid hitting limits, follow these guidelines:

    • High-Frequency/Large Scale Subscriptions: If you need to monitor a large number of stocks, do not use individual stock subscriptions. Instead, use Full Push Data (全推数据), which provides a cross-section of all market contracts. This is more efficient in terms of traffic and processing for high-subscription scenarios.
    • Single Stock Subscription Limit: It is recommended to keep individual stock subscriptions (subscribe_XXX) to no more than 50 stocks. If you need more, switch to Full Push Data.
    • Static Data Updates: Information like sector classifications and industry categories changes infrequently. Do not download these frequently; update them on a weekly or daily basis instead.
  7. Create a trading strategy with XtQuantTrader

    main

    To build a trading strategy, you need to implement a callback class inheriting from XtQuantTraderCallback to handle real-time market and trade events, then initialize the XtQuantTrader with your client path and a unique session_id.

    #coding=utf-8
    from xtquant.xttrader import XtQuantTrader, XtQuantTraderCallback
    from xtquant.xtquant import StockAccount
    from xtquant import xtconstant
    
    class MyXtQuantTraderCallback(XtQuantTraderCallback):
        def on_disconnected(self):
            print("connection lost")
        def on_stock_order(self, order):
            print("on order callback:", order.stock_code, order.order_status, order.order_sysid)
        def on_stock_asset(self, asset):
            print("on asset callback", asset.account_id, asset.cash, asset.total_asset)
        def on_stock_trade(self, trade):
            print("on trade callback", trade.account_id, trade.stock_code, trade.order_id)
        def on_stock_position(self, position):
            print("on position callback", position.stock_code, position.volume)
        def on_order_error(self, order_error):
            print("on order_error callback", order_error.order_id, order_error.error_id, order_error.error_msg)
        def on_cancel_error(self, cancel_error):
            print("on cancel_error callback", cancel_error.order_id, cancel_error.error_id, cancel_error.error_msg)
        def on_order_stock_async_response(self, response):
            print("on_order_stock_async_response", response.account_id, response.order_id, response.seq)
    
    if __name__ == "__main__":
        path = 'D:\\迅投极速交易终端 睿智融科版\\userdata_mini'
        session_id = 123456
        xt_trader = XtQuantTrader(path, session_id)
        acc = StockAccount('1000000365')
        callback = MyXtQuantTraderCallback()
        xt_trader.register_callback(callback)
        xt_trader.start()
        if xt_trader.connect() == 0:
            xt_trader.subscribe(acc)
            # ... trading logic ...
            xt_trader.run_forever()
  8. Perform quantitative strategy research with XtQuant

    main

    XtQuant enables advanced quantitative research workflows:

    • News Event Quantization: Using deep learning (e.g., BERT sequence modeling) to analyze news event sequences and predict industry index movements.
    • Technical Feature Extraction: Utilizing pandas_ta (a high-level technical analysis tool based on pandas and ta-lib) to automatically extract over 130 technical indicators (e.g., MACD, RSI, KDJ, Bollinger Bands) and candle patterns.
    • Cross-Market Linkage: Analyzing correlations between different markets (e.g., using US market technical factors to predict A-share industry movements) using machine learning.
  9. Set up the XtQuantTrader lifecycle (Connect, Start, Stop)

    main

    Follow these steps to prepare the trading environment:

    1. Register Callbacks: Use register_callback(callback) to attach an instance of XtQuantTraderCallback to receive real-time updates.
    2. Start Environment: Call start() to launch the trading thread.
    3. Connect: Call connect() to establish a connection to MiniQMT. This is a one-time connection; if it disconnects, you must call it again manually. It returns 0 on success.
    4. Run/Wait: Use run_forever() to block the current thread and wait for events until stop() is called.
    5. Stop: Call stop() to shut down the API.
    # 1. Register callback
    callback = MyXtQuantTraderCallback()
    xt_trader.register_callback(callback)
    
    # 2. Prepare environment
    xt_trader.start()
    
    # 3. Connect
    connect_result = xt_trader.connect()
    if connect_result == 0:
        print("Connected successfully")
    
    # 4. Keep running
    xt_trader.run_forever()
    
    # 5. Stop
    xt_trader.stop()
  10. Convert Millisecond Timestamps to String Format

    main

    When working with market data, you may need to convert millisecond timestamps into the specific string format used by the system (e.g., YYYYMMDDHHMMSS.mmm).

    Use the following logic to perform the conversion:

    import time
    
    def conv_time(ct):
        '''
        conv_time(1476374400000) --> '20161014000000.000'
        '''
        local_time = time.localtime(ct / 1000)
        data_head = time.strftime('%Y%m%d%H%M%S', local_time)
        data_secs = (ct - int(ct)) * 1000
        time_stamp = '%s.%03d' % (data_head, data_secs)
        return time_stamp
  11. Get and download financial data

    main

    Access corporate financial statements using the following methods:

    Retrieval:

    • get_financial_data(stock_list, table_list, start_time, end_time, report_type)
      • table_list supports: 'Balance' (Balance Sheet), 'Income' (Income Statement), 'CashFlow' (Cash Flow Statement).
      • report_type supports: 'report_time' (cutoff date) or 'announce_time' (disclosure date).
      • Returns a dict where keys are stock codes and values are dicts containing pd.DataFrames for each requested table.

    Downloading:

    • download_financial_data(stock_list, table_list): A synchronous operation to download financial data to the local environment.
    # Get Income Statement data
    financials = get_financial_data(
        stock_list=['600000.SH'], 
        table_list=['Income'], 
        report_type='report_time'
    )
    
    # Download financial data
    download_financial_data(stock_list=['600000.SH'], table_list=['Balance', 'Income'])
  12. Initialize the XtQuantTrader API instance

    main

    To interact with the MiniQMT client, you must first create an instance of XtQuantTrader. This requires the full path to the userdata_mini directory within your MiniQMT installation and a unique session_id. Different Python strategies should use different session_id values to avoid conflicts.

    path = 'D:\迅投极速交易终端 睿智融科版\userdata_mini'
    # session_id must be unique for different Python strategies
    session_id = 123456
    xt_trader = XtQuantTrader(path, session_id)