Merge pull request #612 from narumiruna/narumi/decimal

This commit is contained in:
なるみ 2022-05-13 18:45:32 +08:00 committed by GitHub
commit adab6ea434
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 84 additions and 69 deletions

View File

@ -1,16 +1,19 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
from ..utils import parse_number
@dataclass @dataclass
class Balance: class Balance:
exchange: str exchange: str
currency: str currency: str
available: float available: Decimal
locked: float locked: Decimal
borrowed: str borrowed: str
@classmethod @classmethod
@ -18,10 +21,10 @@ class Balance:
return cls( return cls(
exchange=obj.exchange, exchange=obj.exchange,
currency=obj.currency, currency=obj.currency,
available=float(obj.available), available=parse_number(obj.available),
locked=float(obj.locked), locked=parse_number(obj.locked),
borrowed=obj.borrowed, borrowed=obj.borrowed,
) )
def total(self) -> float: def total(self) -> Decimal:
return self.available + self.locked return self.available + self.locked

View File

@ -1,9 +1,12 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from decimal import Decimal
from typing import List
import bbgo_pb2 import bbgo_pb2
from typing import List from ..utils import parse_number
@dataclass @dataclass
@ -25,12 +28,12 @@ class Depth:
@dataclass @dataclass
class PriceVolume: class PriceVolume:
price: float price: Decimal
volume: float volume: Decimal
@classmethod @classmethod
def from_pb(cls, obj: bbgo_pb2.PriceVolume): def from_pb(cls, obj: bbgo_pb2.PriceVolume):
return cls( return cls(
price=float(obj.price), price=parse_number(obj.price),
volume=float(obj.volume), volume=parse_number(obj.volume),
) )

View File

@ -2,10 +2,11 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
from ..utils import parse_float from ..utils import parse_number
from ..utils import parse_time from ..utils import parse_time
@ -13,15 +14,15 @@ from ..utils import parse_time
class KLine: class KLine:
exchange: str exchange: str
symbol: str symbol: str
open: float open: Decimal
high: float high: Decimal
low: float low: Decimal
close: float close: Decimal
volume: float volume: Decimal
session: str = None session: str = None
start_time: datetime = None start_time: datetime = None
end_time: datetime = None end_time: datetime = None
quote_volume: float = None quote_volume: Decimal = None
closed: bool = None closed: bool = None
@classmethod @classmethod
@ -29,12 +30,12 @@ class KLine:
return cls( return cls(
exchange=obj.exchange, exchange=obj.exchange,
symbol=obj.symbol, symbol=obj.symbol,
open=parse_float(obj.open), open=parse_number(obj.open),
high=parse_float(obj.high), high=parse_number(obj.high),
low=parse_float(obj.low), low=parse_number(obj.low),
close=parse_float(obj.close), close=parse_number(obj.close),
volume=parse_float(obj.volume), volume=parse_number(obj.volume),
quote_volume=parse_float(obj.quote_volume), quote_volume=parse_number(obj.quote_volume),
start_time=parse_time(obj.start_time), start_time=parse_time(obj.start_time),
end_time=parse_time(obj.end_time), end_time=parse_time(obj.end_time),
closed=obj.closed, closed=obj.closed,

View File

@ -2,12 +2,13 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
from ..enums import OrderType from ..enums import OrderType
from ..enums import SideType from ..enums import SideType
from ..utils import parse_float from ..utils import parse_number
from ..utils import parse_time from ..utils import parse_time
@ -18,11 +19,11 @@ class Order:
order_id: str order_id: str
side: SideType side: SideType
order_type: OrderType order_type: OrderType
price: float price: Decimal
stop_price: float stop_price: Decimal
status: str status: str
quantity: float quantity: Decimal
executed_quantity: float executed_quantity: Decimal
client_order_id: str client_order_id: str
group_id: int group_id: int
created_at: datetime created_at: datetime
@ -35,11 +36,11 @@ class Order:
order_id=obj.id, order_id=obj.id,
side=SideType(obj.side), side=SideType(obj.side),
order_type=OrderType(obj.order_type), order_type=OrderType(obj.order_type),
price=parse_float(obj.price), price=parse_number(obj.price),
stop_price=parse_float(obj.stop_price), stop_price=parse_number(obj.stop_price),
status=obj.status, status=obj.status,
quantity=parse_float(obj.quantity), quantity=parse_number(obj.quantity),
executed_quantity=parse_float(obj.executed_quantity), executed_quantity=parse_number(obj.executed_quantity),
client_order_id=obj.client_order_id, client_order_id=obj.client_order_id,
group_id=obj.group_id, group_id=obj.group_id,
created_at=parse_time(obj.created_at), created_at=parse_time(obj.created_at),

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
@ -14,10 +15,10 @@ class SubmitOrder:
exchange: str exchange: str
symbol: str symbol: str
side: SideType side: SideType
quantity: float quantity: Decimal
order_type: OrderType order_type: OrderType
price: float = None price: Decimal = None
stop_price: float = None stop_price: Decimal = None
client_order_id: str = None client_order_id: str = None
group_id: int = None group_id: int = None

View File

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

View File

@ -2,11 +2,12 @@ from __future__ import annotations
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime from datetime import datetime
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
from ..enums import SideType from ..enums import SideType
from ..utils import parse_float from ..utils import parse_number
from ..utils import parse_time from ..utils import parse_time
@ -16,12 +17,12 @@ class Trade:
exchange: str exchange: str
symbol: str symbol: str
trade_id: str trade_id: str
price: float price: Decimal
quantity: float quantity: Decimal
created_at: datetime created_at: datetime
side: SideType side: SideType
fee_currency: str fee_currency: str
fee: float fee: Decimal
maker: bool maker: bool
@classmethod @classmethod
@ -31,11 +32,11 @@ class Trade:
exchange=obj.exchange, exchange=obj.exchange,
symbol=obj.symbol, symbol=obj.symbol,
trade_id=obj.id, trade_id=obj.id,
price=parse_float(obj.price), price=parse_number(obj.price),
quantity=parse_float(obj.quantity), quantity=parse_number(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=parse_float(obj.fee), fee=parse_number(obj.fee),
maker=obj.maker, maker=obj.maker,
) )

View File

@ -1,4 +1,4 @@
from .convert import parse_float from .convert import parse_number
from .convert import parse_time from .convert import parse_time
from .grpc_utils import get_credentials_from_env from .grpc_utils import get_credentials_from_env
from .grpc_utils import get_grpc_cert_file_from_env from .grpc_utils import get_grpc_cert_file_from_env

View File

@ -1,15 +1,16 @@
from datetime import datetime from datetime import datetime
from decimal import Decimal
from typing import Union from typing import Union
def parse_float(s: Union[str, float]) -> float: def parse_number(s: Union[str, float]) -> Decimal:
if s is None: if s is None:
return 0 return 0
if s == "": if s == "":
return 0 return 0
return float(s) return Decimal(s)
def parse_time(t: Union[str, int]) -> datetime: def parse_time(t: Union[str, int]) -> datetime:

View File

@ -1,3 +1,5 @@
from decimal import Decimal
import bbgo_pb2 import bbgo_pb2
from bbgo.data import Balance from bbgo.data import Balance
from bbgo.data import ErrorMessage from bbgo.data import ErrorMessage
@ -24,8 +26,8 @@ def test_balance_from_pb():
assert balance.exchange == exchange assert balance.exchange == exchange
assert balance.currency == currency assert balance.currency == currency
assert balance.available == float(available) assert balance.available == Decimal(available)
assert balance.locked == float(locked) assert balance.locked == Decimal(locked)
assert balance.borrowed == borrowed assert balance.borrowed == borrowed
@ -58,12 +60,12 @@ def test_kline_from_pb():
assert kline.exchange == exchange assert kline.exchange == exchange
assert kline.symbol == symbol assert kline.symbol == symbol
assert kline.open == float(open) assert kline.open == Decimal(open)
assert kline.high == float(high) assert kline.high == Decimal(high)
assert kline.low == float(low) assert kline.low == Decimal(low)
assert kline.close == float(close) assert kline.close == Decimal(close)
assert kline.volume == float(volume) assert kline.volume == Decimal(volume)
assert kline.quote_volume == float(quote_volume) assert kline.quote_volume == Decimal(quote_volume)
assert kline.start_time == parse_time(start_time) assert kline.start_time == parse_time(start_time)
assert kline.end_time == parse_time(end_time) assert kline.end_time == parse_time(end_time)
assert closed == closed assert closed == closed

View File

@ -1,4 +1,6 @@
from bbgo.utils import parse_float from decimal import Decimal
from bbgo.utils import parse_number
from bbgo.utils import parse_time from bbgo.utils import parse_time
@ -10,9 +12,8 @@ def test_parse_time():
def test_parse_float(): def test_parse_float():
assert parse_float(None) == 0 assert parse_number(None) == 0
assert parse_float("") == 0 assert parse_number("") == 0
s = "3.14159265358979" s = "3.14159265358979"
f = 3.14159265358979 assert parse_number(s) == Decimal(s)
assert parse_float(s) == f