class TushareBarFeedThread(TuSharePollingThread): # Events ON_BARS = 1 def __init__(self, queue, identifiers, frequency): super(TushareBarFeedThread, self).__init__(identifiers) self.__queue = queue self.__frequency = frequency self.__updateNextBarClose() def __updateNextBarClose(self): self.__nextBarClose = resamplebase.build_range( utcnow(), self.__frequency).getEnding() def getNextCallDateTime(self): return self.__nextBarClose def doCall(self): endDateTime = self.__nextBarClose self.__updateNextBarClose() bar_dict = {} for identifier in self._identifiers: try: if not self._tickDSDict[identifier].empty(): bar_dict[identifier] = build_bar( to_market_datetime(endDateTime), self._tickDSDict[identifier]) except Exception, e: logger.error(e) if len(bar_dict): bars = bar.Bars(bar_dict) self.__queue.put((TushareBarFeedThread.ON_BARS, bars))
class GetBarThread(PollingThread): # Events ON_BARS = 1 def __init__(self, queue, identifiers, frequency): super(GetBarThread, self).__init__() self.__queue = queue self.__identifiers = identifiers self.__frequency = frequency self.__nextBarStart = None self.__nextBarClose = None self._ticksDf = {} self.__lock = Lock() for identifier in self.__identifiers: self._ticksDf[identifier] = DataFrame( columns=['time', 'price', 'volume', 'amount']) self._ctpMdApi = CTPMdApi(self.__identifiers, self._ticksDf, self.__lock, logger) #self._ctpMdApi.login("tcp://222.66.97.241:41213", '9017811', '123456', '7070') self._ctpMdApi.login("tcp://180.168.212.228:41213", "simnow申请", "simnow申请", "9999") #self._ctpMdApi.login("tcp://211.144.195.163:34505", "", "", "") self.__updateNextBarClose() def __updateNextBarClose(self): self.__nextBarStart = resamplebase.build_range( utcnow(), self.__frequency).getBeginning() self.__nextBarClose = resamplebase.build_range( utcnow(), self.__frequency).getEnding() def getNextCallDateTime(self): return self.__nextBarClose def doCall(self): startDateTime = to_market_datetime(self.__nextBarStart) endDateTime = to_market_datetime(self.__nextBarClose) self.__updateNextBarClose() barDict = dict() for identifier in self.__identifiers: try: period_bar = self._build_bar(identifier, startDateTime, endDateTime) if period_bar: barDict[identifier] = period_bar except Exception, e: logger.error(e) if len(barDict): bars = bar.Bars(barDict) self.__queue.put((GetBarThread.ON_BARS, bars))
def __fill_bars(self, bars_dict): for index, value in enumerate(bars_dict[self.__identifiers[0]]): bar_dict = dict() for identifier in self.__identifiers: if bars_dict[identifier][index] is not None: bar_dict[identifier] = bars_dict[identifier][index] if len(bar_dict): bars = bar.Bars(bar_dict) self.__queue.put((TushareBarFeedThread.ON_BARS, bars))
def __fill_bars(self, bars_dict): for index, value in enumerate(bars_dict[self.__identifiers[0]]): bar_dict = dict() #print index, value, "111" for identifier in self.__identifiers: if bars_dict[identifier][index] is not None: #print "a", identifier bar_dict[identifier] = bars_dict[identifier][index] kline_time = bar_dict[identifier].getDateTime() if self.__last_kline_time[identifier] is None or \ self.__last_kline_time[identifier] < kline_time: self.__last_kline_time[identifier] = kline_time if len(bar_dict): bars = bar.Bars(bar_dict) self.__queue.put((FutuLiveFeed.ON_BARS, bars))
def on_recv_rsp(self, rsp_str): #ret_code, content = super().on_recv_rsp(rsp_str) ret_code, content = super(CurKlineTest, self).on_recv_rsp(rsp_str) print(ret_code) if ret_code != futu_open.RET_OK: print("CurKlineTest: error, msg: %s" % content) return futu_open.RET_ERROR, content print("CurKline : ", content) for index, row in content.iterrows(): open_ = row.open high = row.high low = row.low close = row.close volume = row.volume amount = row.turnover slice_start_time = datetime.datetime.strptime( row.time_key, '%Y-%m-%d %H:%M:%S') slice_start_time = to_market_datetime(slice_start_time) identifier = row.code k_type = row.k_type if k_type != 'K_1M': continue if identifier not in self.__identifiers: continue if self.__last_kline_time[ identifier] is None or self.__last_kline_time[ identifier] < slice_start_time: self.__last_kline_time[identifier] = slice_start_time else: #print "slice_start_time %s last_kline_time %s", (slice_start_time, slice_start_time) continue frequency = bar.Frequency.MINUTE one_bar = bar.BasicBar(slice_start_time, open_, high, low, close, volume, 0, frequency, amount) bar_dict = {} bar_dict[identifier] = one_bar bars = bar.Bars(bar_dict) self.__queue.put((FutuLiveFeed.ON_BARS, bars))