예제 #1
0
 def getContractSnapshot(self, contract):
     snapshot = self.api.quote.snapshots([contract])[0]
     code = snapshot.code
     exchange = Exchange.TSE if snapshot.exchange in ["TSE", "OTC"] else Exchange.TFE
     symbol = f"{code}.{exchange.value}"
     tick = self.ticks.get(symbol, None)
     if tick is None:
         self.code2contract[symbol] = contract
         if exchange == Exchange.TFE:
             name = f"{contract['name']}{contract['delivery_month']}"
         else:
             name = f"{contract['name']}"
         tick = TickData(
             symbol=code,
             exchange=exchange,
             name=name,
             datetime=datetime.fromtimestamp(snapshot.ts / 1000000000 - 8 * 60 * 60),
             gateway_name=self.gateway_name,
         )
     tick.volume = snapshot.total_volume
     tick.last_price = snapshot.close
     tick.limit_up = contract.limit_up
     tick.open_interest = 0
     tick.limit_down = contract.limit_down
     tick.open_price = snapshot.open
     tick.high_price = snapshot.high
     tick.low_price = snapshot.low
     tick.pre_close = contract.reference
     tick.bid_price_1 = snapshot.buy_price
     tick.bid_volume_1 = snapshot.buy_volume
     tick.ask_price_1 = snapshot.sell_price
     tick.ask_volume_1 = snapshot.sell_volume
     self.ticks[symbol] = tick
     self.on_tick(copy(tick))
    def subscribe(self, req: SubscribeRequest):
        """"""
        symbol = req.symbol
        self.ticks[symbol] = TickData(
            gateway_name=self.gateway_name,
            symbol=symbol,
            exchange=Exchange.DERIBIT,
            datetime=datetime.now(),
        )

        channels = [
            "ticker." + symbol + ".raw",
            "user.changes." + symbol + ".raw",
            "book." + symbol + ".raw",
        ]
        msg = {
            "jsonrpc": "2.0",
            "method": "private/subscribe",
            "id": self.id,
            "params": {
                "channels": channels,
                "access_token": self.access_token
            }
        }
        self.id += 1

        self.send_packet(msg)
예제 #3
0
 def qutote_futures_L(self, data):
     code = data.get('Code', None)
     if code is None:
         return
     tick = self.ticks.get(code, None)
     if tick is None:
         contract = self.code2contract.get(code, None)
         tick = TickData(
             symbol=code,
             exchange=Exchange.TFE,
             name=f"{contract['name']}{contract['delivery_month']}",
             datetime=datetime.now(),
             gateway_name=self.gateway_name,
         )
         self.ticks[code] = tick
     tick.datetime = datetime.strptime('{} {}'.format(
         data['Date'], data['Time']), "%Y/%m/%d %H:%M:%S.%f")
     tick.volume = data["VolSum"][0]
     tick.last_price = data["Close"][0]
     tick.limit_up = 0
     tick.open_interest = 0
     tick.limit_down = 0
     tick.open_price = data["Open"]
     tick.high_price = data["High"][0]
     tick.low_price = data["Low"][0]
     tick.pre_close = data["Close"][0] - data["DiffPrice"][0]
     return tick
예제 #4
0
파일: rq.py 프로젝트: cainiaocome/finutils
    def to_vnpy_tick(self):
        raise Exception('not implemented yet')
        from vnpy.trader.constant import Exchange, Interval
        from vnpy.trader.object import TickData, BarData
        from vnpy.trader.utility import BarGenerator

        mapper = {
            'bid_price1': 'bid_price_1',
            'bid_volume1': 'bid_volume_1',
            'ask_price1': 'ask_price_1',
            'ask_volume1': 'ask_volume_1',
            'highest': 'high_price',
            'lowest': 'low_price',
        }
        df = self.df.copy()
        # vnpy tick里面没有amount
        # volume_in_tick, spread是自己计算的数据,vnpy tick初始化的时候也不需要
        df.drop(columns=['amount', 'volume_in_tick', 'spread'], inplace=True)
        df.rename(columns=mapper, inplace=True)
        for ix, row in df.iterrows():
            kwargs = dict(row)
            kwargs.update({
                'symbol': self.symbol,
                'exchange': self.exchange,
                'gateway_name': 'tq',
            })
            yield TickData(**(kwargs))
 def load_ticks(self, csv_path: str):
     ticks = []
     with codecs.open(csv_path, "r", "utf-8") as f:
         reader = csv.DictReader(f)
         for item in reader:
             dt = datetime.strptime(item['datetime'], '%Y-%m-%d %H:%M:%S')
             tick = TickData(
                 symbol=item['symbol'],
                 exchange=Exchange(item['exchange']),
                 datetime=dt,
                 name=item['name'],
                 volume=self.covertf(item['volume']),
                 open_interest=self.covertf(item['open_interest']),
                 last_price=self.covertf(item['last_price']),
                 last_volume=self.covertf(item['last_volume']),
                 limit_up=self.covertf(item['limit_up']),
                 limit_down=self.covertf(item['limit_down']),
                 open_price=self.covertf(item['open_price']),
                 high_price=self.covertf(item['high_price']),
                 low_price=self.covertf(item['low_price']),
                 pre_close=self.covertf(item['pre_close']),
                 bid_price_1=self.covertf(item['bid_price_1']),
                 ask_price_1=self.covertf(item['ask_price_1']),
                 bid_volume_1=self.covertf(item['bid_volume_1']),
                 ask_volume_1=self.covertf(item['ask_volume_1']),
                 gateway_name="JQ")
             ticks.append(tick)
     return ticks
예제 #6
0
    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)
예제 #7
0
    def update_account(self, dt, balance):
        """
        更新资金曲线
        :param dt:
        :param balance: 账号级别,直接使用账号得的balance;
        :return:
        """
        tick = TickData(gateway_name='Fund',
                        symbol=self.symbol,
                        exchange=Exchange.LOCAL,
                        datetime=dt)
        tick.last_price = balance
        tick.volume = 1
        tick.ask_price_1 = balance
        tick.ask_volume_1 = 1
        tick.bid_price_1 = balance
        tick.bid_volume_1 = 1
        tick.date = tick.datetime.strftime('%Y-%m-%d')
        tick.time = tick.datetime.strftime('%H:%M:%S')
        tick.trading_day = get_trading_date(dt)
        tick.open_interest = balance

        if self.inited:
            self.kline.on_tick(tick)

        # 如果是从账号更新,无法更新持仓盈亏
        self.closed_profit = balance
        self.holding_profit = 0
예제 #8
0
    def subscribe(self, req: SubscribeRequest):
        """
        Subscribe to tick data upate.
        """
        self.subscribed[req.vt_symbol] = req

        tick = TickData(
            symbol=req.symbol,
            exchange=req.exchange,
            name=req.symbol,
            datetime=datetime.now(UTC_TZ),
            gateway_name=self.gateway_name,
        )
        self.ticks[req.symbol] = tick

        channel_ticker = f"swap/ticker:{req.symbol}"
        channel_depth = f"swap/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)
예제 #9
0
    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" it does not support exchange {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
예제 #10
0
    def subscribe(self, req: SubscribeRequest):
        """
        Subscribe to tick data upate.
        """
        tick = TickData(
            symbol=req.symbol,
            exchange=req.exchange,
            name=req.symbol,
            datetime=datetime.now(UTC_TZ),
            gateway_name=self.gateway_name,
        )
        self.ticks[req.symbol] = tick

        tick_req = self.generate_req(
            channel="futures.tickers",
            event="subscribe",
            pay_load=[req.symbol]
        )
        self.send_packet(tick_req)

        depth_req = self.generate_req(
            channel="futures.order_book",
            event="subscribe",
            pay_load=[req.symbol, "5", "0"]
        )
        self.send_packet(depth_req)
예제 #11
0
    def subscribe(self, req: SubscribeRequest):
        """
        Subscribe to tick data upate.
        订阅tick数据来更新
        """
        tick = TickData(
            symbol=req.symbol,
            exchange=req.exchange,
            name=req.symbol,
            timestamp=0.0,
            datetime=datetime.now(),
            gateway_name=self.gateway_name,
        )
        self.ticks[req.symbol] = tick
        # 现货 ticker数据  和行情深度
        sopt1, sopt2 = req.symbol.split("_")
        channel_ticker = f'{sopt1}{sopt2}_ticker'
        channel_depth = f"{sopt1}{sopt2}_depth"

        self.callbacks[channel_ticker] = self.on_ticker
        self.callbacks[channel_depth] = self.on_depth
        # websocket 订阅
        req = {
            "event": "addChannel",
            "channel": [channel_ticker, channel_depth]
        }
        self.send_packet(req)
예제 #12
0
    def onRtnDepthMarketData(self, data: dict):
        """
        Callback of tick data update.
        """
        # Filter data update with no timestamp
        if not data["UpdateTime"]:
            return

        symbol = data["InstrumentID"]
        exchange = symbol_exchange_map.get(symbol, "")
        if not exchange:
            return

        timestamp = f"{self.current_date} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
        dt = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
        dt = CHINA_TZ.localize(dt)

        tick = TickData(
            symbol=symbol,
            exchange=exchange,
            datetime=dt,
            name=symbol_name_map[symbol],
            volume=data["Volume"],
            open_interest=data["OpenInterest"],
            last_price=adjust_price(data["LastPrice"]),
            limit_up=data["UpperLimitPrice"],
            limit_down=data["LowerLimitPrice"],
            open_price=adjust_price(data["OpenPrice"]),
            high_price=adjust_price(data["HighestPrice"]),
            low_price=adjust_price(data["LowestPrice"]),
            pre_close=adjust_price(data["PreClosePrice"]),
            bid_price_1=adjust_price(data["BidPrice1"]),
            ask_price_1=adjust_price(data["AskPrice1"]),
            bid_volume_1=data["BidVolume1"],
            ask_volume_1=data["AskVolume1"],
            gateway_name=self.gateway_name
        )

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = data["BidVolume2"]
            tick.bid_volume_3 = data["BidVolume3"]
            tick.bid_volume_4 = data["BidVolume4"]
            tick.bid_volume_5 = data["BidVolume5"]

            tick.ask_volume_2 = data["AskVolume2"]
            tick.ask_volume_3 = data["AskVolume3"]
            tick.ask_volume_4 = data["AskVolume4"]
            tick.ask_volume_5 = data["AskVolume5"]

        self.gateway.on_tick(tick)
예제 #13
0
    def load_tick_data(
        self,
        symbol: str,
        exchange: Exchange,
        start: datetime,
        end: datetime
    ) -> List[TickData]:
        """读取TICK数据"""
        filter = {
            "symbol": symbol,
            "exchange": exchange.value,
            "datetime": {
                "$gte": start,
                "$lte": end
            }
        }

        c: Cursor = self.tick_collection.find(filter)

        ticks = []
        for d in c:
            d["exchange"] = Exchange(d["exchange"])
            d["gateway_name"] = "DB"
            d.pop("_id")

            tick = TickData(**d)
            ticks.append(tick)

        return ticks
예제 #14
0
    def on_query_quotes(self, data: dict, request: Request) -> None:
        """"""
        if self.check_error(data, "查询行情"):
            return

        for d in data["data"]["responseBody"]:
            if d["goodsCode"] not in self.subscribe_symbol:
                continue
            
            dt = CHINA_TZ.localize(datetime.now())

            tick = TickData(
                symbol=d["goodsCode"],
                exchange=Exchange.SR,
                datetime=dt,
                name=d["goodsName"],
                volume=int(d["transactionVolume"]),
                # open_interest=d["currentQuantity"],
                last_price=int(d["newDealPrice"]),
                limit_up=self.up_dn_limit.get(d["goodsCode"], 0)["limit_up"],
                limit_down=self.up_dn_limit.get(d["goodsCode"], 0)["limit_down"],
                open_price=int(d["openingPrice"]),
                high_price=int(d["highestPrice"]),
                low_price=int(d["lowestPrice"]),
                pre_close=int(d["closePrice"]),
                bid_price_1=int(d["buyPrice1"]),
                ask_price_1=int(d["sellPrice1"]),
                bid_volume_1=int(d["buyContractVolume1"]),
                ask_volume_1=int(d["sellContractVolume1"]),
                gateway_name=self.gateway_name
            )

            self.gateway.on_tick(tick)
예제 #15
0
    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,
        )
        # 先在websocket gateway这里面存储tick
        self.ticks[req.symbol] = tick

        # 订阅ticker和5档深度行情频道
        channel_ticker = f"spot/ticker:{req.symbol}"
        channel_depth = f"spot/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)
예제 #16
0
    def subscribe_by_symbol(self, symbol: str) -> None:
        """"""
        if symbol not in symbol_contract_map:
            self.gateway.write_log(f"找不到该合约代码{symbol}")
            return

        # Create tick data buffer
        tick = TickData(
            symbol=symbol,
            name=symbol_contract_map[symbol].name,
            exchange=Exchange.HUOBI,
            datetime=datetime.now(CHINA_TZ),
            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)
예제 #17
0
    def subscribe(self, req: SubscribeRequest):
        """"""
        if req.symbol not in symbol_name_map:
            self.gateway.write_log(f"找不到该合约代码{req.symbol}")
            return

        # Create tick buf data
        tick = TickData(
            symbol=req.symbol,
            name=symbol_name_map.get(req.symbol, ""),
            exchange=Exchange.BINANCE,
            datetime=datetime.now(CHINA_TZ),
            gateway_name=self.gateway_name,
        )
        self.ticks[req.symbol] = tick

        # Close previous connection
        if self._active:
            self.stop()
            self.join()

        # Create new connection
        channels = []
        for ws_symbol in self.ticks.keys():
            channels.append(ws_symbol + "@ticker")
            channels.append(ws_symbol + "@depth5")

        url = WEBSOCKET_DATA_HOST + "/".join(channels)
        self.init(url, self.proxy_host, self.proxy_port)
        self.start()
예제 #18
0
    def on_quote_change(self, tiger_symbol: str, data: list, trading: bool):
        """"""
        data = dict(data)
        symbol, exchange = convert_symbol_tiger2vt(tiger_symbol)

        tick = self.ticks.get(symbol, None)
        if not tick:
            tick = TickData(
                symbol=symbol,
                exchange=exchange,
                gateway_name=self.gateway_name,
                datetime=datetime.now(),
                name=self.symbol_names[symbol],
            )
            self.ticks[symbol] = tick

        tick.datetime = datetime.fromtimestamp(int(data["timestamp"]) / 1000)
        tick.pre_close = data.get("prev_close", tick.pre_close)
        tick.last_price = data.get("latest_price", tick.last_price)
        tick.volume = data.get("volume", tick.volume)
        tick.open_price = data.get("open", tick.open_price)
        tick.high_price = data.get("high", tick.high_price)
        tick.low_price = data.get("low", tick.low_price)
        tick.ask_price_1 = data.get("ask_price", tick.ask_price_1)
        tick.bid_price_1 = data.get("bid_price", tick.bid_price_1)
        tick.ask_volume_1 = data.get("ask_size", tick.ask_volume_1)
        tick.bid_volume_1 = data.get("bid_size", tick.bid_volume_1)

        self.on_tick(copy(tick))
예제 #19
0
    def on_price_info(self, packet: dict) -> None:
        """"""
        if "data" not in packet:
            return

        for d in packet["data"]:

            tick = TickData(
                symbol=d["symbol"].replace('.', '-'),
                exchange=Exchange.OTC,
                name=d["symbol"].replace('.', '-'),
                bid_price_1=d["bid"],
                ask_price_1=d["ask"],
                volume=d["last_volume"],
                datetime=datetime.now(),
                gateway_name=self.gateway_name
            )
            if tick.last_price:
                tick.last_price = d["last"]
                tick.high_price = d["last_high"]
                tick.low_price = d["last_low"]
            else:
                tick.last_price = (d["bid"] + d["ask"]) / 2
                tick.high_price = (d["bid_high"] + d["ask_high"]) / 2
                tick.low_price = (d["bid_low"] + d["ask_low"]) / 2

            self.on_tick(tick)
예제 #20
0
    def onDepthMarketData(self, data: dict) -> None:
        """"""
        timestamp = str(data["data_time"])
        dt = datetime.strptime(timestamp, "%Y%m%d%H%M%S%f")

        tick = TickData(symbol=data["ticker"],
                        exchange=EXCHANGE_XTP2VT[data["exchange_id"]],
                        datetime=dt,
                        volume=data["qty"],
                        last_price=data["last_price"],
                        limit_up=data["upper_limit_price"],
                        limit_down=data["lower_limit_price"],
                        open_price=data["open_price"],
                        high_price=data["high_price"],
                        low_price=data["low_price"],
                        pre_close=data["pre_close_price"],
                        gateway_name=self.gateway_name)

        tick.bid_price_1, tick.bid_price_2, tick.bid_price_3, tick.bid_price_4, tick.bid_price_5 = data[
            "bid"][0:5]
        tick.ask_price_1, tick.ask_price_2, tick.ask_price_3, tick.ask_price_4, tick.ask_price_5 = data[
            "ask"][0:5]
        tick.bid_volume_1, tick.bid_volume_2, tick.bid_volume_3, tick.bid_volume_4, tick.bid_volume_5 = data[
            "bid_qty"][0:5]
        tick.ask_volume_1, tick.ask_volume_2, tick.ask_volume_3, tick.ask_volume_4, tick.ask_volume_5 = data[
            "ask_qty"][0:5]

        tick.name = symbol_name_map.get(tick.vt_symbol, tick.symbol)
        self.gateway.on_tick(tick)
예제 #21
0
    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,
            timestamp=0.0,
            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)
예제 #22
0
    def subscribe(self, req: SubscribeRequest):
        """"""
        self.subscribed[req.symbol] = req
        if not self._active:
            return

        tick = TickData(
            symbol=req.symbol,
            name=symbol_name_map.get(req.symbol, ""),
            exchange=Exchange.BITSTAMP,
            datetime=datetime.now(UTC_TZ),
            gateway_name=self.gateway_name,
        )

        for prefix in [
            "order_book_",
            "live_trades_",
            "live_orders_"
        ]:
            channel = f"{prefix}{req.symbol}"
            d = {
                "event": "bts:subscribe",
                "data": {
                    "channel": channel
                }
            }
            self.ticks[channel] = tick
            self.send_packet(d)
예제 #23
0
    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)
예제 #24
0
    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

        # Extract ib contract detail
        ib_contract = generate_ib_contract(req.symbol, req.exchange)
        if not ib_contract:
            self.gateway.write_log("代码解析失败,请检查格式是否正确")
            return

        # 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
예제 #25
0
    def subscribe(self, req: SubscribeRequest) -> None:
        """订阅行情"""
        symbol: str = req.symbol

        # 过滤重复的订阅
        if symbol in self.ticks:
            return

        # 创建TICK对象
        tick: TickData = TickData(
            gateway_name=self.gateway_name,
            symbol=symbol,
            name=symbol,
            exchange=Exchange.DERIBIT,
            datetime=datetime.now(LOCAL_TZ),
        )
        self.ticks[symbol] = tick

        # 发出订阅请求
        params: dict = {
            "channels": [
                f"ticker.{symbol}.100ms",
                f"book.{symbol}.none.10.100ms"
            ]
        }

        self.send_request("public/subscribe", params)
예제 #26
0
    def load_tick_data(self, symbol: str, exchange: Exchange, start: datetime,
                       end: datetime) -> List[TickData]:
        """"""
        query = ("select * from tick_data"
                 " where vt_symbol=$vt_symbol"
                 f" and time >= '{start.date().isoformat()}'"
                 f" and time <= '{end.date().isoformat()}';")

        bind_params = {
            "vt_symbol": generate_vt_symbol(symbol, exchange),
        }

        result = self.client.query(query, bind_params=bind_params)
        points = result.get_points()

        ticks: List[TickData] = []
        for d in points:
            dt = datetime.strptime(d["time"], "%Y-%m-%dT%H:%M:%SZ")

            tick = TickData(symbol=symbol,
                            exchange=exchange,
                            datetime=dt.astimezone(DB_TZ),
                            name=d["name"],
                            volume=d["volume"],
                            turnover=d["turnover"],
                            open_interest=d["open_interest"],
                            last_price=d["last_price"],
                            last_volume=d["last_volume"],
                            limit_up=d["limit_up"],
                            limit_down=d["limit_down"],
                            open_price=d["open_price"],
                            high_price=d["high_price"],
                            low_price=d["low_price"],
                            pre_close=d["pre_close"],
                            bid_price_1=d["bid_price_1"],
                            bid_price_2=d["bid_price_2"],
                            bid_price_3=d["bid_price_3"],
                            bid_price_4=d["bid_price_4"],
                            bid_price_5=d["bid_price_5"],
                            ask_price_1=d["ask_price_1"],
                            ask_price_2=d["ask_price_2"],
                            ask_price_3=d["ask_price_3"],
                            ask_price_4=d["ask_price_4"],
                            ask_price_5=d["ask_price_5"],
                            bid_volume_1=d["bid_volume_1"],
                            bid_volume_2=d["bid_volume_2"],
                            bid_volume_3=d["bid_volume_3"],
                            bid_volume_4=d["bid_volume_4"],
                            bid_volume_5=d["bid_volume_5"],
                            ask_volume_1=d["ask_volume_1"],
                            ask_volume_2=d["ask_volume_2"],
                            ask_volume_3=d["ask_volume_3"],
                            ask_volume_4=d["ask_volume_4"],
                            ask_volume_5=d["ask_volume_5"],
                            localtime=d["localtime"],
                            gateway_name="DB")
            ticks.append(tick)

        return ticks
예제 #27
0
    def load_tick_data(
        self,
        symbol: str,
        exchange: Exchange,
        start: datetime,
        end: datetime
    ) -> List[TickData]:
        """"""
        s: QuerySet = DbTickData.objects(
            symbol=symbol,
            exchange=exchange.value,
            datetime__gte=convert_tz(start),
            datetime__lte=convert_tz(end),
        )

        ticks: List[TickData] = []
        for db_tick in s:
            tick = TickData(
                symbol=db_tick.symbol,
                exchange=Exchange(db_tick.exchange),
                datetime=db_tick.datetime.astimezone(DB_TZ),
                name=db_tick.name,
                volume=db_tick.volume,
                turnover=db_tick.turnover,
                open_interest=db_tick.open_interest,
                last_price=db_tick.last_price,
                last_volume=db_tick.last_volume,
                limit_up=db_tick.limit_up,
                limit_down=db_tick.limit_down,
                open_price=db_tick.open_price,
                high_price=db_tick.high_price,
                low_price=db_tick.low_price,
                pre_close=db_tick.pre_close,
                bid_price_1=db_tick.bid_price_1,
                bid_price_2=db_tick.bid_price_2,
                bid_price_3=db_tick.bid_price_3,
                bid_price_4=db_tick.bid_price_4,
                bid_price_5=db_tick.bid_price_5,
                ask_price_1=db_tick.ask_price_1,
                ask_price_2=db_tick.ask_price_2,
                ask_price_3=db_tick.ask_price_3,
                ask_price_4=db_tick.ask_price_4,
                ask_price_5=db_tick.ask_price_5,
                bid_volume_1=db_tick.bid_volume_1,
                bid_volume_2=db_tick.bid_volume_2,
                bid_volume_3=db_tick.bid_volume_3,
                bid_volume_4=db_tick.bid_volume_4,
                bid_volume_5=db_tick.bid_volume_5,
                ask_volume_1=db_tick.ask_volume_1,
                ask_volume_2=db_tick.ask_volume_2,
                ask_volume_3=db_tick.ask_volume_3,
                ask_volume_4=db_tick.ask_volume_4,
                ask_volume_5=db_tick.ask_volume_5,
                localtime=db_tick.localtime,
                gateway_name="DB"
            )
            ticks.append(tick)

        return ticks
예제 #28
0
def move_df_to_mongodb(imported_data:pd.DataFrame,collection_name:str):
    ticks = []
    start = None
    count = 0
    utc_8 = timezone(timedelta(hours=8))
    for row in imported_data.itertuples():

        tick = TickData(
              symbol = row.symbol,
              exchange = row.exchange,
              datetime = row.datetime.replace(tzinfo=utc_8),
              #datetime = row.datetime,
              name = "TickDataName",
              volume = row.volume,
              open_interest = row.open_interest,
              turnover = row.turnover,
              last_price = row.last_price,
              last_volume = row.last_volume,
              last_amount = row.last_amount,
              limit_up = row.limit_up,
              limit_down = row.limit_down,
              open_price = row.open_price,
              high_price = row.high_price,
              low_price = row.low_price,
              pre_close = row.pre_close,
              bid_price_1 = row.bid_price_1,
              bid_price_2 = row.bid_price_2,
              bid_price_3 = row.bid_price_3,
              bid_price_4 = row.bid_price_4,
              bid_price_5 = row.bid_price_5,
              ask_price_1 = row.ask_price_1,
              ask_price_2 = row.ask_price_2,
              ask_price_3 = row.ask_price_3,
              ask_price_4 = row.ask_price_4,
              ask_price_5 = row.ask_price_5,
              bid_volume_1 = row.bid_volume_1,
              bid_volume_2 = row.bid_volume_2,
              bid_volume_3 = row.bid_volume_3,
              bid_volume_4 = row.bid_volume_4,
              bid_volume_5 = row.bid_volume_5,
              ask_volume_1 = row.ask_volume_1,
              ask_volume_2 = row.ask_volume_2,
              ask_volume_3 = row.ask_volume_3,
              ask_volume_4 = row.ask_volume_4,
              ask_volume_5 = row.ask_volume_5,
              gateway_name="DB",
        )
        ticks.append(tick)

        # do some statistics
        count += 1
        if not start:
            start = tick.datetime
    end = tick.datetime

    # insert into database
    database_manager.save_tick_data(ticks, collection_name)
    print(f'Insert Tick: {count} from {start} - {end}')
예제 #29
0
    def onRtnDepthMarketData(self, data: dict) -> None:
        """行情数据推送"""
        # 过滤没有时间戳的异常行情数据
        if not data["UpdateTime"]:
            return

        symbol: str = data["InstrumentID"]
        contract: ContractData = symbol_contract_map.get(symbol, None)
        if not contract:
            return

        timestamp: str = f"{self.current_date} {data['UpdateTime']}.{int(data['UpdateMillisec']/100)}"
        dt: datetime = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S.%f")
        dt = CHINA_TZ.localize(dt)

        tick: TickData = TickData(
            symbol=symbol,
            exchange=contract.exchange,
            datetime=dt,
            name=contract.name,
            volume=data["Volume"],
            open_interest=data["OpenInterest"],
            last_price=adjust_price(data["LastPrice"]),
            limit_up=data["UpperLimitPrice"],
            limit_down=data["LowerLimitPrice"],
            open_price=adjust_price(data["OpenPrice"]),
            high_price=adjust_price(data["HighestPrice"]),
            low_price=adjust_price(data["LowestPrice"]),
            pre_close=adjust_price(data["PreClosePrice"]),
            bid_price_1=adjust_price(data["BidPrice1"]),
            ask_price_1=adjust_price(data["AskPrice1"]),
            bid_volume_1=data["BidVolume1"],
            ask_volume_1=data["AskVolume1"],
            gateway_name=self.gateway_name
        )

        if data["BidVolume2"] or data["AskVolume2"]:
            tick.bid_price_2 = adjust_price(data["BidPrice2"])
            tick.bid_price_3 = adjust_price(data["BidPrice3"])
            tick.bid_price_4 = adjust_price(data["BidPrice4"])
            tick.bid_price_5 = adjust_price(data["BidPrice5"])

            tick.ask_price_2 = adjust_price(data["AskPrice2"])
            tick.ask_price_3 = adjust_price(data["AskPrice3"])
            tick.ask_price_4 = adjust_price(data["AskPrice4"])
            tick.ask_price_5 = adjust_price(data["AskPrice5"])

            tick.bid_volume_2 = data["BidVolume2"]
            tick.bid_volume_3 = data["BidVolume3"]
            tick.bid_volume_4 = data["BidVolume4"]
            tick.bid_volume_5 = data["BidVolume5"]

            tick.ask_volume_2 = data["AskVolume2"]
            tick.ask_volume_3 = data["AskVolume3"]
            tick.ask_volume_4 = data["AskVolume4"]
            tick.ask_volume_5 = data["AskVolume5"]

        self.gateway.on_tick(tick)
예제 #30
0
    def on_tick_data(self, data: list) -> None:
        """"""
        if not data:
            error = dict()
            error["error_id"] = "pytdx"
            error["error_msg"] = "tick数据为空"
            self.on_error(error)
            return

        dt = datetime.now()
        for d in data:
            tick = TickData(
                symbol=d["code"],
                exchange=MARKET_PT2VT[d["market"]],
                datetime=dt,
                volume=d["vol"],
                last_price=d["price"],
                open_price=d["open"],
                high_price=d["high"],
                low_price=d["low"],
                pre_close=d["last_close"],
                gateway_name=self.gateway_name,
                bid_price_1=d["bid1"],
                bid_price_2=d["bid2"],
                bid_price_3=d["bid3"],
                bid_price_4=d["bid4"],
                bid_price_5=d["bid5"],
                ask_price_1=d["ask1"],
                ask_price_2=d["ask2"],
                ask_price_3=d["ask3"],
                ask_price_4=d["ask4"],
                ask_price_5=d["ask5"],
                bid_volume_1=d["bid_vol1"],
                bid_volume_2=d["bid_vol2"],
                bid_volume_3=d["bid_vol3"],
                bid_volume_4=d["bid_vol4"],
                bid_volume_5=d["bid_vol5"],
                ask_volume_1=d["ask_vol1"],
                ask_volume_2=d["ask_vol2"],
                ask_volume_3=d["ask_vol3"],
                ask_volume_4=d["ask_vol4"],
                ask_volume_5=d["ask_vol5"],
            )
            # pricetick = symbol_pricetick_map.get(tick.vt_symbol, 0)
            # if pricetick:
            #     tick.bid_price_1 = round_to(tick.bid_price_1, pricetick)
            #     tick.bid_price_2 = round_to(tick.bid_price_2, pricetick)
            #     tick.bid_price_3 = round_to(tick.bid_price_3, pricetick)
            #     tick.bid_price_4 = round_to(tick.bid_price_4, pricetick)
            #     tick.bid_price_5 = round_to(tick.bid_price_5, pricetick)
            #     tick.ask_price_1 = round_to(tick.ask_price_1, pricetick)
            #     tick.ask_price_2 = round_to(tick.ask_price_2, pricetick)
            #     tick.ask_price_3 = round_to(tick.ask_price_3, pricetick)
            #     tick.ask_price_4 = round_to(tick.ask_price_4, pricetick)
            #     tick.ask_price_5 = round_to(tick.ask_price_5, pricetick)

            tick.name = symbol_name_map.get(tick.vt_symbol, tick.symbol)
            self.gateway.on_tick(tick)