def test_ftx_checksum(): BID = 'bid' ASK = 'ask' r = requests.get( "https://ftx.com/api/markets/BTC-PERP/orderbook?depth=100") r.raise_for_status() ftx_data = json.loads(r.text, parse_float=Decimal) def ftx_checksum(book): bid_it = reversed(book[BID]) ask_it = iter(book[ASK]) bids = [f"{bid}:{book[BID][bid]}" for bid in bid_it] asks = [f"{ask}:{book[ASK][ask]}" for ask in ask_it] if len(bids) == len(asks): combined = [val for pair in zip(bids, asks) for val in pair] elif len(bids) > len(asks): combined = [ val for pair in zip(bids[:len(asks)], asks) for val in pair ] combined += bids[len(asks):] else: combined = [ val for pair in zip(bids, asks[:len(bids)]) for val in pair ] combined += asks[len(bids):] computed = ":".join(combined).encode() return zlib.crc32(computed) ob = OrderBook(checksum_format='FTX') book = { BID: sd({ Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['bids'] }), ASK: sd({ Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['asks'] }) } ob.bids = { Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['bids'] } ob.asks = { Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['asks'] } ob.checksum() == ftx_checksum(book)
def test_orderbook_setitem(): ob = OrderBook() data = requests.get( "https://api.pro.coinbase.com/products/BTC-USD/book?level=2").json() ob.bids = {Decimal(price): size for price, size, _ in data['bids']} ob.asks = {Decimal(price): size for price, size, _ in data['asks']} assert ob.bids.index(0)[0] < ob.asks.index(0)[0] assert ob.bids.index(-1)[0] < ob.asks.index(-1)[0] assert ob.bids.index(-1)[0] < ob.bids.index(0)[0] assert ob.asks.index(-1)[0] > ob.asks.index(0)[0]
def test_okcoin_checksum(): ob = OrderBook(checksum_format='OKCOIN') asks = { Decimal("3366.8"): Decimal("9"), Decimal("3368"): Decimal("8"), Decimal("3372"): Decimal("8") } bids = {Decimal("3366.1"): Decimal("7")} expected = 831078360 ob.bids = bids ob.asks = asks assert ob.checksum() == expected
def main(): ob = OrderBook(checksum_format='KRAKEN') asks = { Decimal("0.05005"): Decimal("0.00000500"), Decimal("0.05010"): Decimal("0.00000500"), Decimal("0.05015"): Decimal("0.00000500"), Decimal("0.05020"): Decimal("0.00000500"), Decimal("0.05025"): Decimal("0.00000500"), Decimal("0.05030"): Decimal("0.00000500"), Decimal("0.05035"): Decimal("0.00000500"), Decimal("0.05040"): Decimal("0.00000500"), Decimal("0.05045"): Decimal("0.00000500"), Decimal("0.05050"): Decimal("0.00000500") } bids = { Decimal("0.05000"): Decimal("0.00000500"), Decimal("0.04995"): Decimal("0.00000500"), Decimal("0.04990"): Decimal("0.00000500"), Decimal("0.04980"): Decimal("0.00000500"), Decimal("0.04975"): Decimal("0.00000500"), Decimal("0.04970"): Decimal("0.00000500"), Decimal("0.04965"): Decimal("0.00000500"), Decimal("0.04960"): Decimal("0.00000500"), Decimal("0.04955"): Decimal("0.00000500"), Decimal("0.04950"): Decimal("0.00000500") } ob.asks = asks ob.bids = bids book = {BID: sd(bids), ASK: sd(asks)} start = time.time() checksum = calc_checksum(book) end = time.time() assert checksum == 974947235 print("Kraken") print("Depth 10") print(f"Python: {(end - start) * 1000000} microseconds") start = time.time() checksum = ob.checksum() end = time.time() assert checksum == 974947235 print(f"C: {(end - start) * 1000000} microseconds") print("\nDepth 1000") book = {BID: sd({Decimal(update[0]): Decimal(update[1]) for update in data['result']['XETHZUSD']['bids']}), ASK: sd({Decimal(update[0]): Decimal(update[1]) for update in data['result']['XETHZUSD']['asks']}) } ob.bids = {Decimal(update[0]): Decimal(update[1]) for update in data['result']['XETHZUSD']['bids']} ob.asks = {Decimal(update[0]): Decimal(update[1]) for update in data['result']['XETHZUSD']['asks']} start = time.time() checksum1 = calc_checksum(book) end = time.time() print(f"Python: {(end - start) * 1000000} microseconds") start = time.time() checksum2 = ob.checksum() end = time.time() print(f"C: {(end - start) * 1000000} microseconds") assert checksum1 == checksum2 print("\n\nFTX") ob = OrderBook(checksum_format='FTX') book = {BID: sd({Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['bids']}), ASK: sd({Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['asks']}) } ob.bids = {Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['bids']} ob.asks = {Decimal(update[0]): Decimal(update[1]) for update in ftx_data['result']['asks']} start = time.time() checksum1 = ftx_checksum(book) end = time.time() print(f"Python: {(end - start) * 1000000} microseconds") start = time.time() checksum2 = ob.checksum() end = time.time() print(f"C: {(end - start) * 1000000} microseconds") assert checksum1 == checksum2