def to_vnpy(self, gateway): tick = VtTickData() tick.symbol = self.instrument tick.exchange = EXCHANGE_OANDA tick.gatewayName = gateway.gatewayName tick.vtSymbol = VN_SEPARATOR.join([tick.symbol, tick.gatewayName]) tick.datetime, tick.date, tick.time = parse_datetime_str(self.time) ibids = list(range(len(self.bids))) iasks = list(range(len(self.asks))) bids = { "bidPrice%s" % (i + 1): float(v["price"]) for i, v in zip(ibids, self.bids) } bid_volumes = { "bidVolume%s" % (i + 1): v["liquidity"] for i, v in zip(ibids, self.bids) } asks = { "askPrice%s" % (i + 1): float(v["price"]) for i, v in zip(iasks, self.asks) } ask_volumes = { "askVolume%s" % (i + 1): v['liquidity'] for i, v in zip(iasks, self.asks) } tick.__dict__.update(bids) tick.__dict__.update(bid_volumes) tick.__dict__.update(asks) tick.__dict__.update(ask_volumes) tick.lastPrice = float( Decimal(str((tick.askPrice1 + tick.bidPrice1) / 2.0)).quantize( Decimal(str(tick.askPrice1)))) return { VtTickData: [tick], }
def processLine(self, line): historyData = line.split(',') #historyDataLen = len(historyData) symbol = historyData[2] #print 'processLine, symbol:' + symbol #从list转化为tick对象 historytick = VtTickData() historytick._id = historyData[0] historytick.gatewayName = 'CTP' historytick.symbol = symbol historytick.TradingDay = historyData[1] historytick.exchange = historyData[3] historytick.vtSymbol = historytick.symbol # '.'.join([tick.symbol, tick.exchange]) historytick.lastPrice = self.convertFloatZero(historyData[5]) #lastVolume historytick.volume = historyData[12] historytick.openInterest = historyData[14] UpdateMillisec = int(historyData[20]) historytick.time = '.'.join([historyData[19], str(UpdateMillisec/ 100)]) historytick.date = historyData[42] historytick.datetime = datetime.strptime(' '.join([historytick.date, historytick.time]), '%Y%m%d %H:%M:%S.%f') historytick.openPrice = self.convertFloatZero(historyData[9]) historytick.highPrice = self.convertFloatZero(historyData[10]) historytick.lowPrice = self.convertFloatZero(historyData[11]) historytick.preClosePrice = self.convertFloatZero(historyData[12]) historytick.ClosePrice = self.convertFloatZero(historyData[15]) historytick.SettlementPrice = self.convertFloatZero(historyData[16]) historytick.upperLimit = self.convertFloatZero(historyData[17]) historytick.lowerLimit = self.convertFloatZero(historyData[18]) # CTP只有一档行情 historytick.bidPrice1 = self.convertFloatZero(historyData[21]) historytick.bidVolume1 = historyData[22] historytick.askPrice1 = self.convertFloatZero(historyData[23]) historytick.askVolume1 = historyData[24] historytick.AveragePrice = self.convertFloatZero(historyData[41]) return historytick