Skip to content

Commit 19613f4

Browse files
Update spot REST examples
1 parent ceeedee commit 19613f4

File tree

6 files changed

+103
-98
lines changed

6 files changed

+103
-98
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Projects using this SDK:
7171

7272
- https://github.com/btschwertfeger/kraken-infinity-grid
7373
- https://github.com/btschwertfeger/kraken-rebalance-bot
74+
- https://github.com/btschwertfeger/python-kraken-sdk/network/dependents
7475

7576
---
7677

examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# python-kraken-sdk usage examples
22

33
This directory contains several examples demonstrating how to use the
4-
`python-kraken-sdk`. These examples cover various functionalities such as spot
5-
trading, futures trading, and market data retrieval using both REST and
4+
`python-kraken-sdk`. These examples cover various functionalities such as Spot
5+
trading, Futures trading, and market data retrieval using both REST and
66
WebSocket APIs.
77

88
Ideal examples of successful running trading algorithms based on the
99
python-kraken-sdk are listed below:
1010

1111
- https://github.com/btschwertfeger/kraken-infinity-grid
1212
- https://github.com/btschwertfeger/kraken-rebalance-bot
13+
- https://github.com/btschwertfeger/python-kraken-sdk/network/dependents
1314

1415
For more detailed and up-to-date usage, refer to the unit tests provided in the
1516
main package.

examples/futures_examples.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
#
88

99
"""
10-
Module that implements some example usage for the Kraken Futures REST clients.
11-
12-
This module may not be maintained on a regular basis, so please refer to the
13-
unit tests of this package as they provide a much deeper dive into the usage.
10+
Module that implements *some* examples for the Kraken Futures REST clients
11+
usage.
1412
"""
1513

1614
import logging

examples/futures_ws_examples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import asyncio
1616
import logging
17-
import logging.config
1817
import os
1918
import time
2019

examples/spot_examples.py

Lines changed: 97 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
#
88

99
"""
10-
Module that implements some samples for the Kraken Spot REST clients.
11-
12-
This module may not be maintained on a regular basis, so please refer to the
13-
unit tests of this package as they provide a much deeper dive into the usage.
10+
Module that implements *some* examples for the Kraken Spot REST clients usage.
1411
"""
1512

1613
import logging
@@ -29,37 +26,41 @@
2926
logging.getLogger("urllib3").setLevel(logging.WARNING)
3027

3128

32-
key = os.getenv("API_KEY")
33-
secret = os.getenv("SECRET_KEY")
29+
key = os.getenv("SPOT_API_KEY")
30+
secret = os.getenv("SPOT_SECRET_KEY")
3431

3532

3633
def user_examples() -> None:
37-
"""Example usage of the User client"""
34+
"""Example usage of the Spot User client"""
35+
# Usage of the User client to access private endpoints:
3836
user = User(key=key, secret=secret)
3937

4038
print(user.get_account_balance())
41-
print(user.get_trade_balance()) # asset='BTC'
39+
print(user.get_trade_balance()) # asset="BTC"
4240
print(user.get_open_orders())
4341
print(user.get_closed_orders())
4442
print(
45-
user.get_orders_info(txid="OBQFM7-JNVKS-H3ULEH"),
46-
) # or txid='id1,id2,id3' or txid=['id1','id2']
43+
user.get_orders_info(
44+
txid="OBQFM7-JNVKS-H3ULEH", # or txid="id1,id2,id3" or txid=["id1","id2"]
45+
),
46+
)
4747
print(user.get_trades_history())
48-
time.sleep(3)
48+
time.sleep(3) # to avoid rate limit
4949
print(user.get_trades_info(txid="TCNTTR-QBEVO-E5H5UK"))
50-
print(user.get_open_positions()) # txid='someid'
50+
print(user.get_open_positions()) # or txid="someid"
5151
print(
52-
user.get_ledgers_info(),
53-
) # asset='BTC' or asset='BTC,EUR' or asset=['BTC','EUR']
52+
user.get_ledgers_info(), # asset="BTC" or asset="BTC,EUR" or asset=["BTC","EUR"]
53+
)
5454
print(user.get_ledgers(id_="LIORGR-33NXH-LBUS5Z"))
55-
print(user.get_trade_volume()) # pair='BTC/EUR'
55+
print(user.get_trade_volume()) # pair="BTC/EUR"
5656

57-
# ____export_report____
57+
# Exporting a ledger and trade report can be useful for analysis or
58+
# record-keeping purposes:
5859
response = user.request_export_report(
59-
report="ledgers",
60+
report="ledgers", # or report="trades"
6061
description="myLedgers1",
6162
format_="CSV",
62-
) # report='trades'
63+
)
6364
print(user.get_export_report_status(report="ledgers"))
6465

6566
# save report to file
@@ -71,11 +72,11 @@ def user_examples() -> None:
7172

7273
print(
7374
user.delete_export_report(id_=response["id"], type_="delete"),
74-
) # alternative: type_=cancel
75+
)
7576

7677

7778
def market_examples() -> None:
78-
"""Example usage of the Market client"""
79+
"""Example usage of the Spot Market client"""
7980
market = Market()
8081

8182
print(market.get_assets(assets=["XBT"]))
@@ -90,61 +91,62 @@ def market_examples() -> None:
9091

9192

9293
def trade_examples() -> None:
93-
"""Example usage of the Trade client"""
94-
raise ValueError(
95-
"Attention: Please check if you really want to execute trade functions.",
94+
"""Example usage of the Spot Trade client"""
95+
print(
96+
"Attention: Please check if you really want to execute trade functions."
97+
" Running them without caution may lead to unintended orders!",
9698
)
99+
return
97100
trade = Trade(key=key, secret=secret)
98101

99-
if False:
100-
print(
101-
trade.create_order(
102-
ordertype="limit",
103-
side="buy",
104-
volume=1,
105-
pair="BTC/EUR",
106-
price=0.01,
107-
),
108-
)
109-
print(
110-
trade.create_order_batch(
111-
orders=[
112-
{
113-
"close": {
114-
"ordertype": "stop-loss-limit",
115-
"price": 120,
116-
"price2": 110,
117-
},
118-
"ordertype": "limit",
119-
"price": 140,
120-
"price2": 130,
121-
"timeinforce": "GTC",
122-
"type": "buy",
123-
"userref": "345dsdfddfgdsgdfgsfdsfsdf",
124-
"volume": 1000,
125-
},
126-
{
127-
"ordertype": "limit",
128-
"price": 150,
129-
"timeinforce": "GTC",
130-
"type": "sell",
131-
"userref": "1dfgesggwe5t3",
132-
"volume": 123,
102+
print(
103+
trade.create_order(
104+
ordertype="limit",
105+
side="buy",
106+
volume=1,
107+
pair="BTC/EUR",
108+
price=0.01,
109+
),
110+
)
111+
print(
112+
trade.create_order_batch(
113+
orders=[
114+
{
115+
"close": {
116+
"ordertype": "stop-loss-limit",
117+
"price": 120,
118+
"price2": 110,
133119
},
134-
],
135-
pair="BTC/USD",
136-
validate=True,
137-
),
138-
)
120+
"ordertype": "limit",
121+
"price": 140,
122+
"price2": 130,
123+
"timeinforce": "GTC",
124+
"type": "buy",
125+
"userref": "345dsdfddfgdsgdfgsfdsfsdf",
126+
"volume": 1000,
127+
},
128+
{
129+
"ordertype": "limit",
130+
"price": 150,
131+
"timeinforce": "GTC",
132+
"type": "sell",
133+
"userref": "1dfgesggwe5t3",
134+
"volume": 123,
135+
},
136+
],
137+
pair="BTC/USD",
138+
validate=True,
139+
),
140+
)
139141

140-
print(
141-
trade.edit_order(txid="sometxid", pair="BTC/EUR", volume=4.2, price=17000),
142-
)
143-
time.sleep(2)
142+
print(
143+
trade.edit_order(txid="sometxid", pair="BTC/EUR", volume=4.2, price=17000),
144+
)
145+
time.sleep(2)
144146

145-
print(trade.cancel_order(txid="O2JLFP-VYFIW-35ZAAE"))
146-
print(trade.cancel_all_orders())
147-
print(trade.cancel_all_orders_after_x(timeout=6))
147+
print(trade.cancel_order(txid="O2JLFP-VYFIW-35ZAAE"))
148+
print(trade.cancel_all_orders())
149+
print(trade.cancel_all_orders_after_x(timeout=6))
148150

149151
print(
150152
trade.cancel_order_batch(
@@ -162,35 +164,40 @@ def funding_examples() -> None:
162164
"""Example usage of the Funding client"""
163165
funding = Funding(key=key, secret=secret)
164166
print(funding.get_deposit_methods(asset="DOT"))
165-
# print(funding.get_deposit_address(asset='DOT', method='Polkadot'))
166-
# print(funding.get_recent_deposits_status(asset='DOT'))
167+
# print(funding.get_deposit_address(asset="DOT", method="Polkadot"))
168+
# print(funding.get_recent_deposits_status(asset="DOT"))
167169
print(
168170
funding.get_withdrawal_info(asset="DOT", key="MyPolkadotWallet", amount="200"),
169171
)
170172

171-
raise ValueError(
172-
"Attention: Please check if you really want to execute funding functions.",
173+
print(
174+
"Attention: Please check if you really want to execute funding functions."
175+
" Running them without caution may lead to unintended withdrawals!",
176+
)
177+
return
178+
time.sleep(2) # to avoid rate limit
179+
print(funding.withdraw_funds(asset="DOT", key="MyPolkadotWallet", amount=200))
180+
print(funding.get_recent_withdraw_status(asset="DOT"))
181+
print(funding.cancel_withdraw(asset="DOT", refid="12345"))
182+
print(
183+
funding.wallet_transfer(
184+
asset="ETH",
185+
amount=0.100,
186+
from_="Spot Wallet",
187+
to_="Futures Wallet",
188+
),
173189
)
174-
if False:
175-
time.sleep(2)
176-
print(funding.withdraw_funds(asset="DOT", key="MyPolkadotWallet", amount=200))
177-
print(funding.get_recent_withdraw_status(asset="DOT"))
178-
print(funding.cancel_widthdraw(asset="DOT", refid="12345"))
179-
print(
180-
funding.wallet_transfer(
181-
asset="ETH",
182-
amount=0.100,
183-
from_="Spot Wallet",
184-
to_="Futures Wallet",
185-
),
186-
)
187190

188191

189192
def main() -> None:
190-
user_examples()
191-
market_examples()
192-
trade_examples()
193-
funding_examples()
193+
"""Uncomment the examples you want to run:"""
194+
# NOTE: These are only examples that show how to use the clients, there are
195+
# many other functions available in the clients.
196+
197+
# user_examples()
198+
# market_examples()
199+
# trade_examples()
200+
# funding_examples()
194201

195202

196203
if __name__ == "__main__":

examples/spot_ws_examples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import asyncio
1717
import logging
18-
import logging.config
1918
import os
2019

2120
from kraken.spot import SpotWSClient

0 commit comments

Comments
 (0)