From ead3f79f6c2da4871da1abb029e9ebe2bbf6cd59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Wed, 20 Apr 2022 15:00:44 +0800 Subject: [PATCH 1/2] python: add ticker, kline and trade to market data --- python/bbgo/data/__init__.py | 1 + python/bbgo/data/event.py | 23 +++++++++++++++++++++++ python/bbgo/data/ticker.py | 30 ++++++++++++++++++++++++++++++ python/bbgo/data/trade.py | 9 +++++---- python/bbgo/utils/convert.py | 2 +- 5 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 python/bbgo/data/ticker.py diff --git a/python/bbgo/data/__init__.py b/python/bbgo/data/__init__.py index 5f7c033e7..e579559d8 100644 --- a/python/bbgo/data/__init__.py +++ b/python/bbgo/data/__init__.py @@ -9,4 +9,5 @@ from .kline import KLine from .order import Order from .submit_order import SubmitOrder from .subscription import Subscription +from .ticker import Ticker from .trade import Trade diff --git a/python/bbgo/data/event.py b/python/bbgo/data/event.py index d8ebad373..8e96c818d 100644 --- a/python/bbgo/data/event.py +++ b/python/bbgo/data/event.py @@ -1,15 +1,20 @@ from __future__ import annotations from dataclasses import dataclass +from datetime import datetime from typing import List import bbgo_pb2 from ..enums import ChannelType from ..enums import EventType +from ..utils import parse_time from .balance import Balance from .depth import Depth +from .error import ErrorMessage +from .kline import KLine from .order import Order +from .ticker import Ticker from .trade import Trade @@ -42,24 +47,42 @@ class UserDataEvent(Event): @dataclass class MarketDataEvent(Event): + session: str exchange: str symbol: str channel_type: ChannelType event_type: EventType + subscribed_at: datetime + error: ErrorMessage depth: Depth = None + kline: KLine = None + ticker: Ticker = None + trades: List[Trade] = None @classmethod def from_pb(cls, obj: bbgo_pb2.MarketData) -> MarketDataEvent: channel_type = ChannelType(obj.channel) event = cls( + session=obj.session, exchange=obj.exchange, symbol=obj.symbol, channel_type=channel_type, event_type=EventType(obj.event), + subscribed_at=parse_time(obj.subscribed_at), + error=ErrorMessage.from_pb(obj.error), ) if channel_type == ChannelType.BOOK: 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 diff --git a/python/bbgo/data/ticker.py b/python/bbgo/data/ticker.py new file mode 100644 index 000000000..c43635b8b --- /dev/null +++ b/python/bbgo/data/ticker.py @@ -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), + ) diff --git a/python/bbgo/data/trade.py b/python/bbgo/data/trade.py index a29139a83..63fd01661 100644 --- a/python/bbgo/data/trade.py +++ b/python/bbgo/data/trade.py @@ -6,6 +6,7 @@ from datetime import datetime import bbgo_pb2 from ..enums import SideType +from ..utils import parse_float from ..utils import parse_time @@ -14,7 +15,7 @@ class Trade: session: str exchange: str symbol: str - id: str + trade_id: str price: float quantity: float created_at: datetime @@ -30,11 +31,11 @@ class Trade: exchange=obj.exchange, symbol=obj.symbol, id=obj.id, - price=float(obj.price), - quantity=float(obj.quantity), + price=parse_float(obj.price), + quantity=parse_float(obj.quantity), created_at=parse_time(obj.created_at), side=SideType(obj.side), fee_currency=obj.fee_currency, - fee=float(obj.fee), + fee=parse_float(obj.fee), maker=obj.maker, ) diff --git a/python/bbgo/utils/convert.py b/python/bbgo/utils/convert.py index 3f85ad49c..a3bb98bc8 100644 --- a/python/bbgo/utils/convert.py +++ b/python/bbgo/utils/convert.py @@ -2,7 +2,7 @@ from datetime import datetime from typing import Union -def parse_float(s: str) -> float: +def parse_float(s: Union[str, float]) -> float: if s is None: return 0 From 7af11d6074d89a4505bd7debe3ee15fb3417d63d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Wed, 20 Apr 2022 15:17:18 +0800 Subject: [PATCH 2/2] python: bump version to v0.1.3 --- python/pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/pyproject.toml b/python/pyproject.toml index 32fdb8fa0..2071e0f2e 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "bbgo" -version = "0.1.1" +version = "0.1.3" description = "" authors = ["なるみ "] packages = [