Skip to content

Commit a5498c2

Browse files
committed
industry api
1 parent c218b48 commit a5498c2

File tree

6 files changed

+226
-4
lines changed

6 files changed

+226
-4
lines changed

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/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 = quote_client.get_industry_list(IndustryLevel.GGROUP)
114+
print(industries)
115+
# 获取某行业下公司列表
116+
industry_stocks = quote_client.get_industry_stocks(50101020)
117+
print(industry_stocks)
118+
# 获取某股票的行业
119+
stock_industry = quote_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/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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pandas as pd
4+
import six
5+
from tigeropen.common.consts import Market
6+
from tigeropen.common.util.string_utils import get_string
7+
from tigeropen.common.response import TigerResponse
8+
9+
10+
class IndustryListResponse(TigerResponse):
11+
def __init__(self):
12+
super(IndustryListResponse, self).__init__()
13+
self.industry_list = list()
14+
self._is_success = None
15+
16+
def parse_response_content(self, response_content):
17+
response = super(IndustryListResponse, self).parse_response_content(response_content)
18+
if 'is_success' in response:
19+
self._is_success = response['is_success']
20+
if self.data:
21+
for ind in self.data:
22+
industry = dict(industry_level=ind.get('industryLevel'), id=ind.get('id'),
23+
name_cn=get_string(ind.get('nameCN')),
24+
name_en=get_string(ind.get('nameEN')))
25+
self.industry_list.append(industry)
26+
27+
28+
class IndustryStocksResponse(TigerResponse):
29+
def __init__(self):
30+
super(IndustryStocksResponse, self).__init__()
31+
self.industry_stocks = list()
32+
self._is_success = None
33+
34+
def parse_response_content(self, response_content):
35+
response = super(IndustryStocksResponse, self).parse_response_content(response_content)
36+
if 'is_success' in response:
37+
self._is_success = response['is_success']
38+
if self.data:
39+
for item in self.data:
40+
industry_list = list()
41+
industries = item.get('industryDetailDTOList', [])
42+
for ind in industries:
43+
industry_list.append(dict(industry_level=ind.get('industryLevel'), id=ind.get('id'),
44+
name_cn=get_string(ind.get('nameCN')),
45+
name_en=get_string(ind.get('nameEN'))))
46+
company = dict(symbol=item.get('symbol'), company_name=item.get('companyName'),
47+
market=item.get('market'), industry_list=industry_list)
48+
self.industry_stocks.append(company)
49+
50+
51+
class StockIndustryResponse(TigerResponse):
52+
def __init__(self):
53+
super(StockIndustryResponse, self).__init__()
54+
self.stock_industry = list()
55+
self._is_success = None
56+
57+
def parse_response_content(self, response_content):
58+
response = super(StockIndustryResponse, self).parse_response_content(response_content)
59+
if 'is_success' in response:
60+
self._is_success = response['is_success']
61+
if self.data:
62+
for item in self.data:
63+
industry = dict(industry_level=item.get('industryLevel'), id=item.get('id'),
64+
name_cn=get_string(item.get('nameCN')),
65+
name_en=get_string(item.get('nameEN')))
66+
self.stock_industry.append(industry)

tigeropen/quote/quote_client.py

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import delorean
1010
import six
1111

12-
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, CorporateActionType
12+
from tigeropen.common.consts import THREAD_LOCAL, SecurityType, CorporateActionType, IndustryLevel
1313
from tigeropen.common.exceptions import ApiException
14-
from tigeropen.fundamental.request.model import FinancialDailyParams, FinancialReportParams, CorporateActionParams
14+
from tigeropen.fundamental.request.model import FinancialDailyParams, FinancialReportParams, CorporateActionParams, \
15+
IndustryParams
1516
from tigeropen.fundamental.response.corporate_dividend_response import CorporateDividendResponse
1617
from tigeropen.fundamental.response.corporate_earnings_calendar_response import EarningsCalendarResponse
1718
from tigeropen.fundamental.response.corporate_split_response import CorporateSplitResponse
1819
from tigeropen.fundamental.response.financial_report_response import FinancialReportResponse
1920
from tigeropen.fundamental.response.financial_daily_response import FinancialDailyResponse
21+
from tigeropen.fundamental.response.industry_response import IndustryListResponse, IndustryStocksResponse, \
22+
StockIndustryResponse
2023
from tigeropen.quote.response.future_briefs_response import FutureBriefsResponse
2124
from tigeropen.quote.response.future_exchange_response import FutureExchangeResponse
2225
from tigeropen.quote.response.future_contract_response import FutureContractResponse
@@ -47,7 +50,8 @@
4750
TIMELINE, KLINE, TRADE_TICK, OPTION_EXPIRATION, OPTION_CHAIN, FUTURE_EXCHANGE, OPTION_BRIEF, \
4851
OPTION_KLINE, OPTION_TRADE_TICK, FUTURE_KLINE, FUTURE_TICK, FUTURE_CONTRACT_BY_EXCHANGE_CODE, \
4952
FUTURE_TRADING_DATE, QUOTE_SHORTABLE_STOCKS, FUTURE_REAL_TIME_QUOTE, \
50-
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION
53+
FUTURE_CURRENT_CONTRACT, QUOTE_REAL_TIME, QUOTE_STOCK_TRADE, FINANCIAL_DAILY, FINANCIAL_REPORT, CORPORATE_ACTION, \
54+
INDUSTRY_LIST, INDUSTRY_STOCKS, STOCK_INDUSTRY
5155
from tigeropen.common.consts import Market, Language, QuoteRight, BarPeriod
5256
from tigeropen.common.util.contract_utils import extract_option_info
5357
from tigeropen.common.util.common_utils import eastern
@@ -954,3 +958,71 @@ def get_financial_report(self, symbols, market, fields, period_type):
954958
return response.financial_report
955959
else:
956960
raise ApiException(response.code, response.message)
961+
962+
def get_industry_list(self, industry_level=IndustryLevel.GGROUP):
963+
"""
964+
获取行业列表
965+
:param industry_level: 行业级别. 可选值为 common.consts.IndustryLevel 枚举类型. 默认一级行业
966+
:return: 由行业信息 dict 构成的列表. industry_level 为行业级别, id 为行业 id
967+
如 [{'industry_level': 'GGROUP', 'id': '5020', 'name_cn': '媒体与娱乐', 'name_en': 'Media & Entertainment'},
968+
{'industry_level': 'GGROUP', 'id': '2550', 'name_cn': '零售业', 'name_en': 'Retailing'},
969+
...]
970+
"""
971+
params = IndustryParams()
972+
params.industry_level = industry_level.value
973+
request = OpenApiRequest(INDUSTRY_LIST, biz_model=params)
974+
response_content = self.__fetch_data(request)
975+
if response_content:
976+
response = IndustryListResponse()
977+
response.parse_response_content(response_content)
978+
if response.is_success():
979+
return response.industry_list
980+
else:
981+
raise ApiException(response.code, response.message)
982+
983+
def get_industry_stocks(self, industry, market=Market.US):
984+
"""
985+
获取某行业下的股票列表
986+
:param industry: 行业 id
987+
:param market: 市场枚举类型
988+
:return: 公司信息列表.
989+
如 [{'symbol': 'A', 'company_name': 'A', 'market': 'US', 'industry_list': [{...}, {...},..]},
990+
{'symbol': 'B', 'company_name': 'B', 'market': 'US', 'industry_list': [{...}, {...},..]},
991+
...]
992+
"""
993+
params = IndustryParams()
994+
params.market = market.value
995+
params.industry_id = industry
996+
request = OpenApiRequest(INDUSTRY_STOCKS, biz_model=params)
997+
response_content = self.__fetch_data(request)
998+
if response_content:
999+
response = IndustryStocksResponse()
1000+
response.parse_response_content(response_content)
1001+
if response.is_success():
1002+
return response.industry_stocks
1003+
else:
1004+
raise ApiException(response.code, response.message)
1005+
1006+
def get_stock_industry(self, symbol, market=Market.US):
1007+
"""
1008+
获取股票的行业
1009+
:param symbol: 股票 symbol
1010+
:param market: 市场枚举类型
1011+
:return: 所属多级行业的列表
1012+
如 [{'industry_level': 'GSECTOR', 'id': '45', 'name_cn': '信息技术', 'name_en': 'Information Technology'},
1013+
{'industry_level': 'GGROUP', 'id': '4520', 'name_cn': '技术硬件与设备', 'name_en': 'Technology Hardware & Equipment'},
1014+
{'industry_level': 'GIND', 'id': '452020', 'name_cn': '电脑与外围设备', 'name_en': 'Technology Hardware, Storage & Peripherals'},
1015+
{'industry_level': 'GSUBIND', 'id': '45202030', 'name_cn': '电脑硬件、储存设备及电脑周边', 'name_en': 'Technology Hardware, Storage & Peripherals'}]
1016+
"""
1017+
params = IndustryParams()
1018+
params.symbol = symbol
1019+
params.market = market.value
1020+
request = OpenApiRequest(STOCK_INDUSTRY, biz_model=params)
1021+
response_content = self.__fetch_data(request)
1022+
if response_content:
1023+
response = StockIndustryResponse()
1024+
response.parse_response_content(response_content)
1025+
if response.is_success():
1026+
return response.stock_industry
1027+
else:
1028+
raise ApiException(response.code, response.message)

0 commit comments

Comments
 (0)