Skip to content

Instantly share code, notes, and snippets.

@thertrader
Last active July 25, 2017 15:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save thertrader/7483309 to your computer and use it in GitHub Desktop.
Save thertrader/7483309 to your computer and use it in GitHub Desktop.
Evaluating Quandl Data Quality
#######################################################
# Evaluating Quandl Data Quality
#
# thertrader@gmail.com - Nov 2013
#######################################################
library(quantmod)
library(Quandl)
library(Rbbg)
library(XML)
#######################################################
# STEP 1: Quandl List of Futures (web scraping with XML)
#######################################################
tables <- readHTMLTable("http://www.quandl.com/futures")
quandlFutures <- NULL
for (i in 1:length(tables)) {
quandlFutures <- rbind(quandlFutures,as.data.frame(tables[i]))
}
colnames(quandlFutures) <- c("Name","Symbol","Exchange",'Price',"Price1DayChg","MTDChg","YTDChg","Info")
quandlFuturesSymbols <- paste("OFDP/FUTURE_",as.character(sort(unique(quandlFutures[,"Symbol"]))),"1",sep="")
noFrontContractList <- c("OFDP/FUTURE_AL1",
"OFDP/FUTURE_AG1",
"OFDP/FUTURE_AU1",
"OFDP/FUTURE_CU1",
"OFDP/FUTURE_FU1",
"OFDP/FUTURE_PB1",
"OFDP/FUTURE_RB1",
"OFDP/FUTURE_WR1",
"OFDP/FUTURE_ZN1")
quandlFuturesSymbols <- sort(quandlFuturesSymbols[-match(noFrontContractList,quandlFuturesSymbols)])
#######################################################
# STEP 2: Mapping Table Bloomberg / Quandl
#######################################################
bloombergTickers <- c("AD1 Curncy",
"DNA Index",
"CO1 Comdty",
"BO1 Comdty",
"BP1 Curncy",
"BR1 Curncy",
"?",
"C 1 Comdty",
"CC1 Comdty",
"CD1 Curncy",
"ICEDCER Index",
"?",
"CL1 Comdty",
"CT1 Comdty",
"DA1 Comdty",
"DJ1 Index",
"DX1 Curncy",
"EC1 Curncy",
"ED1 Comdty",
"DL1 Comdty",
"EM1 Comdty",
"ES1 Index",
"FC1 Comdty",
"FF1 Comdty",
"FV1 Comdty",
"GC1 Comdty",
"?",
"HG1 Comdty",
"HO1 Comdty",
"?",
"?",
"JY1 Curncy",
"KC1 Comdty",
"LB1 Comdty",
"LC1 Comdty",
"LH1 Comdty",
"FN1 Comdty",
"MD1 Index",
"PEA Curncy",
"NV1 Curncy",
"NG1 Comdty",
"NX1 Index",
"NQ1 Index",
"O 1 Comdty",
"JO1 Comdty",
"PA1 Comdty",
"?",
"RM1 Index",
"RGAA Index",
"RR1 Comdty",
"RU1 Curncy",
"RVBA Index",
"S 1 Comdty",
"?",
"SB1 Comdty",
"SF1 Curncy",
"SI1 Comdty",
"SM1 Comdty",
"SP1 Index",
"RTA1 Index",
"TU1 Comdty",
"TY1 Comdty",
"WN1 Comdty",
"US1 Comdty",
"UX1 Index",
"W 1 Comdty",
"DM1 Index")
multiplier <- c(100,
1,
1,
1,
100,
100,
1,
1,
1,
100,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
100,
100,
1,
1,
0.01,
1,
1,
1,
1,
1,
1,
0.0001,
100,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
0.001,
1,
1,
1,
1,
100,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1,
1)
tickersTable <- cbind(quandlFuturesSymbols,bloombergTickers,multiplier)
colnames(tickersTable) <- c("quandl","bloomberg","multiplier")
#######################################################
# STEP 3: Data Benchmarking
#######################################################
quandlApiKey <- "your_Quandl_Api_Key" ##change this to your Quandl API Key
startDateQuandl <- "1900-01-01"
startDateBloomberg <- paste(substring(startDateQuandl,1,4),
substring(startDateQuandl,6,7),
substring(startDateQuandl,9,10),
sep="")
instrumentsInfo <- NULL
for (i in 1:nrow(tickersTable)){
if (tickersTable[i,"bloomberg"] != "?"){
# i <- 2
print(tickersTable[i,c("quandl","bloomberg")])
## Quandl
dataQuandl <- Quandl(tickersTable[i,"quandl"],
type = "xts",
sort = "asc",
start_date = startDateQuandl,
authcode = quandlApiKey)
quandlInitDate <- index(dataQuandl[1,])
quandlNobs <- nrow(dataQuandl)
## Bloomberg
# .jinit() # need to run this sometimes before dowloading Bloomberg data
dataBloomberg <- bdh(blpConnect(),
tickersTable[i,"bloomberg"],
c("PX_OPEN","PX_HIGH","PX_LOW","PX_LAST"),
startDateBloomberg)
bloombergInitDate <- rownames(dataBloomberg)[1]
bloombergNobs <- nrow(dataBloomberg)
## Checking
intersectDates <- intersect(as.character(index(dataQuandl)),as.character(rownames(dataBloomberg)))
numIntersectDates <- length(intersectDates)
notInBloomberg <- length(setdiff(as.character(index(dataQuandl)),as.character(rownames(dataBloomberg))))
notInQuandl <- length(setdiff(as.character(rownames(dataBloomberg)),as.character(index(dataQuandl))))
posInQuandl <- match(intersectDates,as.character(index(dataQuandl)))
posInBloomberg <- match(intersectDates,as.character(rownames(dataBloomberg)))
result <- cbind(round(as.numeric(tickersTable[i,"multiplier"])*dataQuandl[posInQuandl,"Settle"],6),round(dataBloomberg[posInBloomberg,"PX_LAST"],6))
mismatchClose <- length(which(result[,1] != result[,2]))
instrumentsInfo <- rbind(instrumentsInfo,cbind(tickersTable[i,"quandl"],
tickersTable[i,"bloomberg"],
as.character(quandlInitDate),
as.character(bloombergInitDate),
numIntersectDates,
notInQuandl,
notInBloomberg,
mismatchClose))
}
}
colnames(instrumentsInfo) <- c("quandlName",
"bloombergName",
"quandlStartDate",
"bloombergStartDate",
"datesIntersection",
"notInQuandl",
"notInBloomberg",
"nobsMismatch")
posNames <- match(substring(instrumentsInfo[,"quandlName"],13,nchar(instrumentsInfo[,"quandlName"])-1),
as.character(quandlFutures[,"Symbol"]))
quandlDataQuality <- cbind(as.character(quandlFutures[posNames,"Name"]),instrumentsInfo)
colnames(quandlDataQuality)[1] <- "instrument"
outputPath <- "your_path"
write.table(quandlDataQuality,paste(outputPath,"result.txt",sep=""),row.names=FALSE,sep=",")
###############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Please visit: <http://www.gnu.org/licenses/>.
###############################################################################
# Copyright (C) 2013 The R Trader
#
# For more information please visit my blog at www.thertrader.com
# or you can reach me at: TheRTrader at gmail
###############################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment