Preparation

About 5 min

Environment requirement

  • Operation system requirement:
    • Windows
    • MacOS
    • Linux
    • Solaris
  • Programming language: Python 3.4 or later

Install Python

To minimize compatibility issues, we recommend you to install the latest version of Python. Versions earlier than Python 3.4 may not work properly. You can type 'python3 -v' in the console for a version check or a installation verification.

Install latest version python:

  1. Visit the Download page of the Python website: https://www.python.org/downloads/open in new window

  2. Select the operating system, download the installation package, and install it as prompted

Install Tiger Open API Python SDK

Method 1:Use pip to install(Recommended)

In Terminal or CMD, enter the following command:

$ pip3 install tigeropen to install Tiger Open API Python SDK.

If need to upgrade the version, can use:

pip3 install tigeropen --upgrade

remark:
If pip official repository is slow, can change another mirror, like:

pip install tigeropen -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
pip3 install tigeropen -i https://pypi.tuna.tsinghua.edu.cn/ --trusted-host pypi.tuna.tsinghua.edu.cn

Method 2:Github

The source code of the OpenAPI SDK project is posted on Github,The Github address is:

https://github.com/tigerfintech/openapi-python-sdkopen in new window

How to install:

  1. clone the project into local dirctory

  2. run the setup.py in the directory, using the following command: python3 setup.py install

Install IDE

We recommend using pycharm as your IDE

Official Download Page:

https://www.jetbrains.com/pycharm/download/open in new window

Registered Developer Information

Before using the API, please first click the link to visit the official website of Tiger Quantification to open the permission and register the developer identity:https://developer.itigerup.com/profileopen in new windowPlease use Chrome for this page

To access the Open API, you need to Open a tiger account and Funded.

During the registration process, you will be asked to sign an API authorization agreement, after that, you need to fill in your prefered accoount configuration and click "update", you will need to fill in the following information:

TitleOptionalDescription
RSA public keyNOAuto generate by API Website.The RSA pulic key ensures the security of your account, by RSA two-way signature mechanism,the keys protect the API request from being illegally changed.
IP White ListYesOnly IP addresses in the white list can access Tiger API,use “;” to divide multiple IPs
Callback URLYescustomized callback URL of users's program, used for receive the message for update on order and account status, user can also use the callback function provided in the SDK to receive callback messages

You can obtain the following information upon sucessful registration:

  • tigerId: A unique Developer ID assgined by the OpenAPI platform. It is used to identify a developer,you have to pass in ID to use API.
  • account: Account number,you will need to pass in your account number when you are calling trade related APIs。There are 3 types of accounts: global account, standard account and paper account.
    • Global Account:a string beginning with capital letter U, example:U12300123,
    • Prime Account:number(5 to 10 digit), example:51230321,
    • Paper Account:17 digit number,example:20191106192858300,

Only active accounts with balance will be shown on the page, if an user has both active global account and standard account, they will both be shown on the developer info page.

Developer Register Page: Use your phone and verifyCode to register.

DeveloperRegister

Developer Info Page: Tiger ID,Live Account,Paper Account,License Info will be used in SDK.

DeveloperInfo

Attention:You need to keep the privateKey in the picture below locally and keep it properly to prevent leakage, please update it in time if you find leakage. The private key will not be saved on the Tiger server, users need to save it before refreshing the page. The private key will disappear automatically after the page is refreshed. If the private key is not saved, you can replace it by re-generate button.

Users can save the corresponding type of RSA privateKey according to different SDK call requirements.

RSAKey

RSA Keys

The Tiger Open API uses RSA for authentication. RSA encryption is used to secure user interface requests. The RSA bidirectional signature authentication mechanism is used to prevent interface requests from being maliciously tampered. The private key is saved locally by the user.

Attention:

  1. The Java private key format is different from that used by the Python SDK. The Python private key format is PKCS#1. If any problem occurs when you use the SDK, please check whether the private key is in the correct format first

Subscribing to Market Data (Optional)

We offer our users free access to delayed market data for the U.S market. Our customers from Mainland China will also get free Level 2 Quote Subscription for HK market. Live quotes and historical data are also available for a fee. Please note that Open API is an independent service from our APP, and API data need to be purchase seperately. Please follow the instructions below to subscribe market data for API:

Individuals

1.Login Personal Centeropen in new window to purchase quote.

QuotePurchase

2.Go to Tiger Trade APP - Profile- Market Data Store- OpenAPI and select from the subscription list shown on this page

Institutional user

Go to Tiger Brokers Institutional Account Center - Market Data and subscribe

QuotePurchase

Translation into English

Before initiating official API requests, you must complete the necessary configurations for API calls. Detailed configuration information (including tigerId, account, license, etc.) can be found on the developer information page.

There are two configuration methods:


Method 1

Using market data queries as an example, all market data operations are implemented through the member methods of the QuoteClient object. Therefore, before invoking the relevant market data interfaces, you need to initialize QuoteClient. The implementation process is as follows:

First, create a client_config object using the function get_client_config() as shown in the example below. Then, pass the client_config object to QuoteClient to initialize it. Finally, call specific methods of the QuoteClient. The initialization of TradeClient and PushClient follows a similar process.

CAUTION

In the example below, the read_private_key('Path to the private key PEM file') corresponds to a PEM file that needs to be generated manually. Copy the PKCS#1 format private key from the developer page to a local file, and then fill in the complete path (including the file name) here. For example: /data0/config/private_key.pem. Save the private key in the private_key.pem file.

from tigeropen.common.consts import (Language,        # Language
                                     Market,          # Market
                                     BarPeriod,       # K-line period
                                     QuoteRight)      # Adjustment type
from tigeropen.tiger_open_config import TigerOpenClientConfig
from tigeropen.common.util.signature_utils import read_private_key
from tigeropen.quote.quote_client import QuoteClient

def get_client_config():
    """
    https://quant.itigerup.com/#developer Retrieve developer information
    """
    client_config = TigerOpenClientConfig()
    # For HK Stock license, specify the token path using props_path, e.g., props_path='/Users/xxx/xxx/'
    # If not specified, it defaults to the current path
    # client_config = TigerOpenClientConfig(props_path='.')
    client_config.private_key = read_private_key('Path to the private key PEM file')
    client_config.tiger_id = 'Replace with your tigerId'
    client_config.account = 'Replace with your account, simulation account recommended'
    # For institutional accounts, add the secret key
    client_config.secret_key = 'Replace with your secret key'
    client_config.language = Language.zh_CN  # Optional, defaults to English if not specified
    # client_config.timezone = 'US/Eastern'  # Optional timezone setting
    return client_config

# Create the client_config object using the above-defined function
client_config = get_client_config()

# Initialize QuoteClient with the configuration object
quote_client = QuoteClient(client_config)

# Retrieve market data for ticker 00700
stock_price = quote_client.get_stock_briefs(['00700'])

Method 2

Using a configuration file.
Export the configuration file tiger_openapi_config.properties from the developer website and place it in an appropriate system path, such as /Users/demo/props/. Then, specify this path in the props_path parameter of TigerOpenClientConfig (or place the configuration file in the current program directory; the SDK will default to the current path).
Using this method eliminates the need to configure tiger_id, account, private_key, etc., in the code.

Additionally, for HK Stock licenses, the file tiger_openapi_token.properties is mandatory and must also be placed in the path specified by props_path.

from tigeropen.common.consts import (Language,        # Language
                                     Market,          # Market
                                     BarPeriod,       # K-line period
                                     QuoteRight)      # Adjustment type
from tigeropen.tiger_open_config import TigerOpenClientConfig

def get_client_config():
    """
    https://quant.itigerup.com/#developer Retrieve developer information
    """
    # For HK Stock license, specify the token path using props_path, e.g., '/Users/xxx/xxx/'
    # Use keyword arguments to specify props_path
    client_config = TigerOpenClientConfig(props_path='/Users/demo/props/')
    return client_config

# Create the client_config object using the above-defined function
client_config = get_client_config()

Introduction to Common ClientConfig Configuration Options

After instantiating client_config = TigerOpenClientConfig(), you can set configuration properties using client_config.<property>, such as client_config.timeout = 60.

# Developer information (props_path is the recommended method for configuring developer info)
client_config.tiger_id = 1
client_config.account = '123456'
client_config.license = 'TBSG'
client_config.private_key = read_private_key('Path to private key')  # Requires `from tigeropen.common.util.signature_utils import read_private_key`
# The private key can also be specified as a string
client_config.private_key = 'MIICWwIBAAKBgQCSW+.....PrivateKeyContents'

# Logging level and path
client_config.log_level = logging.DEBUG  # Requires `import logging`
client_config.log_path = '/tmp/tigerapi.log'

# Language
client_config.language = 'zh_CN'
# Timezone (If a timezone is set, time-related string parameters will be processed as this timezone. By default, SDK does not set a timezone, and the server assumes Beijing time.)
client_config.timezone = 'US/Eastern'

# API timeout
client_config.timeout = 15
# Retry settings
# Maximum retry duration in seconds
client_config.retry_max_time = 60
# Maximum number of retry attempts
client_config.retry_max_tries = 5

# 2FA token refresh interval in seconds. Set to 0 to disable automatic refresh
client_config.token_refresh_duration = 24 * 60 * 60
Last update: