KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Connecting to API

  • Connecting to API

    Posted by hizmym on August 14, 2024 at 10:09 am

    Hi,

    I have few questions when it comes to connection to an API. is it possible to connect to public api for market data (binance or so on) using kdb q? how can i do it

    can I acheive this using .Q.hg or other methodslike web socket

    https://developers.binance.com/docs/binance-spot-api-docs/faqs

    this is the market data how can i connect to it use those data. Can i load the data save it to disk as table or other.

    your support is highly appreciated thank you.

    rocuinneagain replied 4 months, 2 weeks ago 3 Members · 2 Replies
  • 2 Replies
  • jlucid

    Member
    August 14, 2024 at 12:11 pm

    You can use this library by Jonathon McMurrray.
    Just load the ws-client_0.2.2.q file into a q session.
    This example below shows how to request Top of book messages for BTCUSDT

    https://github.com/jonathonmcmurray/ws.q

    Note, that because you need a secure connection (for wss), you need to have set the following environmental variables first. Including creation of your SSL certs.

    $export SSL_KEY_FILE=/path/to/server-key.pem

    $export SSL_CERT_FILE=/path/to/server-crt.pem

    $export SSL_VERIFY_SERVER=NO

    $rlwrap q ws-client_0.2.2.q -E 2

    q)func:{[x] show x}

    q).ws.open[$wss://stream.binance.com:443/ws/btcusdt@bookTicker";func]
    {\”u\”:50341952152,\”s\”:\”BTCUSDT\”,\”b\”:\”61344.99000000\”,\”B\”:\”7.60825000\”,\”a\”:\”61345.00000000\”,\”A\”:\”0.04012000\”}”

    You can parse the messages using .j.k

    q).j.k “{\”u\”:50341952152,\”s\”:\”BTCUSDT\”,\”b\”:\”61344.99000000\”,\”B\”:\”7.60825000\”,\”a\”:\”61345.00000000\”,\”A\”:\”0.04012000\”}”

    u| 5.034195e+10

    s| “BTCUSDT”

    b| “61344.99000000”

    B| “7.60825000”

    a| “61345.00000000”

    A| “0.04012000”

  • rocuinneagain

    Member
    August 14, 2024 at 12:31 pm

    You can see an example of streaming data from http://www.coinapi.io over Websockets here:

    For REST APIs the best library to use is kurl:

Log in to reply.