TigerOpen Python SDK

repository·master·Indexed 41 days ago

https://github.com/tigerfintech/openapi-python-sdk

Official Python SDK for Tiger Brokers, providing a unified interface for market data, trading, account management, and real-time data streaming. Features include QuoteClient for market data, TradeClient for order execution, PushClient for WebSocket-based real-time pushes, and a CLI tool. Also supports AI integration via a Model Context Protocol (MCP) Server for tools like Cursor, Claude Code, and Trae.

Tokens
11.2K
Snippets
55
Records
60
Agent score
100%

What's inside TigerOpen

  1. Configure the TigerOpen SDK

    master

    Before using the SDK, you must obtain your tiger_id, private_key (must be in PKCS#1 format), and account from the Developer Portal.

    Option A: Using a properties file

    Create a file named tiger_openapi_config.properties with the following content:

    tiger_id=your_tiger_id
    private_key_pk1=your_pkcs1_private_key
    account=your_account

    Then initialize the config in Python:

    from tigeropen.tiger_open_config import TigerOpenClientConfig
    config = TigerOpenClientConfig(props_path='~/.tigeropen/')

    Option B: Direct configuration in code

    from tigeropen.tiger_open_config import TigerOpenClientConfig
    
    config = TigerOpenClientConfig()
    config.tiger_id = 'your_tiger_id'
    config.account = 'your_account'
    config.private_key = 'your_private_key_content'
    from tigeropen.tiger_open_config import TigerOpenClientConfig
    
    config = TigerOpenClientConfig()
    config.tiger_id = 'your_tiger_id'
    config.account = 'your_account'
    config.private_key = 'your_private_key_content'
  2. Configure the TigerOpen Client

    master

    Before using the SDK, you must register as a developer at the Developer Information Page to obtain your tiger_id, private_key (RSA PKCS#1 format), and account number.

    There are two ways to configure the client:

    Method 1: Configuration File

    Create a file named tiger_openapi_config.properties (e.g., in ~/.tigeropen/) with the following content:

    tiger_id=your_tiger_id
    private_key_pk1=your_pkcs1_private_key
    account=your_account

    Then initialize the config in Python:

    from tigeropen.tiger_open_config import TigerOpenClientConfig
    config = TigerOpenClientConfig(props_path='~/.tigeropen/')

    Method 2: Direct Code Configuration

    Pass credentials directly to the TigerOpenClientConfig object:

    from tigeropen.tiger_open_config import TigerOpenClientConfig
    
    config = TigerOpenClientConfig()
    config.tiger_id = 'your_tiger_id'
    config.account = 'your_account'
    config.private_key = 'your_private_key_content'
    from tigeropen.tiger_open_config import TigerOpenClientConfig
    
    config = TigerOpenClientConfig()
    config.tiger_id = 'your_tiger_id'
    config.account = 'your_account'
    config.private_key = 'your_private_key_content'
  3. Install the Tiger MCP Server using uv

    master

    The Tiger MCP Server is managed via uv, a Python package and project manager. First, install uv on your system:

    macOS/Linux:

    curl -LsSf https://astral.sh/uv/install.sh | sh

    Windows (PowerShell as Administrator):

    powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

    Once uv is installed, you can run the server using uvx.

    curl -LsSf https://astral.sh/uv/install.sh | sh
  4. Integrate TigerOpen with MCP Server (AI Integration)

    master

    The Model Context Protocol (MCP) Server allows AI tools like Cursor, Claude Code, and Trae to perform market queries and trade management via natural language.

    Installation

    First, ensure uv is installed:

    pip install uv

    Configuration

    Add the following to your AI tool's MCP settings:

    For Personal Accounts:

    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account"
          }
        }
      }
    }

    For Institutional Accounts:

    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account",
            "TIGEROPEN_SECRET_KEY": "your_secret_key",
            "TIGEROPEN_TOKEN": "your_token"
          }
        }
      }
    }

    Note:

    • To enable Read-only mode (disables order placement), add "TIGERMCP_READONLY": "true" to the env block.
    • macOS 12 users may need to install coreutils (brew install coreutils) if encountering realpath errors.
    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account"
          }
        }
      }
    }
  5. Generate Python protobuf files from proto definitions

    master

    To generate the Python implementation (including .py and .pyi type stub files) from the protobuf definitions located in tigeropen/push/pb/, use the protoc compiler from the root of the openapi-python-sdk directory. This process ensures that the Python SDK is synchronized with the latest protobuf schema definitions.

    cd openapi-python-sdk
    protoc  --proto_path=.  --python_out=.  --pyi_out=. tigeropen/push/pb/*.proto
  6. Integrate Tiger MCP with Cursor, Claude, or Trae

    master

    To use the Tiger MCP Server within AI IDEs like Cursor, Claude, or Trae, add the following configuration to your MCP settings JSON. Ensure you provide the correct path to your properties file in the env section.

    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_PROPS_PATH": "/path/to/your/tiger_openapi_config.properties"
          }
        }
      }
    }
    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_PROPS_PATH": "/path/to/your/tiger_openapi_config.properties"
          }
        }
      }
    }
  7. Configure TigerOpen API credentials for Tiger MCP

    master

    The Tiger MCP Server requires TigerOpen API credentials to function. You can provide these in two ways:

    Set the TIGEROPEN_PROPS_PATH environment variable to the absolute path of your tiger_openapi_config.properties file (which should contain your tiger_id, private_key, and account information).

    Option 2: Using direct environment variables

    Set the following variables directly:

    • TIGEROPEN_TIGER_ID: Your Tiger ID
    • TIGEROPEN_PRIVATE_KEY: Your private key
    • TIGEROPEN_ACCOUNT: Your trading account

    Read-Only Mode

    To prevent the server from performing any trading actions, set the TIGERMCP_READONLY environment variable to true.

    export TIGEROPEN_PROPS_PATH="path/to/your/tiger_openapi_config.properties"
    # OR
    export TIGEROPEN_TIGER_ID="your Tiger ID"
    export TIGEROPEN_PRIVATE_KEY="your private key"
    export TIGEROPEN_ACCOUNT="your trading account"
    # For read-only mode
    export TIGERMCP_READONLY="true"
  8. Install the TigerOpen Python SDK

    master

    The TigerOpen Python SDK requires Python 3.8+. You can install it using several methods:

    One-click Installation

    Automatically detects your package manager (uv, pipx, or pip):

    curl -fsSL https://raw.githubusercontent.com/tigerfintech/openapi-python-sdk/master/install.sh | sh

    Manual Installation

    • Using uv (Recommended):
      uv pip install tigeropen
    • Using pip:
      pip install tigeropen
    • Using Conda:
      conda install -c conda-forge tigeropen

    From Source

    git clone https://github.com/tigerfintech/openapi-python-sdk.git
    cd openapi-python-sdk
    pip install -e .

    Uninstall

    tigeropen uninstall
    # 一键安装(自动检测 uv/pipx/pip)
    curl -fsSL https://raw.githubusercontent.com/tigerfintech/openapi-python-sdk/master/install.sh | sh
  9. Install TigerOpen AI Skills for Claude Code

    master

    TigerOpen provides skill packs for AI programming tools following the Agent Skills standard. These provide expert knowledge of the Tiger OpenAPI to tools like Claude Code.

    Option 1: Claude Code Plugin Marketplace

    claude install-plugin tigerfintech/openapi-python-sdk/tigeropen/examples/ai/skills

    Option 2: Global installation

    cp -r tigeropen/examples/ai/skills/ ~/.claude/skills/tigeropen/

    Option 3: Project-level installation

    cp -r tigeropen/examples/ai/skills/ .claude/skills/tigeropen/

    Available Skill Modules:

    • quickstart: Environment setup & SDK configuration
    • quote: Market data API guidance
    • trade: Trading API guidance
    • option: Options API guidance
    • push: Real-time push API guidance
    • mcp: MCP Server configuration
    claude install-plugin tigerfintech/openapi-python-sdk/tigeropen/examples/ai/skills
  10. Integrate TigerOpen with AI Tools via MCP Server

    master

    TigerOpen provides a Model Context Protocol (MCP) Server that allows AI coding tools like Cursor, Claude Code, or Trae to query market data and manage trades using natural language.

    Installation

    Ensure uv is installed, then use uvx to run the server.

    pip install uv

    Configuration for AI Tools

    Add the following to your tool's MCP configuration file:

    For Personal Accounts:

    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account"
          }
        }
      }
    }

    For Institutional Accounts:

    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account",
            "TIGEROPEN_SECRET_KEY": "your_secret_key",
            "TIGEROPEN_TOKEN": "your_token"
          }
        }
      }
    }

    Tips

    • Read-Only Mode: To prevent the AI from placing trades, add "TIGERMCP_READONLY": "true" to the env block.
    • macOS Users: If you encounter realpath errors, install coreutils via brew install coreutils.
    {
      "mcpServers": {
        "tigermcp": {
          "command": "uvx",
          "args": ["tigermcp"],
          "env": {
            "TIGEROPEN_TIGER_ID": "your_tiger_id",
            "TIGEROPEN_PRIVATE_KEY": "your_private_key",
            "TIGEROPEN_ACCOUNT": "your_account"
          }
        }
      }
    }
  11. Automatic token refresh and monitoring

    master

    If configured, TigerOpenClient manages token lifecycles automatically:

    1. Background Refresh: If token_refresh_duration is non-zero, the client starts a background RepeatTimer thread that periodically checks if the token needs refreshing.
    2. Token File Monitoring: To support multi-process environments, the client can monitor the token file for changes. If one process refreshes the token and saves it to the file, other processes will detect the change and update their internal state.

    Requirement for Monitoring: To enable file monitoring, you must install the watchdog package:

    pip install watchdog