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 __init__(self, stra_barupdate=None, stra_onorder=None): '''初始所有变量''' self.stra_uppdate = stra_barupdate self.stra_onorder = stra_onorder '''每bar只执行一次交易''' self.SingleOrderOneBar = False '''每bar只执行一 次交易''' '''K线序列''' self.Bars = [] '''K线序列''' '''合约''' self.Instrument = '' '''合约''' '''合约信息''' self.InstrumentInfo = InstrumentField() '''合约信息''' '''周期''' self.Interval = 1 '''周期''' '''周期类型''' self.IntervalType = IntervalType.Minute '''周期类型''' '''分笔数据 Tick.Instrument用来判断是否有实盘数据''' self.Tick = Tick() '''分笔数据 Tick.Instrument用来判断是否有实盘数据''' '''买卖信号''' self.Orders = [] '''买卖信号''' '''指标字典 策略使用的指标保存在此字典中 以便管理程序显示和处理''' self.IndexDict = {} '''指标字典 策略使用的指标保存在此字典中 以便管理程序显示和处理''' '''时间''' self.D = [] '''时间''' '''最高价''' self.H = [] '''最高价''' '''最低价''' self.L = [] '''最低价''' '''开盘价''' self.O = [] '''开盘价''' '''收盘价''' self.C = [] '''收盘价''' '''交易量''' self.V = [] '''交易量''' '''持仓量''' self.I = [] '''持仓量''' self._lastOrder = OrderItem()
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