forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf7a83f
commit 00c8722
Showing
12 changed files
with
225 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
from .balance import Balance | ||
from .depth import Depth | ||
from .depth import PriceVolume | ||
from .error import ErrorMessage | ||
from .event import Event | ||
from .kline import KLine | ||
from .order import Order | ||
from .subscription import Subscription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
import bbgo_pb2 | ||
|
||
from typing import List | ||
|
||
|
||
# message Depth { | ||
# string exchange = 1; | ||
# string symbol = 2; | ||
# repeated PriceVolume asks = 3; | ||
# repeated PriceVolume bids = 4; | ||
# } | ||
@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: | ||
price: float | ||
volume: float | ||
|
||
@classmethod | ||
def from_pb(cls, obj: bbgo_pb2.PriceVolume): | ||
return cls( | ||
price=float(obj.price), | ||
volume=float(obj.volume), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
import bbgo_pb2 | ||
|
||
from ..enums import EventType, ChannelType | ||
from . import Depth | ||
|
||
|
||
@dataclass | ||
class Event: | ||
exchange: str | ||
symbol: str | ||
channel_type: ChannelType | ||
event_type: EventType | ||
depth: Depth = None | ||
|
||
@classmethod | ||
def from_pb(cls, obj: bbgo_pb2.SubscribeResponse) -> Event: | ||
channel_type = ChannelType.from_pb(obj.channel) | ||
|
||
event = cls( | ||
exchange=obj.exchange, | ||
symbol=obj.symbol, | ||
channel_type=channel_type, | ||
event_type=EventType.from_pb(obj.event), | ||
) | ||
|
||
if channel_type == ChannelType.BOOK: | ||
event.depth = Depth.from_pb(obj.depth) | ||
|
||
return event |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
import bbgo_pb2 | ||
|
||
from ..enums import ChannelType | ||
from ..enums import DepthType | ||
|
||
|
||
@dataclass | ||
class Subscription: | ||
exchange: str | ||
channel: ChannelType | ||
symbol: str | ||
depth: DepthType = None | ||
interval: str = None | ||
|
||
def to_pb(self) -> bbgo_pb2.Subscription: | ||
subscription_pb = bbgo_pb2.Subscription( | ||
exchange=self.exchange, | ||
channel=self.channel.to_pb(), | ||
symbol=self.symbol, | ||
) | ||
|
||
if self.depth is not None: | ||
subscription_pb.depth = self.depth.value | ||
|
||
if self.interval is not None: | ||
subscription_pb.interval = self.interval | ||
|
||
return subscription_pb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from .channel_type import ChannelType | ||
from .depth_type import DepthType | ||
from .event_type import EventType | ||
from .order_type import OrderType | ||
from .side_type import SideType |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
|
||
import bbgo_pb2 | ||
|
||
|
||
class ChannelType(Enum): | ||
BOOK = 'book' | ||
TRADE = 'trade' | ||
TICKER = 'ticker' | ||
USER = 'user' | ||
KLINE = 'kline' | ||
|
||
@classmethod | ||
def from_pb(cls, obj) -> ChannelType: | ||
return { | ||
bbgo_pb2.Channel.BOOK: cls.BOOK, | ||
bbgo_pb2.Channel.TRADE: cls.TRADE, | ||
bbgo_pb2.Channel.TICKER: cls.TICKER, | ||
bbgo_pb2.Channel.USER: cls.USER, | ||
bbgo_pb2.Channel.KLINE: cls.KLINE, | ||
}[obj] | ||
|
||
def to_pb(self) -> bbgo_pb2.Channel: | ||
return { | ||
'book': bbgo_pb2.Channel.BOOK, | ||
'trade': bbgo_pb2.Channel.TRADE, | ||
'ticker': bbgo_pb2.Channel.TICKER, | ||
'user': bbgo_pb2.Channel.USER, | ||
'kline': bbgo_pb2.Channel.KLINE, | ||
}[self.value] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from __future__ import annotations | ||
|
||
from enum import Enum | ||
|
||
|
||
# string depth = 4; // depth is for book, valid values are full, medium, 1, 5 and 20 | ||
class DepthType(Enum): | ||
FULL = 'full' | ||
MEDIUM = 'medium' | ||
DEPTH_1 = '1' | ||
DEPTH_5 = '5' | ||
DEPTH_20 = '20' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import click | ||
import grpc | ||
from loguru import logger | ||
|
||
import bbgo_pb2_grpc | ||
from bbgo import MarketService | ||
from bbgo.data import Subscription | ||
from bbgo.enums import ChannelType | ||
from bbgo.enums import DepthType | ||
|
||
|
||
@click.command() | ||
@click.option('--host', default='127.0.0.1') | ||
@click.option('--port', default=50051) | ||
def main(host, port): | ||
subscriptions = [ | ||
Subscription('binance', ChannelType.BOOK, symbol='BTCUSDT', depth=DepthType.FULL), | ||
] | ||
address = f'{host}:{port}' | ||
channel = grpc.insecure_channel(address) | ||
stub = bbgo_pb2_grpc.MarketDataServiceStub(channel) | ||
|
||
service = MarketService(stub) | ||
response_iter = service.subscribe(subscriptions) | ||
for response in response_iter: | ||
logger.info(response) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |