mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
Merge pull request #550 from narumiruna/python/market-data
python: add ticker, kline and trade to market data
This commit is contained in:
commit
8a724aa619
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
30
python/bbgo/data/ticker.py
Normal file
30
python/bbgo/data/ticker.py
Normal 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),
|
||||
)
|
|
@ -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,
|
||||
)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "bbgo"
|
||||
version = "0.1.1"
|
||||
version = "0.1.3"
|
||||
description = ""
|
||||
authors = ["なるみ <weaper@gmail.com>"]
|
||||
packages = [
|
||||
|
|
Loading…
Reference in New Issue
Block a user