Kraken Spot WebSocket API

The listed functions and data types allow to access to public and private (authenticated) websocket feeds.

SpotWebSocketClient

KrakenEx.SpotWebSocketModule.SpotWebSocketClientType
SpotWebSocketClient

Type 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 key
  • secret – Kraken Spot Secret key

Adjustable attributes:

  • cancel_public_connection – can be set disable the active public websocket connection
  • cancel_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 of SpotBaseRESTAPI
  • public_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 feeds
  • pending_subscriptions – List of pending subscribtions
  • pending_messages – List of pending messages (e.g. create_order events)

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 client

  • SpotWebSocketClient("the-api-key", "the-api-secret-key") – authenticated client for public and private requests

source

WebSocket utilities

KrakenEx.SpotWebSocketModule.connectMethod
connect(
    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 – the SpotWebSocketClient instance
  • callback::Core.Function – Callback function wich receives the websocket messages
  • public::Bool=true – switch to activate/deactivate the public websocket connection
  • private::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)
source
KrakenEx.SpotWebSocketModule.subscribeMethod
subscribe(;
    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)
source
KrakenEx.SpotWebSocketModule.unsubscribeMethod
unsubscribe(;
    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)
source

Client functions

KrakenEx.SpotWebSocketModule.create_orderMethod
create_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)
source
KrakenEx.SpotWebSocketModule.edit_orderMethod
edit_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)
source
KrakenEx.SpotWebSocketModule.cancel_orderMethod
cancel_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)
source
KrakenEx.SpotWebSocketModule.cancel_all_ordersMethod
cancel_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)
source
KrakenEx.SpotWebSocketModule.cancel_all_orders_after_xMethod
cancel_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)
source