Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Kdb+ Queries via R

Kdb+ is a commercial database used in high-frequency trading. The database is accessed via the proprietary programming language Q.

When working with data in R, queries written in Q can be passed to the database, with the results stored as R objects. This requires the use of the R package rkdb.

For software projects, it is good practice to use a tool to manage packages and dependencies. For the purpose of this example, we will use the conda command, which ships with installations of both Anaconda and Miniconda.1

We will use the tool to build an environment that includes all the dependencies necessary to send queries in Q via R, all while working in a Jupyter notebook.

Setup

macOS

  1. Make sure your OS is completely up-to-date, then install the Apple Developer Command Line Tools by running the following command in the Terminal app.
     xcode-select --install
    
  2. Create a conda environment that includes Jupyter and R, as well as the R package devtools. For this example, we will name our environment kdbrtest, but you may name it anything you wish.
     conda create -y --name kdbrtest jupyter r-irkernel r-devtools --channel conda-forge
    
  3. Load the environment you just created.
    conda activate kdbrtest
    
  4. Install the rkdb package. For installation, we use R and the devtools package to download and compile the code hosted in the rkdb repository.
     Rscript -e "devtools::install_github('kxsystems/rkdb')"
    

Now, each time you want to work with R and the rkdb package, activate the kdbrtest environment via the Terminal app with the command conda activate kdbrtest and then type jupyter notebook.

Windows

  1. Open the Anaconda Prompt app and create a conda environment that includes Jupyter and R, as well as the R package devtools. For this example, we will name our environment kdbrtest, but you may name it anything you wish.
     conda create -y --name kdbrtest jupyter r-irkernel r-devtools r-installr --channel conda-forge
    
  2. Load the environment you just created.
    conda activate kdbrtest
    
  3. Open Jupyter to further prepare the environment.
     jupyter notebook
    
  4. Use the R package installr to help install RTools, which allows for building packages like rkdb from source. You’ll be prompted to complete the installation graphically after running these commands in a cell inside a new notebook.2
     library(installr)
     install.Rtools(check=FALSE,check_r_update=FALSE,GUI=FALSE)
    
  5. Complete the installation of rkdb by calling devtools from another cell after the Rtools installation is complete.
     devtools::install_github('kxsystems/rkdb')
    

    Now, each time you want to work with R and the rkdb package, activate the kdbrtest environment via the Anaconda Prompt app with the command conda activate kdbrtest and then type jupyter notebook.

  1. If you haven’t installed Anaconda yet, go with Miniconda instead. 

  2. Accept all the graphical defaults unless you know you have an existing Rtools installation.