SPB Git forge
1commits 1branches 0releases
189.0 KBsize
maindefault branch
4 h agolast push
Python 54.7% TypeScript 42.5% SQL 1.9%
1.7 KB · 37 lines python
Raw Blame History
1from decimal import Decimal23from ktrade.kraken.checksum import _enc, book_checksum, fmt_fixed4from ktrade.kraken.ws import LocalBook567def test_encoding_rules_from_docs():8    assert _enc("45285.2") == "452852"9    assert _enc("0.00100000") == "100000"10    assert fmt_fixed(Decimal("45285.2"), 1) == "45285.2"11    assert fmt_fixed(Decimal("0.001"), 8) == "0.00100000"121314def test_checksum_is_order_independent_and_top10_only():15    asks = [(f"{100 + i}.0", "1.00000000") for i in range(15)]16    bids = [(f"{99 - i}.0", "1.00000000") for i in range(15)]17    a = book_checksum(asks, bids)18    b = book_checksum(list(reversed(asks)), list(reversed(bids)))19    assert a == b20    # levels beyond the top 10 do not matter21    assert book_checksum(asks[:10], bids[:10]) == a22    assert book_checksum(asks[:9], bids[:10]) != a232425def test_local_book_apply_and_checksum():26    b = LocalBook("BTC/USD", depth=10, price_decimals=1, qty_decimals=8)27    snap = {"bids": [{"price": 100.0, "qty": 1.0}, {"price": 99.5, "qty": 2.0}], "asks": [{"price": 100.5, "qty": 1.5}, {"price": 101.0, "qty": 3.0}], "timestamp": "t"}28    expected = book_checksum([("100.5", "1.50000000"), ("101.0", "3.00000000")], [("100.0", "1.00000000"), ("99.5", "2.00000000")])29    assert b.apply("snapshot", {**snap, "checksum": expected})30    # update: remove a level, change another31    upd = {"bids": [{"price": 99.5, "qty": 0}], "asks": [{"price": 100.5, "qty": 0.5}]}32    expected2 = book_checksum([("100.5", "0.50000000"), ("101.0", "3.00000000")], [("100.0", "1.00000000")])33    assert b.apply("update", {**upd, "checksum": expected2})34    assert not b.apply("update", {"bids": [], "asks": [], "checksum": 12345})35    top = b.top()36    assert top["best_bid"] == 100.0 and top["best_ask"] == 100.5 and top["spread"] == 0.537