Skip to content

Commit dacb61b

Browse files
author
gaoan
committed
add:sector api
1 parent e5983ae commit dacb61b

File tree

12 files changed

+267
-17
lines changed

12 files changed

+267
-17
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='tigeropen',
15-
version='1.1.10',
15+
version='1.2.0',
1616
description='TigerBrokers Open API',
1717
packages=find_packages(exclude=[]),
1818
author='TigerBrokers',

tigeropen/common/consts/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,14 @@ class CorporateActionType(Enum):
133133
SPLIT = 'split' # 拆合股
134134
DIVIDEND = 'dividend' # 分红
135135
EARNINGS_CALENDAR = 'earning' # 财报日历
136+
137+
138+
@unique
139+
class IndustryLevel(Enum):
140+
"""
141+
行业级别. 级别从1级到4级依次为: GSECTOR, GGROUP, GIND, GSUBIND
142+
"""
143+
GSECTOR = 'GSECTOR'
144+
GGROUP = 'GGROUP'
145+
GIND = 'GIND'
146+
GSUBIND = 'GSUBIND'

tigeropen/common/consts/service_types.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@
6565
FINANCIAL_DAILY = 'financial_daily'
6666
FINANCIAL_REPORT = 'financial_report'
6767
CORPORATE_ACTION = 'corporate_action'
68+
69+
# 行业数据
70+
INDUSTRY_LIST = 'industry_list'
71+
INDUSTRY_STOCKS = 'industry_stocks'
72+
STOCK_INDUSTRY = 'stock_industry'
73+

tigeropen/common/util/contract_utils.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
@author: gaoan
66
"""
77
import re
8+
9+
from tigeropen.common.consts import SecurityType
810
from tigeropen.trade.domain.contract import Contract
911

1012

@@ -13,7 +15,8 @@ def stock_contract(symbol, currency, local_symbol=None, exchange=None, contract_
1315
contract_id=contract_id)
1416

1517

16-
def option_contract_by_symbol(symbol, expiry, strike, put_call, currency, multiplier=100, local_symbol=None, contract_id=None):
18+
def option_contract_by_symbol(symbol, expiry, strike, put_call, currency, multiplier=100, local_symbol=None,
19+
contract_id=None):
1720
return Contract(symbol, currency, sec_type='OPT', expiry=expiry, strike=strike, put_call=put_call,
1821
multiplier=multiplier, local_symbol=local_symbol, contract_id=contract_id)
1922

@@ -26,7 +29,8 @@ def option_contract(identifier, multiplier=100, currency='USD'):
2629
multiplier=multiplier)
2730

2831

29-
def future_contract(symbol, currency, expiry=None, exchange=None, contract_month=None, multiplier=None, local_symbol=None):
32+
def future_contract(symbol, currency, expiry=None, exchange=None, contract_month=None, multiplier=None,
33+
local_symbol=None):
3034
return Contract(symbol, currency, sec_type='FUT', expiry=expiry, exchange=exchange, contract_month=contract_month,
3135
multiplier=multiplier, local_symbol=local_symbol)
3236

@@ -41,14 +45,28 @@ def cash_contract(symbol, currency, local_symbol=None):
4145
return Contract(symbol, currency, sec_type='CASH', local_symbol=local_symbol)
4246

4347

48+
def war_contract_by_symbol(symbol, expiry, strike, put_call, local_symbol, multiplier=100, currency='HKD',
49+
contract_id=None):
50+
"""港股窝轮"""
51+
return Contract(symbol, currency=currency, sec_type=SecurityType.WAR.value, expiry=expiry, strike=strike,
52+
put_call=put_call, local_symbol=local_symbol, multiplier=multiplier, contract_id=contract_id)
53+
54+
55+
def iopt_contract_by_symbol(symbol, expiry, strike, put_call, local_symbol, multiplier=100, currency='HKD',
56+
contract_id=None):
57+
"""港股牛熊证"""
58+
return Contract(symbol, currency=currency, sec_type=SecurityType.IOPT.value, expiry=expiry, strike=strike,
59+
put_call=put_call, local_symbol=local_symbol, multiplier=multiplier, contract_id=contract_id)
60+
61+
4462
def extract_option_info(identifier):
4563
"""
4664
从期权中提取 symbol, expiry 等信息
4765
:param identifier:
4866
:return:
4967
"""
5068
if identifier:
51-
tokens = re.findall(r'(\w+)\s*(\d{6})(C|P)(\d+)', identifier, re.M)
69+
tokens = re.findall(r'(\w+)\s*(\d{6})([CP])(\d+)', identifier, re.M)
5270
if len(tokens) == 1:
5371
underlying_symbol, expiry, put_call, strike = tokens[0]
5472
expiry = '20' + expiry

tigeropen/examples/client_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def get_client_config():
1818
client_config = TigerOpenClientConfig(sandbox_debug=is_sandbox)
1919
client_config.private_key = read_private_key('your private key file path')
2020
client_config.tiger_id = 'your tiger id'
21-
client_config.account = 'your account' # 环球账户
21+
client_config.account = 'your account' # 环球账户.
22+
# 只使用一个账户时,不论是环球账户, 标准账户或是模拟账户, 都填在 client_config.account 下, 默认只会使用这里的账户.
23+
# standard_account 属性和 paper_account 属性只是为多账户时取用方便, 一般可忽略
2224
client_config.standard_account = None # 标准账户
2325
client_config.paper_account = None # 模拟账户
2426
client_config.language = Language.en_US

tigeropen/examples/quote_client_demo.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import logging
88
import pandas as pd
99
from tigeropen.common.consts import Market, QuoteRight, FinancialReportPeriodType, Valuation, \
10-
Income, Balance, CashFlow, BalanceSheetRatio, Growth, Leverage, Profitability
10+
Income, Balance, CashFlow, BalanceSheetRatio, Growth, Leverage, Profitability, IndustryLevel
1111

1212
from tigeropen.quote.quote_client import QuoteClient
1313

@@ -108,6 +108,17 @@ def get_fundamental():
108108
earnings_calendar = openapi_client.get_corporate_earnings_calendar(Market.US, '2020-01-01', '2020-02-01')
109109
print(earnings_calendar)
110110

111+
# 行业数据
112+
# 获取行业列表
113+
industries = openapi_client.get_industry_list(IndustryLevel.GGROUP)
114+
print(industries)
115+
# 获取某行业下公司列表
116+
industry_stocks = openapi_client.get_industry_stocks(50101020)
117+
print(industry_stocks)
118+
# 获取某股票的行业
119+
stock_industry = openapi_client.get_stock_industry('AAPL', Market.US)
120+
print(stock_industry)
121+
111122

112123
if __name__ == '__main__':
113124
with pd.option_context('display.max_rows', None, 'display.max_columns', None):

tigeropen/examples/trade_client_demo.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tigeropen.trade.trade_client import TradeClient
1515
from tigeropen.quote.request import OpenApiRequest
1616
from tigeropen.examples.client_config import get_client_config
17+
# from tigeropen.common.consts import Currency, SecurityType
1718
# from tigeropen.common.util.contract_utils import stock_contract, option_contract_by_symbol, future_contract, \
1819
# war_contract_by_symbol, iopt_contract_by_symbol
1920
from tigeropen.common.util.order_utils import limit_order, limit_order_with_legs, order_leg
@@ -68,15 +69,22 @@ def trade_apis():
6869
account = client_config.account
6970
openapi_client = TradeClient(client_config, logger=logger)
7071

71-
# stock
72+
# 通过请求获取合约
7273
contract = openapi_client.get_contracts('AAPL')[0]
73-
# 或者本地构造合约对象
74-
# contract = stock_contract(symbol='AAPL', currency='USD')
74+
# contract = openapi_client.get_contract('AAPL', SecurityType.STK, currency=Currency.USD)
7575

76-
# option
76+
# 本地构造合约
77+
# stock 股票
78+
# contract = stock_contract(symbol='AAPL', currency='USD')
79+
# option 期权
7780
# contract = option_contract(identifier='AAPL 190118P00160000')
78-
# future
81+
# contract = option_contract_by_symbol('AAPL', '20200110', strike=280.0, put_call='PUT', currency='USD')
82+
# future 期货
7983
# contract = future_contract('CHF', 'USD', '20190617', multiplier=125000, exchange='GLOBEX')
84+
# war 港股窝轮
85+
# contract = war_contract_by_symbol('02318', '20200326', 107.08, 'CALL', local_symbol='12616', currency='HKD')
86+
# iopt 港股牛熊证
87+
# contract = iopt_contract_by_symbol('02318', '20200420', 87.4, 'CALL', local_symbol='63379', currency='HKD')
8088

8189
order = openapi_client.create_order(account, contract, 'BUY', 'LMT', 100, limit_price=5.0)
8290
# 或者本地构造订单对象

tigeropen/fundamental/request/model.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,59 @@ def to_openapi_dict(self):
207207

208208
return params
209209

210+
211+
class IndustryParams(object):
212+
def __init__(self):
213+
self._industry_level = None
214+
self._industry_id = None
215+
self._market = None
216+
self._symbol = None
217+
218+
@property
219+
def industry_level(self):
220+
return self._industry_level
221+
222+
@industry_level.setter
223+
def industry_level(self, value):
224+
self._industry_level = value
225+
226+
@property
227+
def industry_id(self):
228+
return self._industry_id
229+
230+
@industry_id.setter
231+
def industry_id(self, value):
232+
self._industry_id = value
233+
234+
@property
235+
def market(self):
236+
return self._market
237+
238+
@market.setter
239+
def market(self, value):
240+
self._market = value
241+
242+
@property
243+
def symbol(self):
244+
return self._symbol
245+
246+
@symbol.setter
247+
def symbol(self, value):
248+
self._symbol = value
249+
250+
def to_openapi_dict(self):
251+
params = dict()
252+
253+
if self.industry_level:
254+
params['industry_level'] = self.industry_level
255+
256+
if self.industry_id:
257+
params['industry_id'] = self.industry_id
258+
259+
if self.market:
260+
params['market'] = self.market
261+
262+
if self.symbol:
263+
params['symbol'] = self.symbol
264+
265+
return params
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# -*- coding: utf-8 -*-
2+
from tigeropen.common.util.string_utils import get_string
3+
from tigeropen.common.response import TigerResponse
4+
5+
6+
class IndustryListResponse(TigerResponse):
7+
def __init__(self):
8+
super(IndustryListResponse, self).__init__()
9+
self.industry_list = list()
10+
self._is_success = None
11+
12+
def parse_response_content(self, response_content):
13+
response = super(IndustryListResponse, self).parse_response_content(response_content)
14+
if 'is_success' in response:
15+
self._is_success = response['is_success']
16+
if self.data:
17+
for ind in self.data:
18+
industry = dict(industry_level=ind.get('industryLevel'), id=ind.get('id'),
19+
name_cn=get_string(ind.get('nameCN')),
20+
name_en=get_string(ind.get('nameEN')))
21+
self.industry_list.append(industry)
22+
23+
24+
class IndustryStocksResponse(TigerResponse):
25+
def __init__(self):
26+
super(IndustryStocksResponse, self).__init__()
27+
self.industry_stocks = list()
28+
self._is_success = None
29+
30+
def parse_response_content(self, response_content):
31+
response = super(IndustryStocksResponse, self).parse_response_content(response_content)
32+
if 'is_success' in response:
33+
self._is_success = response['is_success']
34+
if self.data:
35+
for item in self.data:
36+
industry_list = list()
37+
industries = item.get('industryDetailDTOList', [])
38+
for ind in industries:
39+
industry_list.append(dict(industry_level=ind.get('industryLevel'), id=ind.get('id'),
40+
name_cn=get_string(ind.get('nameCN')),
41+
name_en=get_string(ind.get('nameEN'))))
42+
company = dict(symbol=item.get('symbol'), company_name=item.get('companyName'),
43+
market=item.get('market'), industry_list=industry_list)
44+
self.industry_stocks.append(company)
45+
46+
47+
class StockIndustryResponse(TigerResponse):
48+
def __init__(self):
49+
super(StockIndustryResponse, self).__init__()
50+
self.stock_industry = list()
51+
self._is_success = None
52+
53+
def parse_response_content(self, response_content):
54+
response = super(StockIndustryResponse, self).parse_response_content(response_content)
55+
if 'is_success' in response:
56+
self._is_success = response['is_success']
57+
if self.data:
58+
for item in self.data:
59+
industry = dict(industry_level=item.get('industryLevel'), id=item.get('id'),
60+
name_cn=get_string(item.get('nameCN')),
61+
name_en=get_string(item.get('nameEN')))
62+
self.stock_industry.append(industry)

tigeropen/quote/domain/market_status.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88

99
class MarketStatus(object):
10-
def __init__(self, market, status, open_time):
10+
def __init__(self, market, status, open_time, trading_status):
1111
self.market = market
1212
self.status = status
1313
self.open_time = open_time
14+
self.trading_status = trading_status
1415

1516
def __repr__(self):
1617
"""

0 commit comments

Comments
 (0)