Skip to content

Commit

Permalink
Merge pull request #233 from Davi0kProgramsThings/documentation/examples
Browse files Browse the repository at this point in the history
Merge branch `Davi0kProgramsThings:documentation/examples` into branch `bitfinexcom:master`.
  • Loading branch information
vigan-abd authored Dec 28, 2023
2 parents 4777730 + d37faf1 commit 59c3090
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
22 changes: 20 additions & 2 deletions examples/websocket/public/order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@

import zlib
from collections import OrderedDict
from decimal import Decimal
from math import floor, log10
from typing import Any, Dict, List, cast

from bfxapi import Client
from bfxapi.types import TradingPairBook
from bfxapi.websocket.subscriptions import Book


def _format_float(value: float) -> str:
"""
Format float numbers into a string compatible with the Bitfinex API.
"""

def _find_exp(number: float) -> int:
base10 = log10(abs(number))

return floor(base10)

if _find_exp(value) >= -6:
return format(Decimal(repr(value)), "f")

return str(value).replace("e-0", "e-")


class OrderBook:
def __init__(self, symbols: List[str]):
self.__order_book = {
Expand Down Expand Up @@ -58,7 +76,7 @@ def verify(self, symbol: str, checksum: int) -> bool:
values.extend([bid[0], bid[2]])
values.extend([ask[0], ask[2]])

local = ":".join(str(value) for value in values)
local = ":".join(_format_float(value) for value in values)

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

Expand Down Expand Up @@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
if not order_book.verify(symbol, value):
print(
"Mismatch between local and remote checksums: "
f"restarting book for symbol <{symbol}>..."
+ f"restarting book for symbol <{symbol}>..."
)

_subscription = cast(Dict[str, Any], subscription.copy())
Expand Down
22 changes: 20 additions & 2 deletions examples/websocket/public/raw_order_book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,31 @@

import zlib
from collections import OrderedDict
from decimal import Decimal
from math import floor, log10
from typing import Any, Dict, List, cast

from bfxapi import Client
from bfxapi.types import TradingPairRawBook
from bfxapi.websocket.subscriptions import Book


def _format_float(value: float) -> str:
"""
Format float numbers into a string compatible with the Bitfinex API.
"""

def _find_exp(number: float) -> int:
base10 = log10(abs(number))

return floor(base10)

if _find_exp(value) >= -6:
return format(Decimal(repr(value)), "f")

return str(value).replace("e-0", "e-")


class RawOrderBook:
def __init__(self, symbols: List[str]):
self.__raw_order_book = {
Expand Down Expand Up @@ -58,7 +76,7 @@ def verify(self, symbol: str, checksum: int) -> bool:
values.extend([bid[0], bid[2]])
values.extend([ask[0], ask[2]])

local = ":".join(str(value) for value in values)
local = ":".join(_format_float(value) for value in values)

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

Expand Down Expand Up @@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
if not raw_order_book.verify(symbol, value):
print(
"Mismatch between local and remote checksums: "
f"restarting book for symbol <{symbol}>..."
+ f"restarting book for symbol <{symbol}>..."
)

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

0 comments on commit 59c3090

Please sign in to comment.