diff --git a/piker/brokers/deribit/api.py b/piker/brokers/deribit/api.py index 480f2ba0..41b39eec 100644 --- a/piker/brokers/deribit/api.py +++ b/piker/brokers/deribit/api.py @@ -62,6 +62,11 @@ from .venues import ( PAIRTYPES, Pair, OptionPair, + JSONRPCResult, + JSONRPCChannel, + KLinesResult, + Trade, + LastTradesResult, ) from piker.accounting import ( Asset, @@ -95,55 +100,6 @@ _ws_url = 'wss://www.deribit.com/ws/api/v2' _testnet_ws_url = 'wss://test.deribit.com/ws/api/v2' -class JSONRPCResult(Struct): - id: int - usIn: int - usOut: int - usDiff: int - testnet: bool - jsonrpc: str = '2.0' - result: Optional[list[dict]] = None - error: Optional[dict] = None - -class JSONRPCChannel(Struct): - method: str - params: dict - jsonrpc: str = '2.0' - - -class KLinesResult(Struct): - close: list[float] - cost: list[float] - high: list[float] - low: list[float] - open: list[float] - status: str - ticks: list[int] - volume: list[float] - -class Trade(Struct): - trade_seq: int - trade_id: str - timestamp: int - tick_direction: int - price: float - mark_price: float - iv: float - instrument_name: str - index_price: float - direction: str - contracts: float - amount: float - combo_trade_id: Optional[int] = 0, - combo_id: Optional[str] = '', - block_trade_leg_count: Optional[int] = 0, - block_trade_id: Optional[str] = '', - -class LastTradesResult(Struct): - trades: list[Trade] - has_more: bool - - # convert datetime obj timestamp to unixtime in milliseconds def deribit_timestamp(when): return int((when.timestamp() * 1000) + (when.microsecond / 1000)) diff --git a/piker/brokers/deribit/venues.py b/piker/brokers/deribit/venues.py index ab85caf9..91a1583f 100644 --- a/piker/brokers/deribit/venues.py +++ b/piker/brokers/deribit/venues.py @@ -19,6 +19,7 @@ Per market data-type definitions and schemas types. """ from __future__ import annotations +import pendulum from typing import ( Literal, ) @@ -66,29 +67,27 @@ class Pair(Struct, frozen=True, kw_only=True): # dst base_currency: str # "BTC", - tick_size: float # 0.0001 - tick_size_steps: list[dict[str, str | int | float]] # [{'above_price': 0.005, 'tick_size': 0.0005}] + tick_size: float # 0.0001 # [{'above_price': 0.005, 'tick_size': 0.0005}] + tick_size_steps: list[dict[str, float]] @property def price_tick(self) -> Decimal: - step_size: float = self.tick_size_steps[0].get('above_price') - return Decimal(step_size) + return Decimal(str(self.tick_size_steps[0]['above_price'])) @property def size_tick(self) -> Decimal: - step_size: float = self.tick_size_steps[0].get('tick_size') - return Decimal(step_size) + return Decimal(str(self.tick_size)) @property def bs_fqme(self) -> str: - return self.symbol + return f'{self.symbol}' @property def bs_mktid(self) -> str: - return f'{self.symbol}' + return f'{self.symbol}.{self.venue}' -class OptionPair(Pair, frozen=True, kw_only=True): +class OptionPair(Pair, frozen=True): taker_commission: float # 0.0003 strike: float # 5000.0 @@ -118,17 +117,16 @@ class OptionPair(Pair, frozen=True, kw_only=True): @property def expiry(self) -> str: - symbol: str = self.instrument_name.lower() - pair, expiry, strike_price, otype = symbol.split('-') - return f'{expiry}' + iso_date = pendulum.from_timestamp(self.expiration_timestamp / 1000).isoformat() + return iso_date @property def venue(self) -> str: - return 'OPTION' + return 'option' @property def bs_fqme(self) -> str: - return f'{self.symbol}.{self.venue}.{self.expiry}' + return f'{self.symbol}' @property def bs_src_asset(self) -> str: @@ -136,13 +134,58 @@ class OptionPair(Pair, frozen=True, kw_only=True): @property def bs_dst_asset(self) -> str: - return f'{self.base_currency}' - - @property - def bs_mktid(self) -> str: return f'{self.symbol}' PAIRTYPES: dict[MarketType, Pair] = { 'option': OptionPair, } + + +class JSONRPCResult(Struct): + id: int + usIn: int + usOut: int + usDiff: int + testnet: bool + jsonrpc: str = '2.0' + error: Optional[dict] = None + result: Optional[list[dict]] = None + +class JSONRPCChannel(Struct): + method: str + params: dict + jsonrpc: str = '2.0' + + +class KLinesResult(Struct): + low: list[float] + cost: list[float] + high: list[float] + open: list[float] + close: list[float] + ticks: list[int] + status: str + volume: list[float] + +class Trade(Struct): + iv: float + price: float + amount: float + trade_id: str + contracts: float + direction: str + trade_seq: int + timestamp: int + mark_price: float + index_price: float + tick_direction: int + instrument_name: str + combo_id: Optional[str] = '', + combo_trade_id: Optional[int] = 0, + block_trade_id: Optional[str] = '', + block_trade_leg_count: Optional[int] = 0, + +class LastTradesResult(Struct): + trades: list[Trade] + has_more: bool