Skip to content

Commit 59c3090

Browse files
authored
Merge pull request #233 from Davi0kProgramsThings/documentation/examples
Merge branch `Davi0kProgramsThings:documentation/examples` into branch `bitfinexcom:master`.
2 parents 4777730 + d37faf1 commit 59c3090

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

examples/websocket/public/order_book.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22

33
import zlib
44
from collections import OrderedDict
5+
from decimal import Decimal
6+
from math import floor, log10
57
from typing import Any, Dict, List, cast
68

79
from bfxapi import Client
810
from bfxapi.types import TradingPairBook
911
from bfxapi.websocket.subscriptions import Book
1012

1113

14+
def _format_float(value: float) -> str:
15+
"""
16+
Format float numbers into a string compatible with the Bitfinex API.
17+
"""
18+
19+
def _find_exp(number: float) -> int:
20+
base10 = log10(abs(number))
21+
22+
return floor(base10)
23+
24+
if _find_exp(value) >= -6:
25+
return format(Decimal(repr(value)), "f")
26+
27+
return str(value).replace("e-0", "e-")
28+
29+
1230
class OrderBook:
1331
def __init__(self, symbols: List[str]):
1432
self.__order_book = {
@@ -58,7 +76,7 @@ def verify(self, symbol: str, checksum: int) -> bool:
5876
values.extend([bid[0], bid[2]])
5977
values.extend([ask[0], ask[2]])
6078

61-
local = ":".join(str(value) for value in values)
79+
local = ":".join(_format_float(value) for value in values)
6280

6381
crc32 = zlib.crc32(local.encode("UTF-8"))
6482

@@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
111129
if not order_book.verify(symbol, value):
112130
print(
113131
"Mismatch between local and remote checksums: "
114-
f"restarting book for symbol <{symbol}>..."
132+
+ f"restarting book for symbol <{symbol}>..."
115133
)
116134

117135
_subscription = cast(Dict[str, Any], subscription.copy())

examples/websocket/public/raw_order_book.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@
22

33
import zlib
44
from collections import OrderedDict
5+
from decimal import Decimal
6+
from math import floor, log10
57
from typing import Any, Dict, List, cast
68

79
from bfxapi import Client
810
from bfxapi.types import TradingPairRawBook
911
from bfxapi.websocket.subscriptions import Book
1012

1113

14+
def _format_float(value: float) -> str:
15+
"""
16+
Format float numbers into a string compatible with the Bitfinex API.
17+
"""
18+
19+
def _find_exp(number: float) -> int:
20+
base10 = log10(abs(number))
21+
22+
return floor(base10)
23+
24+
if _find_exp(value) >= -6:
25+
return format(Decimal(repr(value)), "f")
26+
27+
return str(value).replace("e-0", "e-")
28+
29+
1230
class RawOrderBook:
1331
def __init__(self, symbols: List[str]):
1432
self.__raw_order_book = {
@@ -58,7 +76,7 @@ def verify(self, symbol: str, checksum: int) -> bool:
5876
values.extend([bid[0], bid[2]])
5977
values.extend([ask[0], ask[2]])
6078

61-
local = ":".join(str(value) for value in values)
79+
local = ":".join(_format_float(value) for value in values)
6280

6381
crc32 = zlib.crc32(local.encode("UTF-8"))
6482

@@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
111129
if not raw_order_book.verify(symbol, value):
112130
print(
113131
"Mismatch between local and remote checksums: "
114-
f"restarting book for symbol <{symbol}>..."
132+
+ f"restarting book for symbol <{symbol}>..."
115133
)
116134

117135
_subscription = cast(Dict[str, Any], subscription.copy())

0 commit comments

Comments
 (0)