mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
use decimal to parse number
This commit is contained in:
parent
5ce47057bb
commit
33f662b9ef
|
@ -1,16 +1,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from ..utils import parse_number
|
||||
|
||||
|
||||
@dataclass
|
||||
class Balance:
|
||||
exchange: str
|
||||
currency: str
|
||||
available: float
|
||||
locked: float
|
||||
available: Decimal
|
||||
locked: Decimal
|
||||
borrowed: str
|
||||
|
||||
@classmethod
|
||||
|
@ -18,10 +21,10 @@ class Balance:
|
|||
return cls(
|
||||
exchange=obj.exchange,
|
||||
currency=obj.currency,
|
||||
available=float(obj.available),
|
||||
locked=float(obj.locked),
|
||||
available=parse_number(obj.available),
|
||||
locked=parse_number(obj.locked),
|
||||
borrowed=obj.borrowed,
|
||||
)
|
||||
|
||||
def total(self) -> float:
|
||||
def total(self) -> Decimal:
|
||||
return self.available + self.locked
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
from typing import List
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from typing import List
|
||||
from ..utils import parse_number
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -25,12 +28,12 @@ class Depth:
|
|||
|
||||
@dataclass
|
||||
class PriceVolume:
|
||||
price: float
|
||||
volume: float
|
||||
price: Decimal
|
||||
volume: Decimal
|
||||
|
||||
@classmethod
|
||||
def from_pb(cls, obj: bbgo_pb2.PriceVolume):
|
||||
return cls(
|
||||
price=float(obj.price),
|
||||
volume=float(obj.volume),
|
||||
price=parse_number(obj.price),
|
||||
volume=parse_number(obj.volume),
|
||||
)
|
||||
|
|
|
@ -2,10 +2,11 @@ from __future__ import annotations
|
|||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from ..utils import parse_float
|
||||
from ..utils import parse_number
|
||||
from ..utils import parse_time
|
||||
|
||||
|
||||
|
@ -13,15 +14,15 @@ from ..utils import parse_time
|
|||
class KLine:
|
||||
exchange: str
|
||||
symbol: str
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
close: float
|
||||
volume: float
|
||||
open: Decimal
|
||||
high: Decimal
|
||||
low: Decimal
|
||||
close: Decimal
|
||||
volume: Decimal
|
||||
session: str = None
|
||||
start_time: datetime = None
|
||||
end_time: datetime = None
|
||||
quote_volume: float = None
|
||||
quote_volume: Decimal = None
|
||||
closed: bool = None
|
||||
|
||||
@classmethod
|
||||
|
@ -29,12 +30,12 @@ class KLine:
|
|||
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),
|
||||
quote_volume=parse_float(obj.quote_volume),
|
||||
open=parse_number(obj.open),
|
||||
high=parse_number(obj.high),
|
||||
low=parse_number(obj.low),
|
||||
close=parse_number(obj.close),
|
||||
volume=parse_number(obj.volume),
|
||||
quote_volume=parse_number(obj.quote_volume),
|
||||
start_time=parse_time(obj.start_time),
|
||||
end_time=parse_time(obj.end_time),
|
||||
closed=obj.closed,
|
||||
|
|
|
@ -2,12 +2,13 @@ from __future__ import annotations
|
|||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from ..enums import OrderType
|
||||
from ..enums import SideType
|
||||
from ..utils import parse_float
|
||||
from ..utils import parse_number
|
||||
from ..utils import parse_time
|
||||
|
||||
|
||||
|
@ -18,11 +19,11 @@ class Order:
|
|||
order_id: str
|
||||
side: SideType
|
||||
order_type: OrderType
|
||||
price: float
|
||||
stop_price: float
|
||||
price: Decimal
|
||||
stop_price: Decimal
|
||||
status: str
|
||||
quantity: float
|
||||
executed_quantity: float
|
||||
quantity: Decimal
|
||||
executed_quantity: Decimal
|
||||
client_order_id: str
|
||||
group_id: int
|
||||
created_at: datetime
|
||||
|
@ -35,11 +36,11 @@ class Order:
|
|||
order_id=obj.id,
|
||||
side=SideType(obj.side),
|
||||
order_type=OrderType(obj.order_type),
|
||||
price=parse_float(obj.price),
|
||||
stop_price=parse_float(obj.stop_price),
|
||||
price=parse_number(obj.price),
|
||||
stop_price=parse_number(obj.stop_price),
|
||||
status=obj.status,
|
||||
quantity=parse_float(obj.quantity),
|
||||
executed_quantity=parse_float(obj.executed_quantity),
|
||||
quantity=parse_number(obj.quantity),
|
||||
executed_quantity=parse_number(obj.executed_quantity),
|
||||
client_order_id=obj.client_order_id,
|
||||
group_id=obj.group_id,
|
||||
created_at=parse_time(obj.created_at),
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
|
@ -14,10 +15,10 @@ class SubmitOrder:
|
|||
exchange: str
|
||||
symbol: str
|
||||
side: SideType
|
||||
quantity: float
|
||||
quantity: Decimal
|
||||
order_type: OrderType
|
||||
price: float = None
|
||||
stop_price: float = None
|
||||
price: Decimal = None
|
||||
stop_price: Decimal = None
|
||||
client_order_id: str = None
|
||||
group_id: int = None
|
||||
|
||||
|
|
|
@ -1,30 +1,31 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from ..utils import parse_float
|
||||
from ..utils import parse_number
|
||||
|
||||
|
||||
@dataclass
|
||||
class Ticker:
|
||||
exchange: str
|
||||
symbol: str
|
||||
open: float
|
||||
high: float
|
||||
low: float
|
||||
close: float
|
||||
volume: float
|
||||
open: Decimal
|
||||
high: Decimal
|
||||
low: Decimal
|
||||
close: Decimal
|
||||
volume: Decimal
|
||||
|
||||
@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),
|
||||
open=parse_number(obj.open),
|
||||
high=parse_number(obj.high),
|
||||
low=parse_number(obj.low),
|
||||
close=parse_number(obj.close),
|
||||
volume=parse_number(obj.volume),
|
||||
)
|
||||
|
|
|
@ -2,11 +2,12 @@ from __future__ import annotations
|
|||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
|
||||
from ..enums import SideType
|
||||
from ..utils import parse_float
|
||||
from ..utils import parse_number
|
||||
from ..utils import parse_time
|
||||
|
||||
|
||||
|
@ -16,12 +17,12 @@ class Trade:
|
|||
exchange: str
|
||||
symbol: str
|
||||
trade_id: str
|
||||
price: float
|
||||
quantity: float
|
||||
price: Decimal
|
||||
quantity: Decimal
|
||||
created_at: datetime
|
||||
side: SideType
|
||||
fee_currency: str
|
||||
fee: float
|
||||
fee: Decimal
|
||||
maker: bool
|
||||
|
||||
@classmethod
|
||||
|
@ -31,11 +32,11 @@ class Trade:
|
|||
exchange=obj.exchange,
|
||||
symbol=obj.symbol,
|
||||
trade_id=obj.id,
|
||||
price=parse_float(obj.price),
|
||||
quantity=parse_float(obj.quantity),
|
||||
price=parse_number(obj.price),
|
||||
quantity=parse_number(obj.quantity),
|
||||
created_at=parse_time(obj.created_at),
|
||||
side=SideType(obj.side),
|
||||
fee_currency=obj.fee_currency,
|
||||
fee=parse_float(obj.fee),
|
||||
fee=parse_number(obj.fee),
|
||||
maker=obj.maker,
|
||||
)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from .convert import parse_float
|
||||
from .convert import parse_number
|
||||
from .convert import parse_time
|
||||
from .grpc_utils import get_credentials_from_env
|
||||
from .grpc_utils import get_grpc_cert_file_from_env
|
||||
|
|
|
@ -1,15 +1,16 @@
|
|||
from datetime import datetime
|
||||
from decimal import Decimal
|
||||
from typing import Union
|
||||
|
||||
|
||||
def parse_float(s: Union[str, float]) -> float:
|
||||
def parse_number(s: Union[str, float]) -> Decimal:
|
||||
if s is None:
|
||||
return 0
|
||||
|
||||
if s == "":
|
||||
return 0
|
||||
|
||||
return float(s)
|
||||
return Decimal(s)
|
||||
|
||||
|
||||
def parse_time(t: Union[str, int]) -> datetime:
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
from decimal import Decimal
|
||||
|
||||
import bbgo_pb2
|
||||
from bbgo.data import Balance
|
||||
from bbgo.data import ErrorMessage
|
||||
|
@ -24,8 +26,8 @@ def test_balance_from_pb():
|
|||
|
||||
assert balance.exchange == exchange
|
||||
assert balance.currency == currency
|
||||
assert balance.available == float(available)
|
||||
assert balance.locked == float(locked)
|
||||
assert balance.available == Decimal(available)
|
||||
assert balance.locked == Decimal(locked)
|
||||
assert balance.borrowed == borrowed
|
||||
|
||||
|
||||
|
@ -58,12 +60,12 @@ def test_kline_from_pb():
|
|||
|
||||
assert kline.exchange == exchange
|
||||
assert kline.symbol == symbol
|
||||
assert kline.open == float(open)
|
||||
assert kline.high == float(high)
|
||||
assert kline.low == float(low)
|
||||
assert kline.close == float(close)
|
||||
assert kline.volume == float(volume)
|
||||
assert kline.quote_volume == float(quote_volume)
|
||||
assert kline.open == Decimal(open)
|
||||
assert kline.high == Decimal(high)
|
||||
assert kline.low == Decimal(low)
|
||||
assert kline.close == Decimal(close)
|
||||
assert kline.volume == Decimal(volume)
|
||||
assert kline.quote_volume == Decimal(quote_volume)
|
||||
assert kline.start_time == parse_time(start_time)
|
||||
assert kline.end_time == parse_time(end_time)
|
||||
assert closed == closed
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
@ -10,9 +12,8 @@ def test_parse_time():
|
|||
|
||||
|
||||
def test_parse_float():
|
||||
assert parse_float(None) == 0
|
||||
assert parse_float("") == 0
|
||||
assert parse_number(None) == 0
|
||||
assert parse_number("") == 0
|
||||
|
||||
s = "3.14159265358979"
|
||||
f = 3.14159265358979
|
||||
assert parse_float(s) == f
|
||||
assert parse_number(s) == Decimal(s)
|
||||
|
|
Loading…
Reference in New Issue
Block a user