def onRtnDepthMarketData(self, data: dict): """ Callback of tick data update. """ symbol = data["InstrumentID"] exchange = symbol_exchange_map.get(symbol, "") if not exchange: return timestamp = f"{data['ActionDay']} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}" tick = TickData( symbol=symbol, exchange=exchange, datetime=datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f"), name=symbol_name_map[symbol], volume=data["Volume"], last_price=data["LastPrice"], limit_up=data["UpperLimitPrice"], limit_down=data["LowerLimitPrice"], open_price=data["OpenPrice"], high_price=data["HighestPrice"], low_price=data["LowestPrice"], pre_close=data["PreClosePrice"], bid_price_1=data["BidPrice1"], ask_price_1=data["AskPrice1"], bid_volume_1=data["BidVolume1"], ask_volume_1=data["AskVolume1"], gateway_name=self.gateway_name ) self.gateway.on_tick(tick)
def subscribe(self, req: SubscribeRequest): """ Subscribe tick data update. """ if not self.status: return if req.exchange not in EXCHANGE_VT2IB: self.gateway.write_log(f"不支持的交易所{req.exchange}") return ib_contract = Contract() ib_contract.conId = str(req.symbol) ib_contract.exchange = EXCHANGE_VT2IB[req.exchange] # Get contract data from TWS. self.reqid += 1 self.client.reqContractDetails(self.reqid, ib_contract) # Subscribe tick data and create tick object buffer. self.reqid += 1 self.client.reqMktData(self.reqid, ib_contract, "", False, False, []) tick = TickData( symbol=req.symbol, exchange=req.exchange, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[self.reqid] = tick self.tick_exchange[self.reqid] = req.exchange
def subscribe(self, req: SubscribeRequest): """""" contract_type = symbol_type_map.get(req.symbol, "") if not contract_type: return buf = [i for i in req.symbol if not i.isdigit()] symbol = "".join(buf) ws_contract_type = CONTRACT_TYPE_MAP[contract_type] ws_symbol = f"{symbol}_{ws_contract_type}" # Create tick data buffer tick = TickData( symbol=req.symbol, name=req.symbol, exchange=Exchange.HUOBI, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[ws_symbol] = tick # Subscribe to market depth update self.req_id += 1 req = { "sub": f"market.{ws_symbol}.depth.step0", "id": str(self.req_id) } self.send_packet(req) # Subscribe to market detail update self.req_id += 1 req = {"sub": f"market.{ws_symbol}.detail", "id": str(self.req_id)} self.send_packet(req)
def OnDepthMarketData(self, market_data: XTPMarketDataStruct, bid1_qty: Sequence[int], bid1_count: int, max_bid1_count: int, ask1_qty: Sequence[int], ask1_count: int, max_ask1_count: int) -> Any: """""" timestamp = str(market_data.data_time) dt = datetime.strptime(timestamp, "%Y%m%d%H%M%S%f") tick = TickData(symbol=market_data.ticker, exchange=EXCHANGE_XTP2VT[market_data.exchange_id], datetime=dt, volume=market_data.qty, last_price=market_data.last_price, limit_up=market_data.upper_limit_price, limit_down=market_data.lower_limit_price, open_price=market_data.open_price, high_price=market_data.high_price, low_price=market_data.low_price, pre_close=market_data.pre_close_price, bid_price_1=market_data.bid[0], bid_price_2=market_data.bid[1], bid_price_3=market_data.bid[2], bid_price_4=market_data.bid[3], bid_price_5=market_data.bid[4], ask_price_1=market_data.ask[0], ask_price_2=market_data.ask[1], ask_price_3=market_data.ask[2], ask_price_4=market_data.ask[3], ask_price_5=market_data.ask[4], bid_volume_1=market_data.bid_qty[0], bid_volume_2=market_data.bid_qty[1], bid_volume_3=market_data.bid_qty[2], bid_volume_4=market_data.bid_qty[3], bid_volume_5=market_data.bid_qty[4], ask_volume_1=market_data.ask_qty[0], ask_volume_2=market_data.ask_qty[1], ask_volume_3=market_data.ask_qty[2], ask_volume_4=market_data.ask_qty[3], ask_volume_5=market_data.ask_qty[4], gateway_name=self.gateway_name) tick.name = symbol_name_map.get(tick.vt_symbol, tick.symbol) self.gateway.on_tick(tick)
def get_tick(self, code): """ Get tick buffer. """ tick = self.ticks.get(code, None) symbol, exchange = convert_symbol_futu2vt(code) if not tick: tick = TickData( symbol=symbol, exchange=exchange, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[code] = tick contract = self.contracts.get(tick.vt_symbol, None) if contract: tick.name = contract.name return tick
def subscribe(self, req: SubscribeRequest): """ Subscribe to tick data upate. """ tick = TickData( symbol=req.symbol, exchange=req.exchange, name=req.symbol, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[req.symbol] = tick
def subscribe(self): # 初始化 for symbol in self.symbols: #l.append('ticker.' + symbol) #l.append('depth.L20.' + symbol) tick = TickData() tick.gateway_name = self.gateway_name tick.symbol = symbol tick.exchange = EXCHANGE_IDCM tick.vtSymbol = '.'.join([tick.exchange, tick.symbol]) self.tickDict[symbol] = tick deal = DealData() deal.gateway_name = self.gateway_name deal.symbol = symbol deal.exchange = EXCHANGE_IDCM deal.vtSymbol = '.'.join([deal.exchange, deal.symbol]) self.dealDict[symbol] = deal for symbol in self.symbols: # 订阅行情深度,支持5,10,20档 channel = "idcm_sub_spot_" + symbol + "_depth_20" req = {'event': 'addChannel', 'channel': channel} self.sendReq(req) # 订阅行情数据 channel = "idcm_sub_spot_" + symbol + "_ticker" req = {'event': 'addChannel', 'channel': channel} self.sendReq(req) # 订阅成交记录 channel = "idcm_sub_spot_" + symbol + "_deals" req = {'event': 'addChannel', 'channel': channel} self.sendReq(req)
def subscribe(self, req: SubscribeRequest): """ Subscribe to tick data upate. """ tick = TickData( symbol=req.symbol, exchange=req.exchange, name=req.symbol, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[req.symbol] = tick channel_ticker = f"futures/ticker:{req.symbol}" channel_depth = f"futures/depth5:{req.symbol}" self.callbacks[channel_ticker] = self.on_ticker self.callbacks[channel_depth] = self.on_depth req = {"op": "subscribe", "args": [channel_ticker, channel_depth]} self.send_packet(req)
def subscribe(self, req: SubscribeRequest): """""" symbol = req.symbol # Create tick data buffer tick = TickData( symbol=symbol, name=symbol_name_map.get(symbol, ""), exchange=Exchange.HUOBI, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.ticks[symbol] = tick # Subscribe to market depth update self.req_id += 1 req = {"sub": f"market.{symbol}.depth.step0", "id": str(self.req_id)} self.send_packet(req) # Subscribe to market detail update self.req_id += 1 req = {"sub": f"market.{symbol}.detail", "id": str(self.req_id)} self.send_packet(req)
def to_tick(self): """ Generate TickData object from DbTickData. """ tick = TickData( symbol=self.symbol, exchange=Exchange(self.exchange), datetime=self.datetime, name=self.name, volume=self.volume, last_price=self.last_price, last_volume=self.last_volume, limit_up=self.limit_up, limit_down=self.limit_down, open_price=self.open_price, high_price=self.high_price, low_price=self.low_price, pre_close=self.pre_close, bid_price_1=self.bid_price_1, ask_price_1=self.ask_price_1, bid_volume_1=self.bid_volume_1, ask_volume_1=self.ask_volume_1, gateway_name="DB", ) if self.bid_price_2: tick.bid_price_2 = self.bid_price_2 tick.bid_price_3 = self.bid_price_3 tick.bid_price_4 = self.bid_price_4 tick.bid_price_5 = self.bid_price_5 tick.ask_price_2 = self.ask_price_2 tick.ask_price_3 = self.ask_price_3 tick.ask_price_4 = self.ask_price_4 tick.ask_price_5 = self.ask_price_5 tick.bid_volume_2 = self.bid_volume_2 tick.bid_volume_3 = self.bid_volume_3 tick.bid_volume_4 = self.bid_volume_4 tick.bid_volume_5 = self.bid_volume_5 tick.ask_volume_2 = self.ask_volume_2 tick.ask_volume_3 = self.ask_volume_3 tick.ask_volume_4 = self.ask_volume_4 tick.ask_volume_5 = self.ask_volume_5 return tick
def now(): return datetime.utcnow() bar = BarData( gateway_name="DB", symbol="test_symbol", exchange=Exchange.BITMEX, datetime=now(), interval=Interval.MINUTE, ) tick = TickData( gateway_name="DB", symbol="test_symbol", exchange=Exchange.BITMEX, datetime=now(), name="DB_test_symbol", ) class TestDatabase(unittest.TestCase): def connect(self, settings: dict): from fcs_trade.trader.database.initialize import init # noqa self.manager = init(settings) def assertBarCount(self, count, msg): bars = self.manager.load_bar_data(symbol=bar.symbol, exchange=bar.exchange, interval=bar.interval,
def on_data_update(self, data): """""" channel_id = data[0] channel, symbol = self.channelDict[channel_id] symbol = str(symbol.replace("t", "")) # Get the Tick object if symbol in self.tickDict: tick = self.tickDict[symbol] else: tick = TickData( symbol=symbol, exchange=Exchange.BITFINEX, name=symbol, datetime=datetime.now(), gateway_name=self.gateway_name, ) self.tickDict[symbol] = tick l_data1 = data[1] # Update general quote if channel == "ticker": tick.volume = float(l_data1[-3]) tick.high_price = float(l_data1[-2]) tick.low_price = float(l_data1[-1]) tick.last_price = float(l_data1[-4]) tick.open_price = float(tick.last_price - l_data1[4]) # Update deep quote elif channel == "book": bid = self.bidDict.setdefault(symbol, {}) ask = self.askDict.setdefault(symbol, {}) if len(l_data1) > 3: for price, count, amount in l_data1: price = float(price) count = int(count) amount = float(amount) if amount > 0: bid[price] = amount else: ask[price] = -amount else: price, count, amount = l_data1 price = float(price) count = int(count) amount = float(amount) if not count: if price in bid: del bid[price] elif price in ask: del ask[price] else: if amount > 0: bid[price] = amount else: ask[price] = -amount try: # BID bid_keys = bid.keys() bidPriceList = sorted(bid_keys, reverse=True) tick.bid_price_1 = bidPriceList[0] tick.bid_price_2 = bidPriceList[1] tick.bid_price_3 = bidPriceList[2] tick.bid_price_4 = bidPriceList[3] tick.bid_price_5 = bidPriceList[4] tick.bid_volume_1 = bid[tick.bid_price_1] tick.bid_volume_2 = bid[tick.bid_price_2] tick.bid_volume_3 = bid[tick.bid_price_3] tick.bid_volume_4 = bid[tick.bid_price_4] tick.bid_volume_5 = bid[tick.bid_price_5] # ASK ask_keys = ask.keys() askPriceList = sorted(ask_keys, reverse=True) tick.ask_price_1 = askPriceList[0] tick.ask_price_2 = askPriceList[1] tick.ask_price_3 = askPriceList[2] tick.ask_price_4 = askPriceList[3] tick.ask_price_5 = askPriceList[4] tick.ask_volume_1 = ask[tick.ask_price_1] tick.ask_volume_2 = ask[tick.ask_price_2] tick.ask_volume_3 = ask[tick.ask_price_3] tick.ask_volume_4 = ask[tick.ask_price_4] tick.ask_volume_5 = ask[tick.ask_price_5] except IndexError: return dt = datetime.now() tick.date = dt.strftime("%Y%m%d") tick.time = dt.strftime("%H:%M:%S.%f") tick.datetime = dt self.gateway.on_tick(copy(tick))