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 generateVtTick(row): '生成tick' tick = VtTickData() tick.symbol = row['code'] tick.exchange = generateExchange(tick.symbol) tick.vtSymbol = '.'.ljust([tick.symbol, tick.exchange]) tick.lastPrice = row['close'] tick.datetime = row.name tick.highPrice = row['high'] tick.lowPrice = row['low']
def loadTickData(self, tick): tickData = VtTickData() tickData.lastPrice = float(tick['lastPrice']) tickData.date = tick['date'] tickData.time = tick['time'] tickData.datetime = datetime.strptime( ' '.join([tick['date'], tick['time']]), '%Y%m%d %H:%M:%S.%f') tickData.volume = int(tick['volume']) tickData.vtSymbol = tick['vtSymbol'] tickData.symbol = tick['symbol'] tickData.exchange = tick['exchange'] tickData.openInterest = int(tick['openInterest']) return tickData
def mongodbRowToTick(row_tick): tickData = VtTickData() tickData.lastPrice = float(row_tick['lastPrice']) tickData.date = row_tick['date'] tickData.time = row_tick['time'] tickData.datetime = datetime.strptime( ' '.join([row_tick['date'], row_tick['time']]), '%Y%m%d %H:%M:%S.%f') tickData.volume = int(row_tick['volume']) tickData.vtSymbol = row_tick['vtSymbol'] tickData.symbol = row_tick['symbol'] tickData.exchange = row_tick['exchange'] tickData.TradingDay = row_tick['TradingDay'] tickData.openInterest = int(row_tick['openInterest']) return tickData
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
def loadTickData(self, tick): tickData = VtTickData() #tickData.lastPrice = float(tick['lastPrice']) #要处理小数位数,只保留一位小数 #线上录得数据写文件,比如原始数据是个整数3967,写到文件里的会是3967.000000000001 tickData.lastPrice = round(float(tick['lastPrice']), 1) tickData.date = tick['date'] tickData.time = tick['time'] tickData.datetime = datetime.strptime(' '.join([tick['date'], tick['time']]), '%Y%m%d %H:%M:%S.%f') tickData.volume = int(tick['volume']) tickData.vtSymbol = tick['vtSymbol'] tickData.symbol = tick['symbol'] tickData.exchange = tick['exchange'] tickData.TradingDay = tick['TradingDay'] #tickData.openInterest = int(tick['openInterest']) #防止持仓量可能带小数 tickData.openInterest = int(float(tick['openInterest'])) return tickData