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
- 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
- Create a conda environment that includes Jupyter and R, as well as the R package
devtools
. For this example, we will name our environmentkdbrtest
, but you may name it anything you wish.conda create -y --name kdbrtest jupyter r-irkernel r-devtools --channel conda-forge
- Load the environment you just created.
conda activate kdbrtest
- Install the
rkdb
package. For installation, we use R and thedevtools
package to download and compile the code hosted in therkdb
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
- 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 environmentkdbrtest
, but you may name it anything you wish.conda create -y --name kdbrtest jupyter r-irkernel r-devtools r-installr --channel conda-forge
- Load the environment you just created.
conda activate kdbrtest
- Open Jupyter to further prepare the environment.
jupyter notebook
- Use the R package
installr
to help install RTools, which allows for building packages likerkdb
from source. You’ll be prompted to complete the installation graphically after running these commands in a cell inside a new notebook.2library(installr) install.Rtools(check=FALSE,check_r_update=FALSE,GUI=FALSE)
- 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 thekdbrtest
environment via the Anaconda Prompt app with the commandconda activate kdbrtest
and then typejupyter notebook
.