mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 23:04:21 +01:00
Use python3/decimal instead of float when parsing ws
This commit is contained in:
committed by
Jacob Plaster
parent
3c8c3fb71e
commit
8dc745f149
@@ -4,17 +4,6 @@ Module used to describe all of the different data types
|
|||||||
|
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
|
|
||||||
def prepare_price(price):
|
|
||||||
"""
|
|
||||||
Convert the price to an acceptable format
|
|
||||||
"""
|
|
||||||
# convert to 4 significant figures
|
|
||||||
prep_price = '{0:.4f}'.format(price)
|
|
||||||
# remove decimal place if zero float
|
|
||||||
return '{0:g}'.format(float(prep_price))
|
|
||||||
|
|
||||||
|
|
||||||
class OrderBook:
|
class OrderBook:
|
||||||
"""
|
"""
|
||||||
Object used to store the state of the orderbook. This can then be used
|
Object used to store the state of the orderbook. This can then be used
|
||||||
@@ -107,13 +96,13 @@ class OrderBook:
|
|||||||
bid = self.bids[index]
|
bid = self.bids[index]
|
||||||
price = bid[0]
|
price = bid[0]
|
||||||
amount = bid[3] if len(bid) == 4 else bid[2]
|
amount = bid[3] if len(bid) == 4 else bid[2]
|
||||||
data += [prepare_price(price)]
|
data += [str(price)]
|
||||||
data += [str(amount)]
|
data += [str(amount)]
|
||||||
if index < len(self.asks):
|
if index < len(self.asks):
|
||||||
ask = self.asks[index]
|
ask = self.asks[index]
|
||||||
price = ask[0]
|
price = ask[0]
|
||||||
amount = ask[3] if len(ask) == 4 else ask[2]
|
amount = ask[3] if len(ask) == 4 else ask[2]
|
||||||
data += [prepare_price(price)]
|
data += [str(price)]
|
||||||
data += [str(amount)]
|
data += [str(amount)]
|
||||||
checksum_str = ':'.join(data)
|
checksum_str = ':'.join(data)
|
||||||
# calculate checksum and force signed integer
|
# calculate checksum and force signed integer
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import json
|
|||||||
import time
|
import time
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
from decimal import *
|
||||||
from .GenericWebsocket import GenericWebsocket, AuthError
|
from .GenericWebsocket import GenericWebsocket, AuthError
|
||||||
from .SubscriptionManager import SubscriptionManager
|
from .SubscriptionManager import SubscriptionManager
|
||||||
from .WalletManager import WalletManager
|
from .WalletManager import WalletManager
|
||||||
@@ -354,7 +355,8 @@ class BfxWebsocket(GenericWebsocket):
|
|||||||
|
|
||||||
async def on_message(self, message):
|
async def on_message(self, message):
|
||||||
self.logger.debug(message)
|
self.logger.debug(message)
|
||||||
msg = json.loads(message)
|
# convert float values to decimal
|
||||||
|
msg = json.loads(message, parse_float=Decimal)
|
||||||
self._emit('all', msg)
|
self._emit('all', msg)
|
||||||
if type(msg) is dict:
|
if type(msg) is dict:
|
||||||
# System messages are received as json
|
# System messages are received as json
|
||||||
|
|||||||
Reference in New Issue
Block a user