Merge pull request #550 from narumiruna/python/market-data

python: add ticker, kline and trade to market data
This commit is contained in:
なるみ 2022-04-20 15:24:24 +08:00 committed by GitHub
commit 8a724aa619
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 61 additions and 6 deletions

View File

@ -9,4 +9,5 @@ from .kline import KLine
from .order import Order from .order import Order
from .submit_order import SubmitOrder from .submit_order import SubmitOrder
from .subscription import Subscription from .subscription import Subscription
from .ticker import Ticker
from .trade import Trade from .trade import Trade

View File

@ -1,15 +1,20 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime
from typing import List from typing import List
import bbgo_pb2 import bbgo_pb2
from ..enums import ChannelType from ..enums import ChannelType
from ..enums import EventType from ..enums import EventType
from ..utils import parse_time
from .balance import Balance from .balance import Balance
from .depth import Depth from .depth import Depth
from .error import ErrorMessage
from .kline import KLine
from .order import Order from .order import Order
from .ticker import Ticker
from .trade import Trade from .trade import Trade
@ -42,24 +47,42 @@ class UserDataEvent(Event):
@dataclass @dataclass
class MarketDataEvent(Event): class MarketDataEvent(Event):
session: str
exchange: str exchange: str
symbol: str symbol: str
channel_type: ChannelType channel_type: ChannelType
event_type: EventType event_type: EventType
subscribed_at: datetime
error: ErrorMessage
depth: Depth = None depth: Depth = None
kline: KLine = None
ticker: Ticker = None
trades: List[Trade] = None
@classmethod @classmethod
def from_pb(cls, obj: bbgo_pb2.MarketData) -> MarketDataEvent: def from_pb(cls, obj: bbgo_pb2.MarketData) -> MarketDataEvent:
channel_type = ChannelType(obj.channel) channel_type = ChannelType(obj.channel)
event = cls( event = cls(
session=obj.session,
exchange=obj.exchange, exchange=obj.exchange,
symbol=obj.symbol, symbol=obj.symbol,
channel_type=channel_type, channel_type=channel_type,
event_type=EventType(obj.event), event_type=EventType(obj.event),
subscribed_at=parse_time(obj.subscribed_at),
error=ErrorMessage.from_pb(obj.error),
) )
if channel_type == ChannelType.BOOK: if channel_type == ChannelType.BOOK:
event.depth = Depth.from_pb(obj.depth) event.depth = Depth.from_pb(obj.depth)
if channel_type == ChannelType.KLINE:
event.kline = KLine.from_pb(obj.kline)
if channel_type == ChannelType.TICKER:
event.ticker = Ticker.from_pb(obj.ticker)
if channel_type == ChannelType.TRADE:
event.trades = [Trade.from_pb(trade) for trade in obj.trades]
return event return event

View File

@ -0,0 +1,30 @@
from __future__ import annotations
from dataclasses import dataclass
import bbgo_pb2
from ..utils import parse_float
@dataclass
class Ticker:
exchange: str
symbol: str
open: float
high: float
low: float
close: float
volume: float
@classmethod
def from_pb(cls, obj: bbgo_pb2.KLine) -> Ticker:
return cls(
exchange=obj.exchange,
symbol=obj.symbol,
open=parse_float(obj.open),
high=parse_float(obj.high),
low=parse_float(obj.low),
close=parse_float(obj.close),
volume=parse_float(obj.volume),
)

View File

@ -6,6 +6,7 @@ from datetime import datetime
import bbgo_pb2 import bbgo_pb2
from ..enums import SideType from ..enums import SideType
from ..utils import parse_float
from ..utils import parse_time from ..utils import parse_time
@ -14,7 +15,7 @@ class Trade:
session: str session: str
exchange: str exchange: str
symbol: str symbol: str
id: str trade_id: str
price: float price: float
quantity: float quantity: float
created_at: datetime created_at: datetime
@ -30,11 +31,11 @@ class Trade:
exchange=obj.exchange, exchange=obj.exchange,
symbol=obj.symbol, symbol=obj.symbol,
id=obj.id, id=obj.id,
price=float(obj.price), price=parse_float(obj.price),
quantity=float(obj.quantity), quantity=parse_float(obj.quantity),
created_at=parse_time(obj.created_at), created_at=parse_time(obj.created_at),
side=SideType(obj.side), side=SideType(obj.side),
fee_currency=obj.fee_currency, fee_currency=obj.fee_currency,
fee=float(obj.fee), fee=parse_float(obj.fee),
maker=obj.maker, maker=obj.maker,
) )

View File

@ -2,7 +2,7 @@ from datetime import datetime
from typing import Union from typing import Union
def parse_float(s: str) -> float: def parse_float(s: Union[str, float]) -> float:
if s is None: if s is None:
return 0 return 0

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "bbgo" name = "bbgo"
version = "0.1.1" version = "0.1.3"
description = "" description = ""
authors = ["なるみ <weaper@gmail.com>"] authors = ["なるみ <weaper@gmail.com>"]
packages = [ packages = [