public
Last active

  • Download Gist
quandlDataQuality.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
#######################################################
# 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=",")
quandlDataQualityREADME.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
###############################################################################
# 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
###############################################################################

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.