Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge branch Davi0kProgramsThings:documentation/examples into branch bitfinexcom:master. #233

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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