โ ๏ธ This is an unofficial collection of REST and websocket clients for Spot and Futures trading on the Kraken cryptocurrency exchange using Julia. Payward Ltd. and Kraken are in no way associated with the authors of this module and documentation.
Documentation: https://btschwertfeger.github.io/KrakenEx.jl/stable/
This project is based on the python-kraken-sdk.
There is no guarantee that this software will work flawlessly at this or later times. Of course, no responsibility is taken for possible profits or losses. This software probably has some errors in it, so use it at your own risk. Also no one should be motivated or tempted to invest assets in speculative forms of investment. By using this software you release the author(s) from any liability regarding the use of this software.
Clients:
- Spot REST Clients
- Spot Websocket Client
- Futures REST Clients
- Futures Websocket Client
General:
- access both public and private endpoints
- responsive error handling and custom exceptions
- tested using unit tests (see
test/*
) - extensive example scripts (see
/examples
)
- Installation and setup
- Spot Client Example Usage
- Futures Client Example Usage
- Base Clients Documentation
- Spot Client Documentation
- Futures Client Documentation
- Troubleshooting
- Notes
- References
using Pkg; Pkg.add("KrakenEx")
- Spot Trading: https://www.kraken.com/u/security/api
- Futures Trading: https://futures.kraken.com/trade/settings/api
- Futures Sandbox: https://demo-futures.kraken.com/settings/api
If any unexpected behavior occurs, please check your API permissions, rate limits, update the KrakenEx.jl SDK, see the Troubleshooting section, and if the error persits please open an issue.
... can be found in /examples/spot_rest_examples.jl
using KrakenEx: SpotBaseRESTAPI
using KrakenEx.SpotMarketModule
using KrakenEx.SpotUserModule
using KrakenEx.SpotTradeModule
using KrakenEx.SpotFundingModule
using KrakenEx.SpotStakingModule
# you can also import all specific functions separate like shown in `/examples/spot_rest_examples.jl`
function main()
key = "Kraken-public-key"
secret = "Kraken-secret-key"
client = SpotBaseRESTAPI() # public client
private_client = SpotBaseRESTAPI(
key=key,
secret=secret
) # authenticated client
#===== User Endpoints =====#
println(get_account_balance(private_client))
println(get_open_orders(private_client))
# ...
#===== Market Endpoints =====#
println(get_assets(client))
println(get_tradable_asset_pair(client, pair=["XBTUSD", "DOTUSD"]))
println(get_ticker(client, pair="DOTUSD"))
# ...
#===== Trade Endpoints =====#
println(
create_order(
private_client,
ordertype="limit",
side="buy",
volume=1,
pair="XBTUSD",
price="10",
oflags="post",
validate=true
)
)
# ...
#===== Funding Endpoints =====#
println(get_deposit_methods(private_client, asset="DOT"))
println(get_deposit_address(private_client, asset="DOT", method="Polkadot"))
# ...
#===== Staking Endpoints =====#
println(list_stakeable_assets(private_client))
println(stake_asset(
private_client, asset="DOT", amount=20000, method="polkadot-staked"
))
# ...
end
main()
... can be found in /examples/spot_websocket_example.jl
using KrakenEx
using KrakenEx.SpotWebSocketModule:
SpotWebSocketClient,
connect,
subscribe, unsubscribe
function main()
key = "Kraken-public-key"
secret = "Kraken-secret-key"
ws_client = SpotWebSocketClient(key, secret)
function on_message(msg::Union{Dict{String,Any},String})
println(msg)
# implement your strategy here....
#=
Dont forget that you can also access public rest endpoints here.
If the `ws_client` instance is authenticated, you can also
use private endpoints:
KrakenEx.SpotMarketModule.cancel_order(
ws_client.rest_client,
txid="XXXXXX-XXXXXX-XXXXXX"
)
=#
end
# the conn will create a public and a private websocket connection
con = @async connect(ws_client, callback=on_message, private=true)
#== Subscribe to public and private websocket feeds ==#
subscribe(
client=ws_client,
subscription=Dict{String,Any}("name" => "ticker"),
pairs=["XBT/USD", "DOT/USD"]
)
subscribe(
client=ws_client,
subscription=Dict{String,Any}("name" => "ownTrades")
)
# wait before unsubscribe is done ...
sleep(2)
#== Unsubscribe from public and private websocket feeds ==#
# unsubscribe(
# client=ws_client,
# subscription=Dict{String,Any}("name" => "ticker"),
# pairs=["XBT/USD", "DOT/USD"]
# )
# unsubscribe(
# client=ws_client,
# subscription=Dict{String,Any}("name" => "ownTrades")
# )
# to cancel a connection you can use:
# ws_client.cancel_private_connection = true
# ws_client.cancel_public_connection = true
wait(con)
end
main()
Kraken provides a sandbox environment at https://demo-futures.kraken.com for paper trading. When using this API keys you have to set the DEMO
parameter to true
when instantiating the respecitve client.
The following example can be found in /examples/futures_rest_examples.jl
.
using KrakenEx: FuturesBaseRESTAPI
using KrakenEx.FuturesMarketModule
using KrakenEx.FuturesUserModule
using KrakenEx.FuturesTradeModule
using KrakenEx.FuturesFundingModule
function main()
# private clients can also access public endpoints
private_client = FuturesBaseRESTAPI(
key="Kraken-public-key",
secret="Kraken-secret-key"
)
# sandbox_client = FuturesBaseRESTAPI(
# API_KEY=key,
# SECRET_KEY=secret,
# DEMO=true
# )
#==== User Endpoints ====#
println(get_wallets(private_client))
println(get_open_orders(private_client))
log = get_account_log_csv(client)
open("myAccountLog.csv", "w") do io
write(io, log)
end
# ...
#==== Market Endpoints ====#
println(get_ohlc(private_client,
tick_type="trade",
symbol="PI_XBTUSD",
resolution="1m",
from=1668989233,
to=1668999233
))
# ...
#==== Trade Endpoints ====#
println(create_order(private_client,
orderType="lmt",
side="buy",
size=1,
limitPrice=4,
symbol="pf_bchusd",
))
println(create_batch_order(client, batchorder_list=[
Dict{String,Any}(
"order" => "send",
"order_tag" => "1",
"orderType" => "lmt",
"symbol" => "PI_XBTUSD",
"side" => "buy",
"size" => 1,
"limitPrice" => 1.00,
"cliOrdId" => "my-another-client-id"
),
Dict{String,Any}(
"order" => "send",
"order_tag" => "2",
"orderType" => "stp",
"symbol" => "PI_XBTUSD",
"side" => "buy",
"size" => 1,
"limitPrice" => 2.00,
"stopPrice" => 3.00,
),
Dict{String,Any}(
"order" => "send",
"order_tag" => "2",
"orderType" => "stp",
"symbol" => "PI_XBTUSD",
"side" => "buy",
"size" => 1,
"limitPrice" => 2.00,
"stopPrice" => 3.00,
),
Dict{String,Any}(
"order" => "cancel",
"order_id" => "e35d61dd-8a30-4d5f-a574-b5593ef0c050",
),
Dict{String,Any}(
"order" => "cancel",
"cliOrdId" => "my_client_id1234"
)
]))
println(cancel_all_orders(private_client))
# ...
#==== Funding Endpoints ====#
println(get_historical_funding_rates(private_client, symbol="PI_XBTUSD"))
# ...
end
main()
The following example can be found in /examples/futures_websocket_example.jl
.
using KrakenEx
using .KrakenEx.FuturesWebSocketModule:
FuturesWebSocketClient,
connect,
subscribe, unsubscribe
function main()
key = "Kraken-public-key"
secret = "Kraken-secret-key"
ws_client = FuturesWebSocketClient(key, secret)
function on_message(msg::Union{Dict{String,Any},String})
println(msg)
# implement your strategy here....
#=
Dont forget that you can also access public rest endpoints here.
If the `ws_client` instance is authenticated, you can also
use private endpoints:
KrakenEx.FuturesMarketModule.cancel_order(
ws_client.rest_client,
txid="XXXXXX-XXXXXX-XXXXXX"
)
=#
end
#=
`connect` can establish a private and a public connection at the same time.
You can turn off either the private or the public connection
using `private=false` or `public=false`.
=#
con = @async connect(ws_client, callback=on_message, private=true)
#== Subscribe to public and private websocket feeds ==#
# public feeds
products::Vector{String} = ["PI_XBTUSD", "PF_SOLUSD"]
subscribe(client=ws_client, feed="ticker", products=products)
# subscribe(client=ws_client, feed="ticker", products=products)
# subscribe(client=ws_client, feed="book", products=products)
# subscribe(client=ws_client, feed="trade", products=products)
# subscribe(client=ws_client, feed="ticker_lite", products=products)
# subscribe(client=ws_client, feed="heartbeat")
# ...
# private feeds
subscribe(client=ws_client, feed="fills")
# subscribe(client=ws_client, feed="open_positions")
subscribe(client=ws_client, feed="open_orders")
# subscribe(client=ws_client, feed="open_orders_verbose")
# subscribe(client=ws_client, feed="deposits_withdrawals")
# subscribe(client=ws_client, feed="account_balances_and_margins")
# subscribe(client=ws_client, feed="balances")
# subscribe(client=ws_client, feed="account_log")
# subscribe(client=ws_client, feed="notifications_auth")
# ...
# wait before unsubscribe is done ...
sleep(2)
#== Unsubscribe from public and private websocket feeds ==#
unsubscribe(client=ws_client, feed="ticker", products=["PF_SOLUSD"])
# unsubscribe(client=ws_client, feed="ticker", products=["PF_XBTUSD"])
# unsubscribe(client=ws_client, feed="fills")
# unsubscribe(client=ws_client, feed="open_orders")
# ...
# to cancel a connection you can use:
# ws_client.cancel_private_connection = true
# ws_client.cancel_public_connection = true
# ...
wait(con)
end
main()
Note: Authenticated Futures websocket clients can also un/subscribe from/to public feeds.
KrakenEx
Type | Documentation |
---|---|
SpotBaseRESTAPI |
Base client for all Spot REST endpoints |
FuturesBaseRESTAPI |
Base client for all Futures REST endpoints |
KrakenEx.SpotUserModule
KrakenEx.SpotTradeModule
Method | Documentation |
---|---|
create_order |
https://docs.kraken.com/rest/#operation/addOrder |
create_order_batch |
https://docs.kraken.com/rest/#operation/addOrderBatch |
edit_order |
https://docs.kraken.com/rest/#operation/editOrder |
cancel_order |
https://docs.kraken.com/rest/#operation/cancelOrder |
cancel_all_orders |
https://docs.kraken.com/rest/#operation/cancelAllOrders |
cancel_all_orders_after_x |
https://docs.kraken.com/rest/#operation/cancelAllOrdersAfter |
cancel_order_batch |
https://docs.kraken.com/rest/#operation/cancelOrderBatch |
KrakenEx.SpotMarketModule
Method | Documentation |
---|---|
get_assets |
https://docs.kraken.com/rest/#operation/getAssetInfo |
get_tradable_asset_pair |
https://docs.kraken.com/rest/#operation/getTradableAssetPairs |
get_ticker |
https://docs.kraken.com/rest/#operation/getTickerInformation |
get_ohlc |
https://docs.kraken.com/rest/#operation/getOHLCData |
get_order_book |
https://docs.kraken.com/rest/#operation/getOrderBook |
get_recent_trades |
https://docs.kraken.com/rest/#operation/getRecentTrades |
get_recent_spreads |
https://docs.kraken.com/rest/#operation/getRecentSpreads |
get_system_status |
checks if Kraken is online |
KrakenEx.SpotFundingModule
Method | Documentation |
---|---|
get_deposit_methods |
https://docs.kraken.com/rest/#operation/getDepositMethods |
get_deposit_address |
https://docs.kraken.com/rest/#operation/getDepositAddresses |
get_recent_deposits_status |
https://docs.kraken.com/rest/#operation/getStatusRecentDeposits |
get_withdrawal_info |
https://docs.kraken.com/rest/#operation/getWithdrawalInformation |
withdraw_funds |
https://docs.kraken.com/rest/#operation/withdrawFund |
get_recent_withdraw_status |
https://docs.kraken.com/rest/#operation/getStatusRecentWithdrawals |
cancel_withdraw |
https://docs.kraken.com/rest/#operation/cancelWithdrawal |
wallet_transfer |
https://docs.kraken.com/rest/#operation/walletTransfer |
KrakenEx.SpotStakingModule
Method | Documentation |
---|---|
stake_asset |
https://docs.kraken.com/rest/#operation/stake |
unstake_asset |
https://docs.kraken.com/rest/#operation/unstake |
list_stakeable_assets |
https://docs.kraken.com/rest/#operation/getStakingAssetInfo |
get_pending_staking_transactions |
https://docs.kraken.com/rest/#operation/getStakingPendingDeposits |
list_staking_transactions |
https://docs.kraken.com/rest/#operation/getStakingTransactions |
KrakenEx.SpotWebSocketModule
Method | Documentation |
---|---|
SpotWebSocketClient |
structure to store data - this is needed for the connect function below. |
connect |
Can create a public and private websoket connection. |
subscribe |
https://docs.kraken.com/websockets/#message-subscribe |
unsubscribe |
https://docs.kraken.com/websockets/#message-unsubscribe |
create_order |
https://docs.kraken.com/websockets/#message-addOrder |
edit_order |
https://docs.kraken.com/websockets/#message-editOrder |
cancel_order |
https://docs.kraken.com/websockets/#message-cancelOrder |
cancel_all_orders |
https://docs.kraken.com/websockets/#message-cancelAll |
cancel_all_orders_after |
https://docs.kraken.com/websockets/#message-cancelAllOrdersAfter |
KrakenEx.FuturesUserModule
KrakenEx.FuturesTradeModule
KrakenEx.FuturesMarketModule
KrakenEx.FuturesFundingModule
Method | Documentation |
---|---|
get_historical_funding_rates |
https://docs.futures.kraken.com/#http-api-trading-v3-api-historical-funding-rates-historicalfundingrates |
initiate_wallet_transfer |
https://docs.futures.kraken.com/#http-api-trading-v3-api-transfers-initiate-wallet-transfer |
initiate_subccount_transfer |
https://docs.futures.kraken.com/#http-api-trading-v3-api-transfers-initiate-sub-account-transfer |
initiate_withdrawal_to_spot_wallet |
https://docs.futures.kraken.com/#http-api-trading-v3-api-transfers-initiate-withdrawal-to-spot-wallet |
KrakenEx.FuturesWebSocketModule
Method | Documentation |
---|---|
FuturesWebSocketClient |
structure which is needed to use the connect function below. |
subscribe |
subscribe to a feed |
unsubscribe |
unsubscribe from a feed |
connect |
function to establish websock connections |
- Check if your version of KrakenEx.jl version is the newest.
- Check the permissions of your API keys and the required permissions on the respective endpoints.
- If you get some cloudflare or rate limit errors, please check your Kraken Tier level and maybe apply for a higher rank if required.
- Use different API keys for different algorithms, because the nonce calculation is based on timestamps and a sent nonce must always be the highest nonce ever sent of that API key. Having multiple algorithms using the same keys will result in invalid nonce errors.
-
Coding standards are not always followed to make arguments and function names as similar as possible to those in the Kraken API documentations.
-
When calling endpoints for examlpe the futures funding endpoint and you submit spaces, braces,... in strings like
" )|] "
a KrakenAuthenticationError will be raised.
- https://docs.kraken.com/rest
- https://docs.kraken.com/websockets
- https://docs.futures.kraken.com
- https://support.kraken.com/hc/en-us/sections/360012894412-Futures-API