def _OnRtnDepthMarketData( self, pDepthMarketData: CThostFtdcDepthMarketDataField): """""" tick: Tick = None # 这个逻辑交由应用端处理更合理 ==> 第一个tick不送给客户端(以处理隔夜早盘时收到夜盘的数据的问题) inst = pDepthMarketData.getInstrumentID() if inst not in self.inst_tick: tick = Tick() self.inst_tick[inst] = tick else: tick = self.inst_tick[inst] tick.AskPrice = pDepthMarketData.getAskPrice1() tick.AskVolume = pDepthMarketData.getAskVolume1() tick.AveragePrice = pDepthMarketData.getAveragePrice() tick.BidPrice = pDepthMarketData.getBidPrice1() tick.BidVolume = pDepthMarketData.getBidVolume1() tick.Instrument = pDepthMarketData.getInstrumentID() tick.LastPrice = pDepthMarketData.getLastPrice() tick.OpenInterest = pDepthMarketData.getOpenInterest() tick.Volume = pDepthMarketData.getVolume() # 用tradingday替代Actionday不可取 # day = pDepthMarketData.getTradingDay() # str = day + ' ' + pDepthMarketData.getUpdateTime() # if day is None or day == ' ': # str = time.strftime('%Y%m%d %H:%M:%S', time.localtime()) # tick.UpdateTime = str # time.strptime(str, '%Y%m%d %H:%M:%S') tick.UpdateTime = pDepthMarketData.getUpdateTime() tick.UpdateMillisec = pDepthMarketData.getUpdateMillisec() # 用线程会导入多数据入库时报错 # _thread.start_new_thread(self.OnTick, (self, tick)) self.OnTick(self, tick)
def read_ticks(self, stra: Strategy, tradingday: str) -> []: """读取tick数据 返回 list[Tick]""" ticks: list = [] if self.cfg.engine_postgres is not None: conn = self.cfg.engine_postgres.raw_connection() cursor = conn.cursor() sql = "select count(1) from pg_tables where schemaname='future_tick' and tablename='{}'".format(tradingday) try: cursor.execute(sql) if cursor.fetchone()[0] == 0: return [] for data in stra.Datas: sql = 'select "Actionday", "AskPrice", "AskVolume", "BidPrice", "BidVolume", "Instrument", "LastPrice", "OpenInterest", "UpdateMillisec", "UpdateTime", "Volume" from future_tick."{}" where "Instrument" = \'{}\''.format( tradingday, data.Instrument) cursor.execute(sql) rows = cursor.fetchall() for d in rows: tick = Tick() tick.Instrument = data.Instrument tick.AskPrice = d[1] tick.AskVolume = d[2] tick.BidPrice = d[3] tick.BidVolume = d[4] tick.LastPrice = d[6] tick.OpenInterest = d[7] tick.UpdateMillisec = d[8] tick.UpdateTime = d[0][0:4] + '-' + d[0][4:6] + '-' + d[0][6:] + ' ' + d[9] tick.Volume = d[10] ticks.append(tick) finally: conn.close() ticks.sort(key=lambda t: t.UpdateTime) return ticks