xStocks REST¶
The examples presented below serve to demonstrate the usage of the Spot REST clients provided by python-kraken-sdk to access Kraken’s REST API for trading xStocks.
For questions, feedback, additions, suggestions for improvement or problems python-kraken-sdk/discussions or python-kraken-sdk/issues may be helpful.
See https://docs.kraken.com/api/docs/guides/global-intro for information about the available endpoints and their usage.
Example usage of Spot REST client for xStocks¶
1# !/usr/bin/env python3
2# -*- mode: python; coding: utf-8 -*-
3#
4# Copyright (C) 2025 Benjamin Thomas Schwertfeger
5# All rights reserved.
6# https://github.com/btschwertfeger
7#
8
9"""
10Module that implements *some* examples for the Kraken Spot REST clients usage
11with focus on xStocks.
12
13NOTE: The xStocks feature is not available globally. Please checkout Kraken's
14 documentation to get to know the availability zones.
15"""
16
17import logging
18import os
19
20from kraken.spot import SpotClient, Trade
21
22logging.basicConfig(
23 format="%(asctime)s %(module)s,line: %(lineno)d %(levelname)8s | %(message)s",
24 datefmt="%Y/%m/%d %H:%M:%S",
25 level=logging.INFO,
26)
27logging.getLogger("requests").setLevel(logging.WARNING)
28logging.getLogger("urllib3").setLevel(logging.WARNING)
29
30
31key = os.getenv("SPOT_API_KEY")
32secret = os.getenv("SPOT_SECRET_KEY")
33
34CLIENT = SpotClient(key=key, secret=secret)
35
36
37def list_all_xstocks() -> None:
38 """List all available xStocks on Kraken."""
39 print(
40 CLIENT.request(
41 "GET",
42 "/0/public/AssetPairs",
43 params={"aclass_base": "tokenized_asset"}, # <- important!
44 auth=False,
45 ),
46 )
47
48
49def create_xstock_order() -> None:
50 """Create a test order for an xStock (validate mode, not placing actual order)."""
51 print(
52 CLIENT.request(
53 "POST",
54 "/0/private/AddOrder",
55 params={
56 "type": "buy",
57 "volume": "1",
58 "ordertype": "limit",
59 "pair": "AAPLxUSD",
60 "price": "100.0",
61 "validate": True,
62 "asset_class": "tokenized_asset", # <- important
63 },
64 ),
65 )
66
67
68def create_xstock_order_alternative() -> None:
69 """Create a test order for an xStock (validate mode, not placing actual order)."""
70 trade = Trade(key=key, secret=secret)
71
72 print(
73 trade.create_order(
74 pair="AAPLxUSD",
75 side="buy",
76 ordertype="limit",
77 volume="1",
78 price="100.0",
79 validate=True,
80 extra_params={"asset_class": "tokenized_asset"},
81 ),
82 )
83
84
85def main() -> None:
86 """Uncomment the examples you want to run:"""
87 # NOTE: These are only examples that show how to use the clients, there are
88 # many other functions available in the clients.
89 list_all_xstocks()
90 # create_xstock_order()
91 # create_xstock_order_alternative()
92
93
94if __name__ == "__main__":
95 main()