Kraken Spot WebSocket API
The listed functions and data types allow to access to public and private (authenticated) websocket feeds.
SpotWebSocketClient
KrakenEx.SpotWebSocketModule.SpotWebSocketClient — TypeSpotWebSocketClientType that stores information about the client and can be used to establish public and private websocket connections for Kraken Spot trading.
Fields
key– Kraken Spot API keysecret– Kraken Spot Secret key
Adjustable attributes:
cancel_public_connection– can be set disable the active public websocket connectioncancel_private_connection– can be set to disable the active private websocket connection
The following will be managed by the constructor or connection:
rest_client– public or private instance ofSpotBaseRESTAPIpublic_url– default websocket url for Kraken public Spot trading (default: "ws.kraken.com")private_url– default websocket url for Kraken private Spot trading (default: "ws-auth.kraken.com")active_subscriptions– List of active subscribed feedspending_subscriptions– List of pending subscribtionspending_messages– List of pending messages (e.g.create_orderevents)
Notes
The attribute rest_client stores a valid SpotBaseRESTAPI client, so REST endpoints can also be accessed using this attributes. If the SpotWebSocketClient is authenticated, so the rest_client attribute can also access private endpoints.
Examples
SpotWebSocketClient()– default, public clientSpotWebSocketClient("the-api-key", "the-api-secret-key")– authenticated client for public and private requests
WebSocket utilities
KrakenEx.SpotWebSocketModule.connect — Methodconnect(
client::SpotWebSocketClient;
callback::Core.Function,
public::Bool=true,
private::Bool=false
)Can create up to two (one private and one public) websocket connections. The public and/or private websocket object will be stored within the SpotWebSocketClient. Websocket feeds can be subscribed and unsubscribed after a successful connection. This function must be invoked using @async. Private websocket connections and privat feed subscriptions requre an authenticated SpotWebSocketClient.
Attributes
client::SpotWebSocketClient– theSpotWebSocketClientinstancecallback::Core.Function– Callback function wich receives the websocket messagespublic::Bool=true– switch to activate/deactivate the public websocket connectionprivate::Bool=false– switch to activate/deactivate the private websocket connection
Example
julia> # ws_client = SpotWebSocketClient() # unauthenticated
julia> ws_client = SpotWebSocketClient("api-key", "api-secret") # authenticated
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... # implement your strategy here
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD", "DOT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)KrakenEx.SpotWebSocketModule.subscribe — Methodsubscribe(;
client::SpotWebSocketClient,
subscription::Dict{String,Any},
pairs::Union{Vector{String},Nothing}=nothing
)Kraken Docs: https://docs.kraken.com/websockets/#message-subscribe
Subscribe to a websocket feed.
Example
julia> ws_client = SpotWebSocketClient()
julia> on_message(msg::Union{Dict{String,Any},String}) = println(msg)
julia> con = @async connect(ws_client, callback=on_message)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD", "DOT/USD"]
... )
julia> wait(conn)KrakenEx.SpotWebSocketModule.unsubscribe — Methodunsubscribe(;
client::SpotWebSocketClient,
subscription::Dict{String,Any},
pairs::Union{Vector{String},Nothing}=nothing
)Kraken Docs: https://docs.kraken.com/websockets/#message-unsubscribe
Unsubscribe from a subscribed feed.
Example
julia> ws_client = SpotWebSocketClient()
julia> on_message(msg::Union{Dict{String,Any},String}) = println(msg)
julia> con = @async connect(ws_client, callback=on_message)
julia> unsubscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD", "DOT/USD"]
... )
julia> wait(conn)Client functions
KrakenEx.SpotWebSocketModule.create_order — Methodcreate_order(client::SpotWebSocketClient;
ordertype::String,
side::String,
pair::String,
volume::Union{String,Nothing},
price::Union{String,Nothing}=nothing,
price2::Union{String,Nothing}=nothing,
leverage::Union{Float64,Int64,String,Nothing}=nothing,
oflags::Union{String,Vector{String},Nothing}=nothing,
starttm::String="0",
expiretim::String="0",
deadline::Union{String,Nothing}=nothing,
userref::Union{Int32,Nothing}=nothing,
validate::Bool=false,
close_ordertype::Union{String,Nothing}=nothing,
close_price::Union{String,Nothing}=nothing,
close_price2::Union{String,Nothing}=nothing,
timeinforce::Union{String,Nothing}=nothing
)Kraken Docs: https://docs.kraken.com/websockets/#message-addOrder
Authenticated client required
Example
julia> using KrakenEx.SpotWebsocketModule:
... SpotWebSocketClient,
... connect, subscribe,
... create_order
julia> ws_client = SpotWebSocketClient("api-key", "api-secret")
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... if condition # set your own conditions ...
... create_order(
... client,
... side="buy",
... pair="XBTUSD",
... volume="0.0001",
... ordertype="market",
... )
... end
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)KrakenEx.SpotWebSocketModule.edit_order — Methodedit_order(client::SpotWebSocketClient;
txid::String,
pair::String,
volume::Union{String,Int64,Float64,Nothing}=nothing,
price::Union{String,Int64,Float64,Nothing}=nothing,
price2::Union{String,Int64,Float64,Nothing}=nothing,
oflags::Union{String,Vector{String},Nothing}=nothing,
deadline::Union{String,Nothing}=nothing,
cancel_response::Bool=false,
validate::Bool=false,
userref::Union{Int32,Nothing}=nothing
)Kraken Docs: https://docs.kraken.com/websockets/#message-editOrder
Authenticated client required
Example
julia> using KrakenEx.SpotWebsocketModule:
... SpotWebSocketClient,
... connect, subscribe,
... edit_order
julia> ws_client = SpotWebSocketClient("api-key", "api-secret")
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... if condition # set your own conditions ...
... edit_order(
... client,
... txid="xxxxxx-xxxxxx-xxxxxx-xxxxxx",
... volume="0.0002",
... )
... end
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)KrakenEx.SpotWebSocketModule.cancel_order — Methodcancel_order(client::SpotWebSocketClient; txid::String)Kraken Docs: https://docs.kraken.com/websockets/#message-cancelOrder
Cancel an order by txid.
Authenticated client required
Example
julia> using KrakenEx.SpotWebsocketModule:
... SpotWebSocketClient,
... connect, subscribe,
... cancel_order
julia> ws_client = SpotWebSocketClient("api-key", "api-secret")
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... if condition # set your own conditions ...
... cancel_order(
... client,
... txid="xxxxxx-xxxxxx-xxxxxx-xxxxxx",
... )
... end
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)KrakenEx.SpotWebSocketModule.cancel_all_orders — Methodcancel_all_orders(client::SpotWebSocketClient)Kraken Docs: https://docs.kraken.com/websockets/#message-cancelAll
Cancel all orders.
Authenticated client required
Example
julia> using KrakenEx.SpotWebsocketModule:
... SpotWebSocketClient,
... connect, subscribe,
... cancel_all_orders
julia> ws_client = SpotWebSocketClient("api-key", "api-secret")
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... if condition # set your own conditions ...
... cancel_all_orders(client)
... end
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)KrakenEx.SpotWebSocketModule.cancel_all_orders_after_x — Methodcancel_all_orders_after_x(client::SpotWebSocketClient, timeout::Int)Kraken Docs: https://docs.kraken.com/websockets/#message-cancelAllOrdersAfter
Cancel all orders after timeout seconds. Set timeout=0 to reset.
Authenticated client required
Example
julia> using KrakenEx.SpotWebsocketModule:
... SpotWebSocketClient,
... connect, subscribe,
... cancel_all_orders_after_x
julia> ws_client = SpotWebSocketClient("api-key", "api-secret")
julia> function on_message(msg::Union{Dict{String,Any},String})
... println(msg)
... if condition # set your own conditions ...
... cancel_all_orders_after_x(client, timeout=60)
... end
... end
julia> con = @async connect(ws_client, callback=on_message, private=true)
julia> subscribe(
... client=ws_client,
... subscription=Dict{String,Any}("name" => "ticker"),
... pairs=["XBT/USD"]
... )
julia> # do more stuff ...
julia> wait(conn)