Other Examples

About 4 min

Other example

Additional examples of the Python SDK are constantly being updated and will be synced to the Github code repository as they become available.

Get historical K-lines in bulk

import datetime
import time
import unittest
import pandas as pd
import pytz

from tigeropen.common.consts import BarPeriod
from tigeropen.quote.quote_client import QuoteClient
from tigeropen.tiger_open_config import get_client_config

pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 100)
pd.set_option('display.width', 1000)

client_config = get_client_config(private_key_path='your private key path',
                                  tiger_id='your tiger id',
                                  account='your account'
                                  )

quote_client = QuoteClient(client_config)

SYMBOL = "symbol"
TIME = "time"
CLOSE = "close"
DATE = "date"

# total number of requested bars.
BARS_TOTAL_NUMBER = 2000

# number of bars per request, the system limits each symbol to a maximum of 1200 bars per request.
BARS_BATCH_SIZE = 300

# number of symbols per request, the system limits each request to a maximum of 50 symbols.
SYMBOLS_BATCH_SIZE = 50

# The interval between each request, to prevent requests too fast to trigger the system rate limit. Time unit: second
REQUEST_INTERVAL = 0.5


class QuoteExamples(unittest.TestCase):
    def setUp(self) -> None:
        print(f'quote permissions:{quote_client.grab_quote_permission()}')

    def test_get_history(self):
        """
        Example of a batch request for historical data
        The processed historical data prints out like this:

        all history:
                                                           time    open    high     low   close  volume
        date                      symbol
        2022-02-11 15:41:00+08:00 00700   1644565260000  476.60  477.20  476.60  477.20   49200
                                  01810   1644565260000   16.56   16.60   16.56   16.56  408000
        2022-02-11 15:42:00+08:00 00700   1644565320000  477.00  477.20  476.20  476.20   95500
                                  01810   1644565320000   16.56   16.58   16.54   16.56  199200
        2022-02-11 15:43:00+08:00 00700   1644565380000  476.20  476.40  475.40  475.40   73110
        ...                                         ...     ...     ...     ...     ...     ...
        2022-02-22 11:08:00+08:00 01810   1645499280000   15.62   15.62   15.60   15.62  128400
        2022-02-22 11:09:00+08:00 00700   1645499340000  434.80  435.00  434.20  434.60   83988
                                  01810   1645499340000   15.60   15.64   15.60   15.62  243400
        2022-02-22 11:10:00+08:00 00700   1645499400000  434.60  434.80  434.20  434.60   29399
                                  01810   1645499400000   15.60   15.62   15.58   15.62  234200

        close data:
        date                       symbol
        2022-02-14 09:40:00+08:00  00700     469.00
                                   01810      16.16
        2022-02-14 09:41:00+08:00  00700     468.60
                                   01810      16.06
        2022-02-14 09:42:00+08:00  00700     468.00
                                              ...
        2022-02-22 11:37:00+08:00  01810      15.64
        2022-02-22 11:38:00+08:00  00700     436.00
                                   01810      15.62
        2022-02-22 11:39:00+08:00  00700     436.00
                                   01810      15.64
        Name: close, Length: 4200, dtype: float64

        history of 00700:
                                     date           time   open   high    low  close  volume
        symbol
        00700  2022-02-14 09:40:00+08:00  1644802800000  470.0  470.2  468.6  469.0   61900
        00700  2022-02-14 09:41:00+08:00  1644802860000  469.0  469.4  468.4  468.6   87800
        00700  2022-02-14 09:42:00+08:00  1644802920000  468.6  468.6  467.6  468.0  131770
        00700  2022-02-14 09:43:00+08:00  1644802980000  468.0  468.8  467.8  468.4  115694
        00700  2022-02-14 09:44:00+08:00  1644803040000  468.8  468.8  468.4  468.6   29607
        ...                          ...            ...    ...    ...    ...    ...     ...
        00700  2022-02-22 11:35:00+08:00  1645500900000  436.4  436.4  436.0  436.0   39400
        00700  2022-02-22 11:36:00+08:00  1645500960000  436.2  436.2  435.8  436.0   47000
        00700  2022-02-22 11:37:00+08:00  1645501020000  436.0  436.4  435.8  436.2   40802
        00700  2022-02-22 11:38:00+08:00  1645501080000  436.2  436.4  436.0  436.0   43102
        00700  2022-02-22 11:39:00+08:00  1645501140000  436.0  436.2  436.0  436.0    1700

        [2100 rows x 7 columns]

        close of 00700:
        symbol
        00700    469.0
        00700    468.6
        00700    468.0
        00700    468.4
        00700    468.6
                 ...
        00700    436.0
        00700    436.0
        00700    436.2
        00700    436.0
        00700    436.0
        Name: close, Length: 2100, dtype: float64
        """
        # HK market
        symbol1 = '00700'
        symbols = [symbol1, '01810']
        timezone = 'Asia/Shanghai'

        # US market
        # symbol1 = 'AAPL'
        # symbols = [symbol1, 'TSLA']
        # timezone = 'US/Eastern'

        end = int(datetime.datetime.today().timestamp() * 1000)
        history = pd.DataFrame()
        for i in range(0, BARS_TOTAL_NUMBER, BARS_BATCH_SIZE):
            if i + BARS_BATCH_SIZE <= BARS_TOTAL_NUMBER:
                limit = BARS_BATCH_SIZE
            else:
                limit = i + BARS_BATCH_SIZE - BARS_TOTAL_NUMBER
            end_time = datetime.datetime.fromtimestamp(end/1000, pytz.timezone(timezone))
            print(f'query {len(symbols)} symobls history, end_time:{end} -- {end_time}, limit:{limit}')
            # This request is for the minute k line, for other periods, can change 'period' parameter
            part = self._request_bars(symbols=symbols, period=BarPeriod.ONE_MINUTE, end_time=end, bars_batch_size=BARS_BATCH_SIZE)
            part[DATE] = pd.to_datetime(part[TIME], unit='ms').dt.tz_localize('UTC').dt.tz_convert(timezone)
            end = min(part[TIME])
            history = history.append(part)
        history.set_index([DATE, SYMBOL], inplace=True)
        history.sort_index(inplace=True)
        print(f'all history:\n{history}')

        close_data = history[CLOSE]
        print(f'close data:\n{close_data}')

        hist_of_symbol1 = history.reset_index().set_index(SYMBOL).loc[symbol1]
        close_of_symbol1 = hist_of_symbol1[CLOSE]
        print(f'history of {symbol1}:\n {hist_of_symbol1}')
        print(f'close of {symbol1}:\n{close_of_symbol1}')
        return history

    @staticmethod
    def _request_bars(symbols, period, end_time, bars_batch_size):
        """
         Request history bars.
        :param symbols: like ['AAPL', 'TSLA']
        :param period: tigeropen.common.consts.BarPeriod. like BarPeriod.DAY
        :param end_time: end time in timestamp format. like 1645499400000
        :param bars_batch_size: bars limit size of each symbol
        :return:
        """
        symbols = list(symbols)
        result = pd.DataFrame()
        for i in range(0, len(symbols), SYMBOLS_BATCH_SIZE):
            part = symbols[i:i + SYMBOLS_BATCH_SIZE]
            quote = quote_client.get_bars(part, period=period, end_time=end_time, limit=bars_batch_size)
            result = result.append(quote)
            # to avoid rate limit
            time.sleep(REQUEST_INTERVAL)
        return result

Option Indicator Caculation Tool

SDK code path: tigeropen/examples/option_helpers/helpers.py , calculate Greek values and implied Volatility,also support option price predictions. this tool will depend on this library: quantlib,Need to install before use: pip install quantlib

Usage 1

FDAmericanDividendOptionHelper is American option calculation tool FDEuropeanDividendOptionHelper is European option calculation class

import quantlib as ql
from tigeropen.examples.option_helpers.helpers import FDAmericanDividendOptionHelper

# Calculate option prices based on implied volatility:
ql.Settings.instance().evaluationDate = ql.Date(19, 4, 2022)
helper = FDAmericanDividendOptionHelper(option_type=ql.Option.Call,  # PUT/CALL
                                        underlying=985,  # Settlement date stock price
                                        strike=990,      # strike price
                                        risk_free_rate=0.017,  # risk free rate
                                        dividend_rate=0,       # dividend rate
                                        volatility=0.6153,     # implied volatility
                                        settlement_date=ql.Date(14, 4, 2022),  # settlement date
                                        expiration_date=ql.Date(22, 4, 2022))  # option expiration date
print(f'value:{helper.NPV()}')
print(f'delta:{helper.delta()}')
print(f'gamma:{helper.gamma()}')
print(f'theta:{helper.theta()}')
print(f'vega:{helper.vega()}')
print(f'rho:{helper.rho()}')


# Calculation of implied volatility based on option prices:
ql.Settings.instance().evaluationDate = ql.Date(19, 4, 2022)
helper = FDAmericanDividendOptionHelper(option_type=ql.Option.Call,
                                        underlying=985,
                                        strike=990,
                                        risk_free_rate=0.017,
                                        dividend_rate=0,
                                        volatility=0, # implied volatility
                                        settlement_date=ql.Date(14, 4, 2022),
                                        expiration_date=ql.Date(22, 4, 2022))
# calculation implied volatility
volatility = helper.implied_volatility(33.6148)
helper.update_implied_volatility(volatility)

print(f'implied volatility:{volatility}')
print(f'value:{helper.NPV()}')
print(f'delta:{helper.delta()}')
print(f'gamma:{helper.gamma()}')
print(f'theta:{helper.theta()}')
print(f'vega:{helper.vega()}')
print(f'rho:{helper.rho()}')

Usage 2

# calculation option price
python helpers.py -t PUT -e '2022-05-20' -s 2022-04-24 -p 215 -u 215.52 -r 0.0078 -v 0.5919

# Calculate implied volatility based on option prices. -n option price
python helpers.py -t CALL -e '2022-04-22' -s 2022-04-14 -p 990 -u 985 -r 0.017 -n 33.6148
# Calculate implied volatility from options intraday data.(use ask bid average price)
python helpers.py -t CALL -e '2022-04-22' -s 2022-04-14 -p 990 -u 985 -r 0.017 -a 35 -b 36

# help command
python helpers.py -h

Last update: