Search code examples
pythonpython-3.xkdb

Connect API to kdb database


simple question - I've successfully connected to the Coinbase API using Python 3.6 and receiving BTC buy/sell prices in my console.

I'd like to connect this to a kdb database and start creating a HDB of tick data but I'm a bit confused on how to structure this set-up, i.e., push the get requests to the database for storage. My python code looks like...

api_key = 'XXXXX'
api_secret = 'XXXXX'
from coinbase.wallet.client import Client
import time, requests


client = Client(api_key, api_secret)


starttime = time.time()
while True:
    buy_price = client.get_buy_price(currency_pair = 'BTC-USD')
    sell_price = client.get_sell_price(currency_pair = 'BTC-USD')
    time.sleep(10.0)
    print(buy_price)
    print(sell_price)
    print("=-=-=-=-=-=")

The console prints the feed, which looks like...

{
  "amount": "8034.79",
  "base": "BTC",
  "currency": "USD"
}
{
  "amount": "7875.67",
  "base": "BTC",
  "currency": "USD"
}
=-=-=-=-=-=
{
  "amount": "8034.80",
  "base": "BTC",
  "currency": "USD"
}
{
  "amount": "7875.97",
  "base": "BTC",
  "currency": "USD"
}
=-=-=-=-=-=

Any guidance in storing this data locally would be helpful. Please let me know if you need any additional info.

Thank you in advance!


Solution

  • You can use PyQ:

    >>> from pyq import q
    >>> p = {
    ...   "amount": "8034.79",
    ...   "base": "BTC",
    ...   "currency": "USD"
    ... }
    >>> q.set(':x', [p])
    k('`:x')
    >>> q.upsert(':x', p)
    k('`:x')
    >>> q.get(':x').show()
    amount  base currency
    ---------------------
    8034.79 BTC  USD
    8034.79 BTC  USD