Skip to content

Commit 0b12fd6

Browse files
kraken.spot.OrderbookClient: add timestamps to book's ask and bid values (#124)
1 parent bc541f8 commit 0b12fd6

3 files changed

Lines changed: 59 additions & 23 deletions

File tree

CHANGELOG.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,45 @@
22

33
## [Unreleased](https://github.com/btschwertfeger/python-kraken-sdk/tree/HEAD)
44

5-
[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v1.3.0...HEAD)
5+
[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v1.4.1...HEAD)
6+
7+
Uncategorized merged pull requests:
8+
9+
- Add "Question" issue template [\#122](https://github.com/btschwertfeger/python-kraken-sdk/pull/122) ([btschwertfeger](https://github.com/btschwertfeger))
10+
11+
## [v1.4.1](https://github.com/btschwertfeger/python-kraken-sdk/tree/v1.4.1) (2023-06-28)
12+
13+
[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v1.4.0...v1.4.1)
14+
15+
**Fixed bugs:**
16+
17+
- `kraken.spot.Market.get_recent_trades`: 'since' parameter does not work [\#119](https://github.com/btschwertfeger/python-kraken-sdk/issues/119)
18+
- Fix `kraken.spot.Market.get_recent_trades` parameter 'since' [\#120](https://github.com/btschwertfeger/python-kraken-sdk/pull/120) ([btschwertfeger](https://github.com/btschwertfeger))
19+
20+
**Closed issues:**
21+
22+
- Create `.github/release.yaml` [\#108](https://github.com/btschwertfeger/python-kraken-sdk/issues/108)
23+
24+
## [v1.4.0](https://github.com/btschwertfeger/python-kraken-sdk/tree/v1.4.0) (2023-06-16)
25+
26+
[Full Changelog](https://github.com/btschwertfeger/python-kraken-sdk/compare/v1.3.0...v1.4.0)
627

728
**Implemented enhancements:**
829

930
- Add the `truncate` parameter to `create_order` of the Spot websocket client [\#111](https://github.com/btschwertfeger/python-kraken-sdk/issues/111)
10-
- Add the `truncate` parameter to create_order of the Spot websocket clients' `create_order` and `cancel_order`+ `kraken.spot.Trade.edit_order` [\#113](https://github.com/btschwertfeger/python-kraken-sdk/pull/113) ([btschwertfeger](https://github.com/btschwertfeger))
31+
- Add a Spot Orderbook client that handles a realtime order book [\#104](https://github.com/btschwertfeger/python-kraken-sdk/issues/104)
32+
- A the Spot order book client \(`kraken.spot.OrderbookClient`\) [\#106](https://github.com/btschwertfeger/python-kraken-sdk/pull/106) ([btschwertfeger](https://github.com/btschwertfeger))
33+
- Add the `truncate` parameter to the Spot websocket clients' `create_order` and `cancel_order`+ `kraken.spot.Trade.edit_order` [\#113](https://github.com/btschwertfeger/python-kraken-sdk/pull/113) ([btschwertfeger](https://github.com/btschwertfeger))
34+
35+
**Fixed bugs:**
36+
37+
- user.get_trade_volume\(\) says it supports multiple currencies as a list, but it does not seem to. [\#115](https://github.com/btschwertfeger/python-kraken-sdk/issues/115)
38+
- kraken.exceptions.KrakenException.KrakenInvalidNonceError: An invalid nonce was supplied. [\#114](https://github.com/btschwertfeger/python-kraken-sdk/issues/114)
1139

1240
Uncategorized merged pull requests:
1341

1442
- Update `/examples/spot_orderbook.py` [\#110](https://github.com/btschwertfeger/python-kraken-sdk/pull/110) ([btschwertfeger](https://github.com/btschwertfeger))
43+
- Create `release.yaml` [\#116](https://github.com/btschwertfeger/python-kraken-sdk/pull/116) ([btschwertfeger](https://github.com/btschwertfeger))
1544

1645
## [v1.3.0](https://github.com/btschwertfeger/python-kraken-sdk/tree/v1.3.0) (2023-05-24)
1746

examples/spot_orderbook.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
#
66

77
"""
8-
NOTE: * The Spot Orderbook client is not released yet. It will be released
9-
in python-kraken-sdk=v1.4.0.
10-
* Have a look at https://gist.github.com/btschwertfeger/6eea0eeff193f7cd1b262cfce4f0eb51
11-
for an example that works now.
12-
138
This module provides an example on how to use the Spot Orderbook
149
client of the python-kraken-sdk (https://github.com/btschwertfeger/python-kraken-sdk)
1510
to retrieve and maintain a valid Spot order book for (a) specific
@@ -74,7 +69,7 @@ async def on_book_update(self: "Orderbook", pair: str, message: list) -> None:
7469
print("Bid Volume\t\t Ask Volume")
7570
for level in range(self.depth):
7671
print(
77-
f"{bid[level][0]} ({bid[level][1]}) \t {ask[level][0]} ({ask[level][1]})"
72+
f"{bid[level][0]} ({bid[level][1][0]}) \t {ask[level][0]} ({ask[level][1][0]})"
7873
)
7974

8075
assert book["valid"] # ensure that the checksum is valid

kraken/spot/orderbook.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def exception_occur(self: "OrderbookClient") -> bool:
257257
:return: ``True`` if any critical error occurred else ``False``
258258
:rtype: bool
259259
"""
260-
return self.ws_client.exception_occur
260+
return bool(self.ws_client.exception_occur)
261261

262262
def get(self: "OrderbookClient", pair: str) -> Optional[dict]:
263263
"""
@@ -267,6 +267,24 @@ def get(self: "OrderbookClient", pair: str) -> Optional[dict]:
267267
:type pair: str
268268
:return: The orderbook of that ``pair``.
269269
:rtype: dict
270+
271+
.. code-block::python
272+
:linenos:
273+
:caption: Orderbook: Get ask and bid
274+
275+
276+
class Orderbook(OrderbookClient):
277+
278+
async def on_book_update(
279+
self: "Orderbook",
280+
pair: str,
281+
message: list
282+
) -> None:
283+
book: Dict[str, Any] = self.get(pair="XBT/USD")
284+
ask: List[Tuple[str, str]] = list(book["ask"].items())
285+
bid: List[Tuple[str, str]] = list(book["bid"].items())
286+
# ask and bid are now in format [price, (volume, timestamp)]
287+
# … and include the whole orderbook
270288
"""
271289
return self.__book.get(pair)
272290

@@ -286,8 +304,8 @@ def __update_book(
286304
['25030.40000', '2.77134976', '1684658131.751539'],
287305
['25032.20000', '0.13978808', '1684658131.751577']
288306
]
289-
... where the first value is the ask or bid price, the second
290-
represents the volume and the last one is the time stamp.
307+
where the first value is the ask or bid price, the second
308+
represents the volume and the last one is the timestamp.
291309
292310
:param side: The side to assign the data to,
293311
either ``ask`` or ``bid``
@@ -298,10 +316,11 @@ def __update_book(
298316
for entry in snapshot:
299317
price: str = entry[0]
300318
volume: str = entry[1]
319+
timestamp: str = entry[2]
301320

302321
if float(volume) > 0.0:
303322
# Price level exist or is new
304-
self.__book[pair][side][price] = volume
323+
self.__book[pair][side][price] = (volume, timestamp)
305324
else:
306325
# Price level moved out of range
307326
self.__book[pair][side].pop(price)
@@ -333,28 +352,21 @@ def __validate_checksum(self: "OrderbookClient", pair: str, checksum: str) -> No
333352
:type checksum: str
334353
"""
335354
book: dict = self.__book[pair]
336-
337-
# sort ask (desc) and bid (asc)
338-
ask: List[tuple] = sorted(book["ask"].items(), key=self.get_first)
339-
bid: List[tuple] = sorted(
340-
book["bid"].items(),
341-
key=self.get_first,
342-
reverse=True,
343-
)
355+
ask = list(book["ask"].items())
356+
bid = list(book["bid"].items())
344357

345358
local_checksum: str = ""
346-
for price_level, volume in ask[:10]:
359+
for price_level, (volume, _) in ask[:10]:
347360
local_checksum += price_level.replace(".", "").lstrip("0") + volume.replace(
348361
".", ""
349362
).lstrip("0")
350363

351-
for price_level, volume in bid[:10]:
364+
for price_level, (volume, _) in bid[:10]:
352365
local_checksum += price_level.replace(".", "").lstrip("0") + volume.replace(
353366
".", ""
354367
).lstrip("0")
355368

356369
self.__book[pair]["valid"] = checksum == str(crc32(local_checksum.encode()))
357-
# assert self.__book[pair]["valid"]
358370

359371
@staticmethod
360372
def get_first(values: tuple) -> float:

0 commit comments

Comments
 (0)