Skip to content

Commit 720add9

Browse files
committed
Merge branch 'dev' into 'master'
Dev See merge request server/openapi/openapi-python-sdk!134
2 parents b5e830d + 43a840c commit 720add9

File tree

16 files changed

+337
-29
lines changed

16 files changed

+337
-29
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 2.1.9 (2022-10-12)
2+
### New
3+
- 支持多牌照配置, 分牌照请求不同域名. 可通过 client_config.license 指定牌照
4+
### Modify
5+
- `Contract` 新增属性 `short_initial_margin`, `short_maintenance_margin`, 新增方法 `to_str()` 可打印全部属性
6+
- `QuoteClient.get_financial_report` 增加参数 `begin_date`, `end_date`
7+
- `QuoteClient.get_trade_ticks` 兼容 1.0 版本接口
8+
9+
110
## 2.1.8 (2022-08-26)
211
### Modify
312
- `TradeClient.get_orders` 新增参数 `seg_type`, 可指定交易品种(证券SEC/期货FUT/全部ALL)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
'Programming Language :: Python :: 3.7',
3535
'Programming Language :: Python :: 3.8',
3636
'Programming Language :: Python :: 3.9',
37+
'Programming Language :: Python :: 3.10',
3738
],
3839
)

tests/test_client_config.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# @Date : 2022/10/8
4+
# @Author : sukai
5+
import unittest
6+
from unittest.mock import MagicMock
7+
8+
from tigeropen.common.consts import License
9+
from tigeropen.tiger_open_config import TigerOpenClientConfig, GATEWAY_SUFFIX
10+
11+
12+
class TestClientConfig(unittest.TestCase):
13+
14+
def test_refresh_server_info(self):
15+
domain_map = {
16+
'socket_port': 9883,
17+
'port': 9887,
18+
'TBSG-QUOTE': 'https://openapi.tigerfintech.com/sgp-quote',
19+
'TBNZ-QUOTE': 'https://openapi.tigerfintech.com/hkg-quote',
20+
'TBSG-PAPER': 'https://openapi-sandbox.tigerfintech.com/sgp',
21+
'TBNZ-PAPER': 'https://openapi-sandbox.tigerfintech.com/hkg',
22+
'TBSG': 'https://openapi.tigerfintech.com/sgp',
23+
'TBNZ': 'https://openapi.tigerfintech.com/hkg',
24+
'COMMON': 'https://openapi.tigerfintech.com',
25+
}
26+
config = TigerOpenClientConfig()
27+
config.query_domains = MagicMock(name='query_domains', return_value=domain_map)
28+
config.domain_conf = config.query_domains()
29+
30+
self.assertEqual('https://openapi.tigerfintech.com' + GATEWAY_SUFFIX, config.server_url)
31+
self.assertEqual(('ssl', 'openapi.tigerfintech.com', 9883), config.socket_host_port)
32+
33+
config.license = License.TBNZ
34+
config.refresh_server_info()
35+
self.assertEqual('https://openapi.tigerfintech.com/hkg' + GATEWAY_SUFFIX, config.server_url)
36+
self.assertEqual('https://openapi.tigerfintech.com/hkg-quote' + GATEWAY_SUFFIX, config.quote_server_url)
37+
38+
config.license = 'TBSG'
39+
config.refresh_server_info()
40+
self.assertEqual('https://openapi.tigerfintech.com/sgp' + GATEWAY_SUFFIX, config.server_url)
41+
self.assertEqual('https://openapi.tigerfintech.com/sgp-quote' + GATEWAY_SUFFIX, config.quote_server_url)
42+
43+
config.is_paper = True
44+
config.license = 'TBNZ'
45+
config.refresh_server_info()
46+
self.assertEqual('https://openapi-sandbox.tigerfintech.com/hkg' + GATEWAY_SUFFIX, config.server_url)
47+
self.assertEqual('https://openapi.tigerfintech.com/hkg-quote' + GATEWAY_SUFFIX, config.quote_server_url)
48+
49+
config = TigerOpenClientConfig(enable_dynamic_domain=False)
50+
config.query_domains = MagicMock(name='query_domains', return_value=domain_map)
51+
config.domain_conf = config.query_domains()
52+
config.license = 'TBNZ'
53+
config.refresh_server_info()
54+
self.assertEqual('https://openapi.tigerfintech.com' + GATEWAY_SUFFIX, config.server_url)
55+
self.assertEqual(('ssl', 'openapi.tigerfintech.com', 9883), config.socket_host_port)

tigeropen/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
55
@author: gaoan
66
"""
7-
__VERSION__ = '2.1.8'
7+
__VERSION__ = '2.1.9'

tigeropen/common/consts/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,17 @@ class OrderType(Enum):
169169
STP = 'STP' # 止损单
170170
STP_LMT = 'STP_LMT' # 止损限价单
171171
TRAIL = 'TRAIL' # 跟踪止损单
172+
173+
174+
@unique
175+
class License(Enum):
176+
TBNZ = 'TBNZ'
177+
TBSG = 'TBSG'
178+
179+
180+
@unique
181+
class ServiceType(Enum):
182+
COMMON = 'COMMON'
183+
TRADE = 'TRADE'
184+
QUOTE = 'QUOTE'
185+

tigeropen/common/consts/service_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
ORDER_TRANSACTIONS = "order_transactions" # 订单成交记录
2626
ANALYTICS_ASSET = "analytics_asset"
2727

28+
USER_LICENSE = "user_license"
29+
2830
"""
2931
合约
3032
"""
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import re
2+
3+
PAPER_ACCOUNT_DIGIT_LEN = 17
4+
5+
6+
class AccountUtil:
7+
8+
@staticmethod
9+
def is_paper_account(account):
10+
try:
11+
if account:
12+
account = str(account)
13+
return account.isdigit() and len(account) >= PAPER_ACCOUNT_DIGIT_LEN
14+
except:
15+
pass
16+
return False
17+

tigeropen/fundamental/request/model.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def __init__(self):
9191
self._market = None
9292
self._fields = None
9393
self._period_type = None
94+
self._begin_date = None
95+
self._end_date = None
9496

9597
@property
9698
def symbols(self):
@@ -124,6 +126,22 @@ def period_type(self):
124126
def period_type(self, value):
125127
self._period_type = value
126128

129+
@property
130+
def begin_date(self):
131+
return self._begin_date
132+
133+
@begin_date.setter
134+
def begin_date(self, value):
135+
self._begin_date = value
136+
137+
@property
138+
def end_date(self):
139+
return self._end_date
140+
141+
@end_date.setter
142+
def end_date(self, value):
143+
self._end_date = value
144+
127145
def to_openapi_dict(self):
128146
params = super().to_openapi_dict()
129147

@@ -139,6 +157,12 @@ def to_openapi_dict(self):
139157
if self.period_type:
140158
params['period_type'] = self.period_type
141159

160+
if self.begin_date:
161+
params['begin_date'] = self.begin_date
162+
163+
if self.end_date:
164+
params['end_date'] = self.end_date
165+
142166
return params
143167

144168

tigeropen/quote/quote_client.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def __init__(self, client_config, logger=None, is_grab_permission=True):
7676
super(QuoteClient, self).__init__(client_config, logger=self.logger)
7777
self._lang = LANGUAGE
7878
self._timezone = eastern
79+
self._url = None
7980
if client_config:
81+
self._url = client_config.quote_server_url
8082
self._lang = client_config.language
8183
if client_config.timezone:
8284
self._timezone = client_config.timezone
@@ -87,7 +89,7 @@ def __init__(self, client_config, logger=None, is_grab_permission=True):
8789

8890
def __fetch_data(self, request):
8991
try:
90-
response = super(QuoteClient, self).execute(request)
92+
response = super(QuoteClient, self).execute(request, url=self._url)
9193
return response
9294
except Exception as e:
9395
if hasattr(THREAD_LOCAL, 'logger') and THREAD_LOCAL.logger:
@@ -480,7 +482,8 @@ def get_bars_by_page(self, symbol, period=BarPeriod.DAY, begin_time=-1, end_time
480482
time.sleep(time_interval)
481483
return pd.concat(result).sort_values('time').reset_index(drop=True)
482484

483-
def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_index=None, limit=None, lang=None):
485+
def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_index=None, limit=None, lang=None,
486+
**kwargs):
484487
"""
485488
获取逐笔成交
486489
:param symbols: 股票代号列表
@@ -497,12 +500,16 @@ def get_trade_ticks(self, symbols, trade_session=None, begin_index=None, end_ind
497500
direction: 价格变动方向,"-"表示向下变动, "+" 表示向上变动
498501
"""
499502
params = MultipleQuoteParams()
500-
params.symbols = symbols
503+
params.symbols = [symbols] if isinstance(symbols, str) else symbols
504+
# compatible with version 1.0
505+
params.symbol = symbols if isinstance(symbols, str) else symbols[0]
501506
params.trade_session = get_enum_value(trade_session)
502507
params.begin_index = begin_index
503508
params.end_index = end_index
504509
params.limit = limit
505510
params.lang = get_enum_value(lang) if lang else get_enum_value(self._lang)
511+
if 'version' in kwargs:
512+
params.version = kwargs.get('version')
506513

507514
request = OpenApiRequest(TRADE_TICK, biz_model=params)
508515
response_content = self.__fetch_data(request)
@@ -1232,14 +1239,16 @@ def get_financial_daily(self, symbols, market, fields, begin_date, end_date):
12321239
else:
12331240
raise ApiException(response.code, response.message)
12341241

1235-
def get_financial_report(self, symbols, market, fields, period_type):
1242+
def get_financial_report(self, symbols, market, fields, period_type, begin_date=None, end_date=None):
12361243
"""
12371244
获取财报数据
12381245
:param symbols:
12391246
:param market: 查询的市场. 可选的值为 common.consts.Market 枚举类型, 如 Market.US
12401247
:param fields: 查询的字段列表. 可选的项为 common.consts 下的 Income, Balance, CashFlow, BalanceSheetRatio,
12411248
Growth, Leverage, Profitability 枚举类型. 如 Income.total_revenue
12421249
:param period_type: 查询的周期类型. 可选的值为 common.consts.FinancialReportPeriodType 枚举类型
1250+
:param begin_date: specify range begin of period_end_date
1251+
:param end_date: specify range end of period_end_date
12431252
:return: pandas.DataFrame, 各 column 的含义如下:
12441253
symbol: 证券代码
12451254
currency: 财报使用的币种
@@ -1254,6 +1263,8 @@ def get_financial_report(self, symbols, market, fields, period_type):
12541263
params.fields = [get_enum_value(field) for field in fields]
12551264
params.period_type = get_enum_value(period_type)
12561265
params.lang = get_enum_value(self._lang)
1266+
params.begin_date = date_str_to_timestamp(begin_date, self._timezone)
1267+
params.end_date = date_str_to_timestamp(end_date, self._timezone)
12571268
request = OpenApiRequest(FINANCIAL_REPORT, biz_model=params)
12581269
response_content = self.__fetch_data(request)
12591270
if response_content:

tigeropen/quote/request/model.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ class MultipleQuoteParams(MarketParams):
210210
def __init__(self):
211211
super(MultipleQuoteParams, self).__init__()
212212
self._symbols = None
213+
self._symbol = None
213214
self._include_hour_trading = None
214215
self._include_ask_bid = None
215216
self._right = None
@@ -231,6 +232,14 @@ def symbols(self):
231232
def symbols(self, value):
232233
self._symbols = value
233234

235+
@property
236+
def symbol(self):
237+
return self._symbol
238+
239+
@symbol.setter
240+
def symbol(self, value):
241+
self._symbol = value
242+
234243
@property
235244
def include_hour_trading(self):
236245
return self._include_hour_trading
@@ -333,6 +342,9 @@ def to_openapi_dict(self):
333342
if self.symbols:
334343
params['symbols'] = self.symbols
335344

345+
if self.symbol:
346+
params['symbol'] = self.symbol
347+
336348
if self.include_hour_trading is not None:
337349
params['include_hour_trading'] = self.include_hour_trading
338350

0 commit comments

Comments
 (0)