futu-api Documentation

repository·master·Indexed 23 days ago

https://github.com/futunnopen/py-futu-api

An open-source Python library providing market data and trading interfaces for quantitative investment via the Futu OpenAPI. It supports high-frequency streaming and low-frequency data retrieval through OpenQuoteContext, as well as Hong Kong stock trading via OpenHKTradeContext. Requires the FutuOpenD gateway client to be running.

Tokens
980
Snippets
2
Records
6
Agent score
30%

What's inside futu-api

  1. How to use OpenQuoteContext for market data

    master

    To access market data (quotes, K-lines, order books, etc.), instantiate an OpenQuoteContext object. You must provide the host and port where your FutuOpenD gateway is running.

    Workflow:

    1. Instantiate ft.OpenQuoteContext(host="...", port=...).
    2. Call .start() to begin asynchronous data reception.
    3. Use .set_handler() with a subclass of ft.TickerHandlerBase() to process incoming data asynchronously.
    4. Use low-frequency methods (e.g., get_market_snapshot) or high-frequency methods (e.g., get_rt_ticker) to fetch data.
    5. Call .stop() to stop receiving data and .close() to release resources.

    Note: You must have the FutuOpenD gateway client running before executing your Python script.

  2. How to use OpenHKTradeContext for Hong Kong stock trading

    master

    To perform trading operations for Hong Kong stocks, use the OpenHKTradeContext.

    Workflow:

    1. Instantiate ft.OpenHKTradeContext(host="...", port=...).
    2. Call .unlock_trade(password='...') to authorize trading.
    3. Execute trading commands like place_order, accinfo_query, or order_list_query. Use ft.TrdEnv.SIMULATE for testing in a simulated environment.
    4. Call .close() to release resources.

    Note: Ensure you are using the correct environment (ft.TrdEnv.SIMULATE vs real trading) to avoid unintended orders.

    import futu as ft
    
    # 1. Instantiate context
    trade_hk_ctx = ft.OpenHKTradeContext(host="127.0.0.1", port=11111)
    
    # 2. Unlock and Trade
    code = 'HK.00123'
    trade_hk_ctx.unlock_trade(password='123456')
    
    # Place a simulated order
    trade_hk_ctx.place_order(
        price=1.1, 
        qty=2000, 
        code=code, 
        trd_side=ft.TrdSide.BUY, 
        order_type=ft.OrderType.NORMAL, 
        trd_env=ft.TrdEnv.SIMULATE
    )
    
    # 3. Cleanup
    trade_hk_ctx.close()
  3. Reference: OpenQuoteContext high-frequency data methods

    master

    The following methods are available on an OpenQuoteContext instance for retrieving real-time or high-frequency data:

    • subscribe(code, sub_types): Subscribe to specific data types (e.g., ft.SubType.QUOTE, ft.SubType.TICKER, ft.SubType.K_DAY, ft.SubType.ORDER_BOOK, ft.SubType.RT_DATA, ft.SubType.BROKER).
    • get_stock_quote(code): Get stock quote.
    • get_rt_ticker(code): Get real-time ticker (逐笔).
    • get_cur_kline(code, num=100, ktype=ft.KLType.K_DAY): Get current K-line data.
    • get_order_book(code): Get order book (摆盘).
    • get_rt_data(code): Get real-time time-series data (分时数据).
    • get_broker_queue(code): Get broker queue (经纪队列).
  4. Reference: OpenQuoteContext low-frequency data methods

    master

    The following methods are available on an OpenQuoteContext instance for retrieving non-streaming market data:

    • request_trading_days(market, start=None, end=None): Get trading days.
    • get_stock_basicinfo(market, stock_type=ft.SecurityType.STOCK): Get basic stock information.
    • get_market_snapshot(code_list): Get market snapshots for a list of codes.
    • get_plate_list(market, ft.Plate.ALL): Get sub-plate lists under a market.
    • get_plate_stock(plate): Get the list of stocks under a specific plate.
  5. Reference: OpenHKTradeContext trading methods

    master

    The following methods are available on an OpenHKTradeContext instance:

    • unlock_trade(password): Unlock the trading interface.
    • accinfo_query(trd_env=ft.TrdEnv.SIMULATE): Query account information.
    • place_order(price, qty, code, trd_side, order_type, trd_env): Place an order.
    • order_list_query(trd_env=ft.TrdEnv.SIMULATE): Query order list.
    • position_list_query(trd_env=ft.TrdEnv.SIMULATE): Query position list.