Get Contracts

Contract Introduction

A contract refers to the trading object or underlying asset (such as a stock or option). Contracts are uniformly formulated by exchanges. For example, to purchase the SPDR S&P 500 ETF, you can uniquely identify it through the symbol "SPY" and market information (i.e., market='US', US market). Through contract information, we can uniquely determine an underlying asset when placing orders or obtaining market data. Common contracts include stock contracts, option contracts, etc.

Most contracts (such as stocks, CFDs, indices) can be uniquely determined by the following four basic attributes:

  • Symbol: Generally, US and UK stock contract codes are English letters
  • Security Type: Common contract types include STK (stock), OPT (option)
  • Currency: Common currencies include USD (US Dollar)
  • Exchange: STK type contracts generally don't use the exchange field, orders are automatically routed

Some contracts (such as options) require additional information to uniquely identify them due to their more complex nature.

Here are several common contract types and their constituent elements:

Stock

contract = Contract()
contract.symbol ="SPY"
contract.sec_type ="STK"
contract.currency ="USD" #not required
contract.market = "US" #not required

Options

TradeUP API's option contracts support two methods:

  • One is the four-element method: symbol (stock code), expiry (option expiration date), strike (option strike price), right (option direction).

  • The other is the standard OCC option contract format with a fixed length of 21 characters, including four parts:

    • Related stock or ETF code, fixed at six characters, padded with spaces if insufficient
    • Option expiration date, 6 digits, format: yymmdd
    • Option type, value is P or C, representing put or call
    • Option strike price, value is price x 1000, fixed at 8 digits, padded with zeros if insufficient



get_contract Get Single Contract Information

TradeClient.get_contract(symbol, sec_type=SecurityType.STK, currency=None, exchange=None, expiry=None, strike=None, put_call=None)

Description

Query single contract information required for trading

Parameters

ParameterTypeRequiredDescription
symbolstrYesStock symbol
sec_typeSecurityTypeNoSecurity type, tigeropen.common.consts.SecurityType enum, Default: SecurityType.STK
currencyCurrencyNoCurrency, tigeropen.common.consts.Currency enum, e.g., Currency.USD
exchangestrNoExchange, optional, e.g., 'CBOE'
expirystrNoContract expiration date (for options), format yyyyMMdd, e.g., '20220130'
strikefloatNoStrike price (for options)
put_callstrNoPut/Call (for options), 'PUT' for put, 'CALL' for call

Return

tigeropen.trade.domain.contract.Contract contract object, see Object Introduction. Common attributes:

Object Attributes

AttributeTypeDescription
identifierstrUnique identifier, stock identifier same as symbol
symbolstrStock symbol, option contract symbol is the underlying symbol
sec_typestrSTK stock/OPT option/IOPT CBBC, etc., default STK
namestrContract name
currencystrCurrency, e.g., USD
exchangestrExchange
expirystrOptions only, option expiration date
strikefloatOptions only, option strike price
multiplierfloatMultiplier, quantity per lot
put_callstrOptions only, option direction, CALL or PUT
short_marginfloatShort margin ratio (deprecated, use short_initial_margin instead)
short_initial_marginfloatShort initial margin ratio
short_maintenance_marginfloatShort maintenance margin ratio (prime accounts have values)
short_fee_ratefloatShort fee rate
shortableboolWhether shortable
shortable_countintShort pool remaining
long_initial_marginfloatLong initial margin
long_maintenance_marginfloatLong maintenance margin
contract_monthstrContract month, e.g., 202201 for January 2022
primary_exchangestrStock listing exchange
marginableboolWhether marginable
marketstrMarket, e.g., US
min_tickfloatMinimum tick size
tickSizes[{"begin":"0","end":"1","tickSize":1.0E-4,"type":"CLOSED"},{"begin":"1","end":"Infinity","tickSize":0.01,"type":"OPEN"}]Minimum tick size price ranges, when order price is in begin-end range, must meet tickSize requirement. begin: left price range, end: right price range, type: range type OPEN/OPEN_CLOSED/CLOSED/CLOSED_OPEN, tickSize: minimum price unit
trading_classstrContract trading class name
close_onlyboolWhether close-only
statusstrContract status
tradeboolWhether tradable
is_etfboolWhether ETF
etf_leverageintETF leverage ratio, only exists when contract is ETF
lot_sizefloatMinimum tradable asset quantity in single transaction
support_overnight_tradingboolWhether supports overnight trading
support_fractional_shareboolWhether supports fractional share trading (prime/paper trading accounts only)
📘

Note

print only displays partial attributes, use print(contract.to_str()) to print all attributes

Example

from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import TigerOpenClientConfig
client_config = TigerOpenClientConfig(props_path='/path/to/your/properties/file/')

trade_client = TradeClient(client_config)

# Stock
contract = trade_client.get_contract('XYZX', sec_type=SecurityType.STK)
# contract = trade_client.get_contract('ES2306', sec_type=SecurityType.STK)
print(contract)
# By default only prints contract symbol/type/currency. Use to_str to print all attributes. For more attributes, specify attribute name directly, e.g., contract.short_margin
print(contract.to_str())

# View short initial margin
print(contract.short_initial_margin)

Return Example

{'contract_id': 1916, 'symbol': 'XYZX', 'currency': 'USD', 'sec_type': 'STK', 'exchange': None, 'origin_symbol': None, 
'local_symbol': 'XYZX', 'expiry': None, 'strike': None, 'put_call': None, 'multiplier': 1.0, 'name': 'Apple Inc', 
'short_margin': 0.35, 'short_initial_margin': 0.35, 'short_maintenance_margin': 0.3, 'short_fee_rate': None, 
'shortable': True, 'shortable_count': None, 'long_initial_margin': 0.3, 'long_maintenance_margin': 0.25, 
'contract_month': None, 'identifier': 'XYZX', 'primary_exchange': 'NASDAQ', 'market': 'US', 'min_tick': None, 
'tick_sizes': [{'begin': '0', 'end': '1', 'type': 'CLOSED', 'tick_size': 0.0001}, {'begin': '1', 'end': 'Infinity', 
'type': 'OPEN', 'tick_size': 0.01}], 'trading_class': 'XYZX', 'status': 1, 'marginable': True, 'trade': True, 
'close_only': False, 'continuous': None, 'last_trading_date': None, 'first_notice_date': None, 
'last_bidding_close_time': None, 'is_etf': False, 'etf_leverage': None, 'discounted_day_initial_margin': None, 
'discounted_day_maintenance_margin': None, 'discounted_time_zone_code': None, 'discounted_start_at': None, 
'discounted_end_at': None, 'categories': None, 'lot_size': 1.0, 'support_overnight_trading': True}

get_contracts Get Multiple Contract Information

TradeClient.get_contracts(symbol, sec_type=SecurityType.STK, currency=None, exchange=None):

Description

Query multiple contract information required for trading, returned as a list

Parameters

ParameterTypeRequiredDescription
symbolstrYesStock symbol, e.g., ''. Maximum 50 symbols per request
sec_typeSecurityTypeNoSecurity type, tigeropen.common.consts.SecurityType enum, Default: SecurityType.STK
currencyCurrencyNoCurrency, tigeropen.common.consts.Currency enum, e.g., Currency.USD
exchangestrNoExchange, optional, e.g., 'CBOE'

Return

list

Each item in the list is a contract object (tigeropen.trade.domain.contract.Contract), see Object Introduction. Common attributes:

Object Attributes

AttributeDescription
identifierUnique identifier, stock identifier same as symbol, option is 21-character identifier
symbolStock symbol, option contract symbol is the underlying symbol
sec_typeSTK stock/OPT option/IOPT CBBC, etc., default STK
nameContract name
currencyCurrency, e.g., USD
exchangeExchange
expiryOptions and, option expiration date
strikeOptions only, option strike price
multiplierMultiplier, quantity per lot
put_callOptions only, option direction, CALL or PUT
short_marginShort margin ratio (deprecated, use short_initial_margin instead)
short_initial_marginShort initial margin ratio
short_maintenance_marginShort maintenance margin ratio (prime accounts have values)
short_fee_rateShort fee rate
shortableShort pool remaining
long_initial_marginLong initial margin
long_maintenance_marginLong maintenance margin
contract_monthContract month, e.g., 202201 for January 2022
primary_exchangeStock listing exchange
marketMarket, e.g., US
min_tickMinimum tick size
tickSizesStock only, minimum tick size price ranges, when order price is in begin-end range, must meet tickSize requirement. begin: left price range, end: right price range, type: range type OPEN/OPEN_CLOSED/CLOSED/CLOSED_OPEN, tickSize: minimum price unit
trading_classContract trading class name
statusContract status
is_etfWhether ETF
etf_leverageETF leverage ratio, only exists when contract is ETF
📘

Note

print only displays partial attributes, use print(contract.to_str()) to print all attributes

Example

from tigeropen.trade.trade_client import TradeClient
from tigeropen.tiger_open_config import TigerOpenClientConfig
client_config = TigerOpenClientConfig(props_path='/path/to/your/properties/file/')

trade_client = TradeClient(client_config)

contracts = trade_client.get_contracts('XYZX', sec_type=SecurityType.STK)
print(contracts)
print(contracts[0].to_str())

Return Example

[
{'contract_id': 1916, 'symbol': 'XYZX', 'currency': 'USD', 'sec_type': 'STK', 'exchange': None, 'origin_symbol': None, 
 'local_symbol': 'XYZX', 'expiry': None, 'strike': None, 'put_call': None, 'multiplier': 1.0, 'name': 'Apple Inc', 
 'short_margin': None, 'short_initial_margin': None, 'short_maintenance_margin': None, 'short_fee_rate': None, 
 'shortable': None, 'shortable_count': None, 'long_initial_margin': None, 'long_maintenance_margin': None, 
 'contract_month': None, 'identifier': 'XYZX', 'primary_exchange': None, 'market': 'US', 'min_tick': None, 
 'tick_sizes': [{'begin': '0', 'end': '1', 'type': 'CLOSED', 'tick_size': 0.0001}, {'begin': '1', 'end': 'Infinity', 
 'type': 'OPEN', 'tick_size': 0.01}], 'trading_class': 'XYZX', 'status': 1, 'marginable': None, 'trade': True, 
 'close_only': False, 'continuous': None, 'last_trading_date': None, 'first_notice_date': None, 
 'last_bidding_close_time': None, 'is_etf': False, 'etf_leverage': None, 'discounted_day_initial_margin': None, 
 'discounted_day_maintenance_margin': None, 'discounted_time_zone_code': None, 'discounted_start_at': None, 
 'discounted_end_at': None, 'categories': None, 'lot_size': 1.0, 'support_overnight_trading': True}]

Create Contract Objects Locally

Stock

from tigeropen.common.util.contract_utils import stock_contract

# US Stock
contract = stock_contract(symbol='XYZX', currency='USD')

Options

from tigeropen.common.util.contract_utils import option_contract, option_contract_by_symbol
contract = option_contract(identifier='XYZX  190118P00160000')
# or
contract = option_contract_by_symbol('XYZX', '20200110', strike=280.0, put_call='PUT', currency='USD')

# Convert between option symbol and four elements
from tigeropen.common.util.contract_utils import extract_option_info, get_option_identifier

# Create option symbol from four elements
underlying_symbol='XYZX'
expiry='20200110'
put_call='PUT'
strike=280
identifier = get_option_identifier(underlying_symbol, expiry, put_call, strike)
# Extract four elements from option symbol identifier='XYZX  190118P00160000'
symbol, expiry, put_call, strike = extract_option_info(identifier)
print(identifier)