bbgo_origin/python/bbgo/data/depth.py

40 lines
832 B
Python
Raw Normal View History

2022-04-13 09:40:21 +00:00
from __future__ import annotations
from dataclasses import dataclass
2022-05-13 08:30:19 +00:00
from decimal import Decimal
from typing import List
2022-04-13 09:40:21 +00:00
import bbgo_pb2
2022-05-13 08:30:19 +00:00
from ..utils import parse_number
2022-04-13 09:40:21 +00:00
@dataclass
class Depth:
exchange: str
symbol: str
asks: List[PriceVolume]
bids: List[PriceVolume]
@classmethod
def from_pb(cls, obj: bbgo_pb2.Depth):
return cls(
exchange=obj.exchange,
symbol=obj.symbol,
asks=[PriceVolume.from_pb(ask) for ask in obj.asks],
bids=[PriceVolume.from_pb(bid) for bid in obj.bids],
)
@dataclass
class PriceVolume:
2022-05-13 08:30:19 +00:00
price: Decimal
volume: Decimal
2022-04-13 09:40:21 +00:00
@classmethod
def from_pb(cls, obj: bbgo_pb2.PriceVolume):
return cls(
2022-05-13 08:30:19 +00:00
price=parse_number(obj.price),
volume=parse_number(obj.volume),
2022-04-13 10:13:54 +00:00
)