def __day_offset(self, start, end, nums, frequency): _offset = len(days_trade_range(start=None)) // nums _start = len(days_trade_range(start=start)) // nums _end = len(days_trade_range(start=end)) // nums #print(_offset, _start, _end, _offset-_start, _offset-_end+1) return [(i * nums, nums) for i in range(_offset, -1, -1) ][_offset - _start:_offset - _end + 1]
def __paralle_hq_bars(self, code, start=None, end=datetime.datetime.today(), count=0, frequency=9, index=False): if frequency == self.KLINE_RI_K: func = self.__day_offset elif frequency == self.KLINE_1MIN: func = self.__min_offset else: return None code = self.__code(code) days = days_trade_range(start=start, end=end, count=count) start, end = days[0], days[-1] offset = func(start=start, end=end, nums=800, frequency=frequency) #print(offset) csize = len(code) self.__paralle_hq_ping() if csize == 1: #单线程 df = self.__hq_bars(code=code, offset=offset, frequency=frequency, index=index) else: qsize = self.__hq.qsize() if qsize >= csize: #每线程一个 for _code in code: self.__tasks.add(self.__hq_bars, code=[_code], offset=offset, frequency=frequency, index=index) else: #每线程多个 ma = csize // qsize fix = csize % qsize for i in range(qsize): _code = ma + 1 if i < fix else ma self.__tasks.add(self.__hq_bars, code=code[0:_code], offset=offset, frequency=frequency, index=index) code = code[_code:] df = pandas.concat(self.__tasks.executor(), sort=False) df = df.assign(volume=df.vol, datetime=pandas.to_datetime(df['datetime'])) df = df[[ 'datetime', 'code', 'open', 'high', 'low', 'close', 'volume', 'amount' ]].set_index(['datetime', 'code']).sort_index() df = df[pandas.to_datetime( df.index.get_level_values(0).strftime('%Y-%m-%d')).isin(days)] return df
def __min_offset(self, start, end, nums, frequency): if frequency == 0: lens = 48 else: lens = 240 _start = days_trade_range(start=start) if len(_start) * lens > 21600: _start = _start[int(21600 / lens):] _start = len(_start) * lens _end = days_trade_range(start=end) if len(_end) * lens > 21600: _end = _end[int(21600 / lens):] _end = len(_end) * lens if _start == _end: _end -= lens return [ ((i, nums)) for i in range(_end - lens if _end >= lens else _end, _start, nums) ]