Beispiel #1
0
def get_new_subscription():
    symbols = Deribit.get_instruments()
    new_subscription = defaultdict(set)
    for symbol in symbols:
        for feed_type in subscription[get_instrument_type(symbol)]:
            new_subscription[feed_type].add(symbol)
    return new_subscription
Beispiel #2
0
 async def _user_trade(self, msg: dict, timestamp: float):
     """
     {
         "params":
         {
             "data":
             [
                 {
                     # "trade_seq" : 30289432,
                     # "trade_id" : "48079254",
                     # "timestamp" : 1590484156350,
                     "tick_direction" : 0,
                     "state" : "filled",
                     "self_trade" : false,
                     "reduce_only" : false,
                     # "price" : 8954,
                     "post_only" : false,
                     "order_type" : "market",
                     # "order_id" : "4008965646",
                     "matching_id" : null,
                     # "mark_price" : 8952.86,
                     "liquidity" : "T",
                     # "instrument_name" : "BTC-PERPETUAL",
                     # "index_price" : 8956.73,
                     "fee_currency" : "BTC",
                     "fee" : 0.00000168,
                     # "direction" : "sell",
                     # "amount" : 20
                 }
             ],
             "channel": "user.trades.BTC-PERPETUAL.raw"
         },
         "method": "subscription",
         "jsonrpc": "2.0"
     }
     """
     for trade in msg["params"]["data"]:
         kwargs = {}
         kwargs['trade_seq'] = Decimal(trade['trade_seq'])
         kwargs['mark_price'] = Decimal(trade['mark_price'])
         kwargs['index_price'] = Decimal(trade['index_price'])
         if get_instrument_type(trade["instrument_name"]) == OPTION:
             kwargs['iv'] = Decimal(trade['iv'])
             kwargs['underlying_price'] = Decimal(trade['underlying_price'])
         await self.callback(USER_TRADES,
                             feed=self.id,
                             symbol=trade["instrument_name"],
                             order_id=trade['trade_id'],
                             side=BUY if trade['direction'] == 'buy' else SELL,
                             amount=Decimal(trade['amount']),
                             price=Decimal(trade['price']),
                             timestamp=timestamp_normalize(self.id, trade['timestamp']),
                             receipt_timestamp=timestamp,
                             **kwargs
                             )
Beispiel #3
0
 async def _trade(self, msg: dict, timestamp: float):
     """
     {
         "params":
         {
             "data":
             [
                 {
                     "trade_seq": 933,
                     "trade_id": "9178",
                     "timestamp": 1550736299753,
                     "tick_direction": 3,
                     "price": 3948.69,
                     "instrument_name": "BTC-PERPETUAL",
                     "index_price": 3930.73,
                     "direction": "sell",
                     "amount": 10
                 }
             ],
             "channel": "trades.BTC-PERPETUAL.raw"
         },
         "method": "subscription",
         "jsonrpc": "2.0"
     }
     """
     for trade in msg["params"]["data"]:
         kwargs = {}
         kwargs['trade_seq'] = Decimal(trade['trade_seq'])
         kwargs['mark_price'] = Decimal(trade['mark_price'])
         kwargs['index_price'] = Decimal(trade['index_price'])
         if get_instrument_type(trade["instrument_name"]) == OPTION:
             kwargs['iv'] = Decimal(trade['iv'])
         await self.callback(TRADES,
                             feed=self.id,
                             symbol=self.exchange_symbol_to_std_symbol(trade["instrument_name"]),
                             order_id=trade['trade_id'],
                             side=BUY if trade['direction'] == 'buy' else SELL,
                             amount=Decimal(trade['amount']),
                             price=Decimal(trade['price']),
                             timestamp=timestamp_normalize(self.id, trade['timestamp']),
                             receipt_timestamp=timestamp,
                             **kwargs
                             )
         if 'liquidation' in trade:
             await self.callback(LIQUIDATIONS,
                                 feed=self.id,
                                 symbol=self.exchange_symbol_to_std_symbol(trade["instrument_name"]),
                                 side=BUY if trade['direction'] == 'buy' else SELL,
                                 leaves_qty=Decimal(trade['amount']),
                                 price=Decimal(trade['price']),
                                 order_id=trade['trade_id'],
                                 status=FILLED,
                                 timestamp=timestamp_normalize(self.id, trade['timestamp']),
                                 receipt_timestamp=timestamp
                                 )
Beispiel #4
0
 async def __call__(self,
                    *,
                    feed: str,
                    symbol: str,
                    bid: Decimal,
                    bid_amount: Decimal,
                    ask: Decimal,
                    ask_amount: Decimal,
                    timestamp: float,
                    receipt_timestamp: float,
                    bid_iv: Decimal = None,
                    ask_iv: Decimal = None,
                    delta: Decimal = None,
                    gamma: Decimal = None,
                    rho: Decimal = None,
                    theta: Decimal = None,
                    vega: Decimal = None,
                    mark_price: Decimal = None,
                    mark_iv: Decimal = None,
                    underlying_index: str = None,
                    underlying_price: Decimal = None):
     instrument_type = get_instrument_type(symbol)
     if not instrument_type in self.callbacks:
         return
     if instrument_type == PERPETUAL:
         await self.callbacks[instrument_type](feed, symbol, bid,
                                               bid_amount, ask, ask_amount,
                                               timestamp, receipt_timestamp)
     elif instrument_type == OPTION:
         await self.callbacks[instrument_type](
             feed, symbol, bid, bid_amount, ask, ask_amount, timestamp,
             receipt_timestamp, bid_iv, ask_iv, delta, gamma, rho, theta,
             vega, mark_price, mark_iv, underlying_index, underlying_price)
     else:
         await self.callbacks[instrument_type](feed, symbol, bid,
                                               bid_amount, ask, ask_amount,
                                               timestamp, receipt_timestamp)
Beispiel #5
0
    async def _ticker(self, msg: dict, timestamp: float):
        '''
        {
            "params" : {
                "data" : {
                    "timestamp" : 1550652954406,
                    "stats" : {
                        "volume" : null,
                        "low" : null,
                        "high" : null
                    },
                    "state" : "open",
                    "settlement_price" : 3960.14,
                    "open_interest" : 0.12759952124659626,
                    "min_price" : 3943.21,
                    "max_price" : 3982.84,
                    "mark_price" : 3940.06,
                    "last_price" : 3906,
                    "instrument_name" : "BTC-PERPETUAL",
                    "index_price" : 3918.51,
                    "funding_8h" : 0.01520525,
                    "current_funding" : 0.00499954,
                    "best_bid_price" : 3914.97,
                    "best_bid_amount" : 40,
                    "best_ask_price" : 3996.61,
                    "best_ask_amount" : 50
                    },
                "channel" : "ticker.BTC-PERPETUAL.raw"
            },
            "method" : "subscription",
            "jsonrpc" : "2.0"}
        '''
        pair = self.exchange_symbol_to_std_symbol(msg['params']['data']['instrument_name'])
        
        bid = Decimal(msg["params"]["data"]["best_bid_price"])
        bid_amount = Decimal(msg["params"]["data"]["best_bid_amount"])
        ask = Decimal(msg["params"]["data"]['best_ask_price'])
        ask_amount = Decimal(msg["params"]["data"]["best_ask_amount"])
        if pair in self.snapshot and self.snapshot[pair] == (bid, bid_amount, ask, ask_amount):
            return
        self.snapshot[pair] = (bid, bid_amount, ask, ask_amount)

        ts = timestamp_normalize(self.id, msg['params']['data']['timestamp'])
        kwargs = {}
        instrument_type = get_instrument_type(pair)
        if instrument_type == OPTION:
            kwargs['bid_iv'] = Decimal(msg["params"]["data"]["bid_iv"])
            kwargs['ask_iv'] = Decimal(msg["params"]["data"]["ask_iv"])
            kwargs['delta'] = Decimal(msg["params"]["data"]["greeks"]["delta"])
            kwargs['gamma'] = Decimal(msg["params"]["data"]["greeks"]["gamma"])
            kwargs['rho'] = Decimal(msg["params"]["data"]["greeks"]["rho"])
            kwargs['theta'] = Decimal(msg["params"]["data"]["greeks"]["theta"])
            kwargs['vega'] = Decimal(msg["params"]["data"]["greeks"]["vega"])
            kwargs['mark_price'] = Decimal(msg["params"]["data"]["mark_price"])
            kwargs['mark_iv'] = Decimal(msg["params"]["data"]["mark_iv"])
            kwargs['underlying_index'] = msg["params"]["data"]["underlying_index"]
            kwargs['underlying_price'] = Decimal(msg["params"]["data"]["underlying_price"])

        await self.callback(TICKER, feed=self.id,
                            symbol=pair,
                            bid=bid,
                            bid_amount=bid_amount,
                            ask=ask,
                            ask_amount=ask_amount,
                            timestamp=ts,
                            receipt_timestamp=timestamp,
                            **kwargs)

        if "current_funding" in msg["params"]["data"] and "funding_8h" in msg["params"]["data"]:
            await self.callback(FUNDING, feed=self.id,
                                symbol=pair,
                                timestamp=ts,
                                receipt_timestamp=timestamp,
                                rate=msg["params"]["data"]["current_funding"],
                                rate_8h=msg["params"]["data"]["funding_8h"])
        oi = msg['params']['data']['open_interest']
        if pair in self.open_interest and oi == self.open_interest[pair]:
            return
        self.open_interest[pair] = oi
        await self.callback(OPEN_INTEREST,
                            feed=self.id,
                            symbol=pair,
                            open_interest=oi,
                            timestamp=ts,
                            receipt_timestamp=timestamp
                            )