66"""
77import logging
88import re
9+ import time
910
1011import delorean
12+ import pandas as pd
1113
1214from tigeropen .common .consts import Market , Language , QuoteRight , BarPeriod , OPEN_API_SERVICE_VERSION_V3
1315from tigeropen .common .consts import THREAD_LOCAL , SecurityType , CorporateActionType , IndustryLevel
@@ -399,7 +401,7 @@ def get_timeline_history(self, symbols, date, right=QuoteRight.BR):
399401 raise ApiException (response .code , response .message )
400402
401403 def get_bars (self , symbols , period = BarPeriod .DAY , begin_time = - 1 , end_time = - 1 , right = QuoteRight .BR , limit = 251 ,
402- lang = None ):
404+ lang = None , page_token = None ):
403405 """
404406 获取K线数据
405407 :param symbols: 股票代号列表
@@ -410,22 +412,25 @@ def get_bars(self, symbols, period=BarPeriod.DAY, begin_time=-1, end_time=-1, ri
410412 :param right: 复权选项 ,QuoteRight.BR: 前复权,nQuoteRight.NR: 不复权
411413 :param limit: 数量限制
412414 :param lang: 语言支持: zh_CN,zh_TW,en_US
415+ :param page_token: the token of next page. only supported when exactly one symbol
413416 :return: pandas.DataFrame 对象,各 column 的含义如下;
414417 time: 毫秒时间戳
415418 open: Bar 的开盘价
416419 close: Bar 的收盘价
417420 high: Bar 的最高价
418421 low: Bar 的最低价
419422 volume: Bar 的成交量
423+ next_page_token: token of next page
420424 """
421425 params = MultipleQuoteParams ()
422- params .symbols = symbols
426+ params .symbols = symbols if isinstance ( symbols , list ) else [ symbols ]
423427 params .period = get_enum_value (period )
424428 params .begin_time = begin_time
425429 params .end_time = end_time
426430 params .right = get_enum_value (right )
427431 params .limit = limit
428432 params .lang = get_enum_value (lang ) if lang else get_enum_value (self ._lang )
433+ params .page_token = page_token if len (params .symbols ) == 1 else None
429434
430435 request = OpenApiRequest (KLINE , biz_model = params )
431436 response_content = self .__fetch_data (request )
@@ -437,6 +442,41 @@ def get_bars(self, symbols, period=BarPeriod.DAY, begin_time=-1, end_time=-1, ri
437442 else :
438443 raise ApiException (response .code , response .message )
439444
445+ def get_bars_by_page (self , symbol , period = BarPeriod .DAY , begin_time = - 1 , end_time = - 1 , total = 10000 , page_size = 1000 ,
446+ right = QuoteRight .BR , time_interval = 2 , lang = None ):
447+ """
448+ request bats by page
449+ :param symbol: symbol of stock.
450+ :param period:
451+ :param begin_time:
452+ :param end_time: time of the latest bar, excluded
453+ :param total: Total bars number
454+ :param page_size: Bars number of each request
455+ :param right:
456+ :param time_interval: Time interval between requests
457+ :param lang:
458+ :return:
459+ """
460+ if begin_time == - 1 and end_time == - 1 :
461+ raise ApiException (400 , 'One of the begin_time or end_time must be specified' )
462+ if isinstance (symbol , list ) and len (symbol ) != 1 :
463+ raise ApiException (400 , 'Paging queries support only one symbol at each request' )
464+ current = 0
465+ next_page_token = None
466+ result = list ()
467+ while current < total :
468+ if current + page_size >= total :
469+ page_size = total - current
470+ current += page_size
471+ bars = self .get_bars (symbols = symbol , period = period , begin_time = begin_time , end_time = end_time , right = right ,
472+ limit = page_size , lang = lang , page_token = next_page_token )
473+ next_page_token = bars ['next_page_token' ].iloc [0 ]
474+ result .append (bars )
475+ if not next_page_token :
476+ break
477+ time .sleep (time_interval )
478+ return pd .concat (result ).sort_values ('time' ).reset_index (drop = True )
479+
440480 def get_trade_ticks (self , symbols , begin_index = None , end_index = None , limit = None , lang = None ):
441481 """
442482 获取逐笔成交
@@ -896,15 +936,16 @@ def get_future_trading_times(self, identifier, trading_date=None):
896936 raise ApiException (response .code , response .message )
897937 return None
898938
899- def get_future_bars (self , identifiers , period = BarPeriod .DAY , begin_time = - 1 , end_time = - 1 , limit = 1000 ):
939+ def get_future_bars (self , identifiers , period = BarPeriod .DAY , begin_time = - 1 , end_time = - 1 , limit = 1000 ,
940+ page_token = None ):
900941 """
901942 获取期货K线数据
902943 :param identifiers: 期货代码列表
903944 :param period: day: 日K,week: 周K,month:月K ,year:年K,1min:1分钟,5min:5分钟,15min:15分钟,30min:30分钟,60min:60分钟
904945 :param begin_time: 开始时间. 若是时间戳需要精确到毫秒, 为13位整数;
905- 或是日期时间格式的字符串, 如 "2019-01-01" 或 "2019-01-01 12:00:00"
906946 :param end_time: 结束时间. 格式同 begin_time
907947 :param limit: 数量限制
948+ :param page_token: the token of next page. only supported when there exactly one identifier
908949 :return: pandas.DataFrame, 各column 含义如下:
909950 identifier: 期货合约代码
910951 time: Bar对应的时间戳, 即Bar的结束时间。Bar的切割方式与交易所一致,以CN1901举例,T日的17:00至T+1日的16:30的数据会被合成一个日级Bar。
@@ -916,13 +957,15 @@ def get_future_bars(self, identifiers, period=BarPeriod.DAY, begin_time=-1, end_
916957 settlement: 结算价,在未生成结算价时返回0
917958 volume: 成交量
918959 open_interest: 未平仓合约数量
960+ next_page_token: token of next page
919961 """
920962 params = FutureQuoteParams ()
921- params .contract_codes = identifiers
963+ params .contract_codes = identifiers if isinstance ( identifiers , list ) else [ identifiers ]
922964 params .period = get_enum_value (period )
923965 params .begin_time = begin_time
924966 params .end_time = end_time
925967 params .limit = limit
968+ params .page_token = page_token if len (params .contract_codes ) == 1 else None
926969
927970 request = OpenApiRequest (FUTURE_KLINE , biz_model = params )
928971 response_content = self .__fetch_data (request )
@@ -934,6 +977,39 @@ def get_future_bars(self, identifiers, period=BarPeriod.DAY, begin_time=-1, end_
934977 else :
935978 raise ApiException (response .code , response .message )
936979
980+ def get_future_bars_by_page (self , identifier , period = BarPeriod .DAY , begin_time = - 1 , end_time = - 1 , total = 10000 ,
981+ page_size = 1000 , time_interval = 2 ):
982+ """
983+ request bats by page
984+ :param identifier: identifier of future
985+ :param period:
986+ :param begin_time:
987+ :param end_time: time of the latest bar, excluded
988+ :param total: Total bars number
989+ :param page_size: Bars number of each request
990+ :param time_interval: Time interval between requests
991+ :return:
992+ """
993+ if begin_time == - 1 and end_time == - 1 :
994+ raise ApiException (400 , 'One of the begin_time or end_time must be specified' )
995+ if isinstance (identifier , list ) and len (identifier ) != 1 :
996+ raise ApiException (400 , 'Paging queries support only one identifier at each request' )
997+ current = 0
998+ next_page_token = None
999+ result = list ()
1000+ while current < total :
1001+ if current + page_size >= total :
1002+ page_size = total - current
1003+ current += page_size
1004+ bars = self .get_future_bars (identifiers = identifier , period = period , begin_time = begin_time , end_time = end_time ,
1005+ limit = page_size , page_token = next_page_token )
1006+ next_page_token = bars ['next_page_token' ].iloc [0 ]
1007+ result .append (bars )
1008+ if not next_page_token :
1009+ break
1010+ time .sleep (time_interval )
1011+ return pd .concat (result ).sort_values ('time' ).reset_index (drop = True )
1012+
9371013 def get_future_trade_ticks (self , identifiers , begin_index = 0 , end_index = 30 , limit = 1000 ):
9381014 """
9391015 获取期货逐笔成交
@@ -1263,4 +1339,4 @@ def get_quote_permission(self):
12631339 return response .permissions
12641340 else :
12651341 raise ApiException (response .code , response .message )
1266- return False
1342+ return False
0 commit comments