Open-source database solution for daily data in R

This post will guide you through building up the database solution for market data from scratch. Database will daily update itself with data directly from CME. I will use R6 Parser class as interface between the database and CME settlement files. As a database I utilize PostgreSQL(PSQL) as it’s pretty fast, free, features rich, has clear documentation and is based on standard widespread SQLanguage. That was fast intro so let’s start putting pieces together.


Notice

This parser is intended to be an educational project. Use of CME settlement files for any other purposes without CME permission is forbidden. If you want to use this project in serious real world application you have to use other source of daily prices or contact CME about your intentions. Note that in practice analysts have prices coming from multiple data vendors so they can cross-check their accuracy.


PostgreSQL installation

I’m using Debian on my workstation so this walktrough deals with PSQL installation on Debian (or Debian based) machine. Even if you will install PSQL on different OS, have a quick look at the user and database name I will be using. Remember that there exist front-end clients for PSQL such as web-based phpPgAdmin or standalone pgadmin which you can use for better overview over your databases. Anyway if you are not familiar with the SQL I recommend you to glance over some short tutorial.

Fire up console, get and install PSQL server and client. Installation needs root privileges.

Installation created new system user postgres. This is superuser for all PSQL related administration. Switch to the postgres user and start the PSQL command line tool psql. Default password for postgres user is “postgres”.

In psql command line change postgres default password.

2x Ctrl-d switches you back to the bash console. Create new system user r_client. This is the user for our later R tool. Provide just password and leave other fields blank.

Create new database user.

Create new database.

 

Note that system user name and database user name are the same. If you want use different system/db names you need configure pg_ident.conf file and define custom system/db name maps.

 

Interfacing database with R

Because I want to access the database from remote machine I need to configure PostgreSQL server to allow remote connections. If you are using PSQL server and PSQL clients (R, pgAdmin) on same machine I recommend you to do following configuration as well because I noted that PSQL server doesn’t listen on defined port by default.

Locate and edit pg_hba.conf file.

Append following line(change IP and subnet according to yours):

Enable database listening on all local interfaces and default port 5432.

Find and set following directives:

Restart server.

Install and load CRAN packages¬† (I’m using RStudio IDE).

So now we have PSQL database server running and properly configured for remote connections. Setting up the R was quite straightforward.

Database design

Now with database ready we need to work out how the data scheme will actually look like. What is the structure of tables and what are relations between them. Let’s work with following simple design.

My cme_data database consists of four tables – daily price, data_vendor, symbol, exchange. Have a look at particular columns description. Note that I’m using table:column_name notation and that not all columns are described considering their names as self-explanatory.

daily_price

  • id – Serial number as primary key. (example: 1, 2, 3, 4, …)
  • data_vendor_name – Data vendor name with foreign key data_vendor:name. (example: IB, GLOBEX)
  • exchange_symbol – Instrument symbol as denoted by exchange with foreign key symbol:instrument. (example: AC)
  • vendor_symbol – Instrument symbol as denoted by data vendor. (example: EH)
  • complete_symbol – Instrument symbol with contract month and year identifier. (example: EHJ13)
  • contract_month – Contract month of the given instrument in form MMMYY. (example: APR13)

symbol

  • name – Full name of the instrument. (example: ETHANOL)
  • product_group – Sector which does the instrument belong to. (example: Energy)
  • currency – Currency which is the instrument traded in.

Such design allows symbol table for multiple OHLCV data with same date and instrument but different data vendors.

Before we start using R, clone cme_parser repository from my github and change R working directory via setwd() function to cme_parser directory. I will use sql files as input to R functions and scripts. Customize and run following R script. It creates desired tables and function triggers which automatically updates time in modified column.

Populating database

With tables ready we can start populating them. First we fill tables containing foreign keys for other tables. We can’t fill daily_price or symbol table first because some of their columns are related to other tables in other words if we want to insert data into constrained column in daily_price, PSQL will try to match our input data with data in “foreign key” table symbol which is obviously empty so it will cause error. Make sure your con connection object is still active and run following code.

The symbol table is little bit trickier to fill. Well, as you know the instrument full symbol consists of three parts – exchange symbol, contract month and contract year. So for example for March 2017 US T-Bonds we have complete symbol ZB H 17 (spaces added just for clearness). For current database purposes I chose 54 instruments from various markets. Have a look at input file with particular instruments details which we use for query construction. Note that the file has Matlab syntax. About a year ago I made this project in Matlab, so I’m using some stuff arranged before. Step through the following script and fill the symbol table.

CME data parse

With tables with foreign keys for main table – daily_price, we can finally download, parse and export data to the database. Use my Parser R6 class. If you are interested in Parser implementation check readme file in the repository.

Let’s quickly check our new fresh exported data.

Output should look something like this (created and modified columns are not shown).

Scheduling parserScript

Settlement prices are posted at approximately 6:00 p.m.  CT which is around 23:00 UTC. That means that we could schedule parserScript.r to be executed every day at 23:11 UTC say with cron tool. Before we do so we need to create and configure .Rprofile to load needed libraries every time r is started.

Paste following lines into the file and save (ctrl+o, enter, ctrl+x).

Test parserScript.r execution. (Every time you test run p$parse() and p$exportQuery() make sure you truncate daily_price table and change date in log/reportdate.log !)

If execution was successful we can add entry to crontab.

Add following line.

Congratulations! You have open-source home database solution for daily market data.

Leave a Reply