mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-18 22:34:21 +01:00
Merge pull request #233 from Davi0kProgramsThings/documentation/examples
Merge branch `Davi0kProgramsThings:documentation/examples` into branch `bitfinexcom:master`.
This commit is contained in:
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import zlib
|
import zlib
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from decimal import Decimal
|
||||||
|
from math import floor, log10
|
||||||
from typing import Any, Dict, List, cast
|
from typing import Any, Dict, List, cast
|
||||||
|
|
||||||
from bfxapi import Client
|
from bfxapi import Client
|
||||||
@@ -9,6 +11,22 @@ from bfxapi.types import TradingPairBook
|
|||||||
from bfxapi.websocket.subscriptions import Book
|
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:
|
class OrderBook:
|
||||||
def __init__(self, symbols: List[str]):
|
def __init__(self, symbols: List[str]):
|
||||||
self.__order_book = {
|
self.__order_book = {
|
||||||
@@ -58,7 +76,7 @@ class OrderBook:
|
|||||||
values.extend([bid[0], bid[2]])
|
values.extend([bid[0], bid[2]])
|
||||||
values.extend([ask[0], ask[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"))
|
crc32 = zlib.crc32(local.encode("UTF-8"))
|
||||||
|
|
||||||
@@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
|
|||||||
if not order_book.verify(symbol, value):
|
if not order_book.verify(symbol, value):
|
||||||
print(
|
print(
|
||||||
"Mismatch between local and remote checksums: "
|
"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())
|
_subscription = cast(Dict[str, Any], subscription.copy())
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
import zlib
|
import zlib
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from decimal import Decimal
|
||||||
|
from math import floor, log10
|
||||||
from typing import Any, Dict, List, cast
|
from typing import Any, Dict, List, cast
|
||||||
|
|
||||||
from bfxapi import Client
|
from bfxapi import Client
|
||||||
@@ -9,6 +11,22 @@ from bfxapi.types import TradingPairRawBook
|
|||||||
from bfxapi.websocket.subscriptions import Book
|
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:
|
class RawOrderBook:
|
||||||
def __init__(self, symbols: List[str]):
|
def __init__(self, symbols: List[str]):
|
||||||
self.__raw_order_book = {
|
self.__raw_order_book = {
|
||||||
@@ -58,7 +76,7 @@ class RawOrderBook:
|
|||||||
values.extend([bid[0], bid[2]])
|
values.extend([bid[0], bid[2]])
|
||||||
values.extend([ask[0], ask[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"))
|
crc32 = zlib.crc32(local.encode("UTF-8"))
|
||||||
|
|
||||||
@@ -111,7 +129,7 @@ async def on_checksum(subscription: Book, value: int):
|
|||||||
if not raw_order_book.verify(symbol, value):
|
if not raw_order_book.verify(symbol, value):
|
||||||
print(
|
print(
|
||||||
"Mismatch between local and remote checksums: "
|
"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())
|
_subscription = cast(Dict[str, Any], subscription.copy())
|
||||||
|
|||||||
Reference in New Issue
Block a user