Historical data

This page contains some samples for downloading historical data of your quant trading system.

Quantmod library

Stock markets from Yahoo finance by Quantmod library (doc)

[1]:
library(quantmod)
symbol <- "AMZN"
getSymbols(symbol) # or getSymbols(symbol, src = "yahoo")
Loading required package: xts

Loading required package: zoo


Attaching package: ‘zoo’


The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric


Loading required package: TTR

Registered S3 method overwritten by 'quantmod':
  method            from
  as.zoo.data.frame zoo

‘getSymbols’ currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
‘loadSymbols’ to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be checked for
alternate defaults.

This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbols for details.


'AMZN'
[2]:
head(AMZN) # returns some data of AMZN vector
           AMZN.Open AMZN.High AMZN.Low AMZN.Close AMZN.Volume AMZN.Adjusted
2007-01-03     38.68     39.06    38.05      38.70    12405100         38.70
2007-01-04     38.59     39.14    38.26      38.90     6318400         38.90
2007-01-05     38.72     38.79    37.60      38.37     6619700         38.37
2007-01-08     38.22     38.31    37.17      37.50     6783000         37.50
2007-01-09     37.60     38.06    37.34      37.78     5703000         37.78
2007-01-10     37.49     37.70    37.07      37.15     6527500         37.15
[3]:
colnames(AMZN) # returns column names of AMZN vector
  1. 'AMZN.Open'
  2. 'AMZN.High'
  3. 'AMZN.Low'
  4. 'AMZN.Close'
  5. 'AMZN.Volume'
  6. 'AMZN.Adjusted'
[4]:
#Op(AMZN) # only column of open of all data
#Hi(AMZN) # only column of high of all data
#Lo(AMZN) # only column of low of all data
#Cl(AMZN) # only column of close of all data
#Vo(AMZN) # only column of volume of all data
#Ad(AMZN) # value adjusted of the close (of all data)
#OHLC(AMZN) # columns open, high, low and close of all data

Exchange rates (forex) from Yahoo finance

[5]:
library(quantmod)
cross_from <- c("EUR", "USD", "CAD")
cross_to <- c("USD", "JPY", "USD")
getQuote(paste0(cross_from, cross_to, "=X"))
A data.frame: 3 × 8
Trade TimeLastChange% ChangeOpenHighLowVolume
<dttm><dbl><dbl><dbl><dbl><dbl><dbl><int>
EURUSD=X2021-01-11 12:21:43 1.2162491-0.006693244-0.5473087 1.2226434 1.2239902 1.21580550
USDJPY=X2021-01-11 12:22:10104.2400000 0.291000370 0.2799453103.9490000104.2490000103.68700000
CADUSD=X2021-01-11 12:22:10 0.7822278-0.006048977-0.7673632 0.7882768 0.7886435 0.78220950

Exchange rates (forex) from Oanda

[6]:
library(quantmod)
currency <- "EUR/GBP"
# the last 6 months
getSymbols(currency, src = "oanda")
head(EURGBP) # returns some data of EURGBP vector
'EUR/GBP'
            EUR.GBP
2020-07-16 0.907685
2020-07-17 0.909031
2020-07-18 0.909320
2020-07-19 0.909368
2020-07-20 0.907640
2020-07-21 0.903154
[7]:
# the last 60 days
getSymbols(currency, from = Sys.Date() - 60, to = Sys.Date(), src = "oanda")
head(EURGBP) # returns some data of EURGBP vector
'EUR/GBP'
            EUR.GBP
2020-11-12 0.895798
2020-11-13 0.898187
2020-11-14 0.896580
2020-11-15 0.896644
2020-11-16 0.897208
2020-11-17 0.896298

CoinMarketCap API

Crypto currencies by coinmarketcap (doc) and coindeskr (doc) Many libraries are not supported for a long period or it may no longer be supported because it has violated a policy:

  • the library coindeskr has violated the CRAN’s policy and it has archived on 2020-09-04

  • its GitHub repository is already available, but it has not updated for a change of CoinMarketCap API

[8]:
library(coinmarketcapr)
library(coindeskr)
last_marketcap <- get_global_marketcap("EUR")
today_marketcap <- get_marketcap_ticker_all()
head(today_marketcap[,1:8])
last_month <- get_last31days_price() # default it is Bitcoin
current_price <- get_current_price() # default it is Bitcoin
⚠ The old API is used when no 'apikey' is given.
Error: lexical error: invalid char in json text.
                                       <!DOCTYPE HTML> <html lang="en-
                     (right here) ------^

Traceback:

1. get_global_marketcap("EUR")
2. check_response(d)
3. stop(cat(crayon::red(cli::symbol$cross, "The request was not succesfull! \n",
 .     "Request URL:\n", req$url, "\n", "Response Content:\n", jsonlite::prettify(rawToChar(req$content)),
 .     "\n")))
4. cat(crayon::red(cli::symbol$cross, "The request was not succesfull! \n",
 .     "Request URL:\n", req$url, "\n", "Response Content:\n", jsonlite::prettify(rawToChar(req$content)),
 .     "\n"))
5. crayon::red(cli::symbol$cross, "The request was not succesfull! \n",
 .     "Request URL:\n", req$url, "\n", "Response Content:\n", jsonlite::prettify(rawToChar(req$content)),
 .     "\n")
6. mypaste(...)
7. lapply(list(...), as.character)
8. jsonlite::prettify(rawToChar(req$content))
9. reformat(txt, TRUE, indent_string = paste(rep(" ", as.integer(indent)),
 .     collapse = ""))
10. stop(out[[2]], call. = FALSE)
11. base::stop(..., call. = FALSE)

Below you can find a sample with sandbox API of CoinMarketCap.

[9]:
library(httr)
library(jsonlite)
base_url <- 'https://sandbox-api.coinmarketcap.com/'

get_coinmarketcap_data <- function(base_url, path, query) {
    response <- GET(url = base_url, path = paste('v1/', path, sep = ''), query = query)
    content <- content(response, 'text', encoding = 'utf-8')
    return(fromJSON(content, flatten = TRUE))
}

# returns all active cryptocurrencies
path <- 'cryptocurrency/map'
marketcap_map <- get_coinmarketcap_data(base_url, path, list())
head(marketcap_map$data)
A data.frame: 6 × 11
idnamesymbolslugis_activerankplatform.idplatform.nameplatform.symbolplatform.slugplatform.token_address
<int><chr><chr><chr><int><int><int><chr><chr><chr><chr>
11Bitcoin BTCbitcoin 1 1NANANANANA
22Litecoin LTClitecoin 1 5NANANANANA
33Namecoin NMCnamecoin 1310NANANANANA
44TerracoinTRCterracoin1926NANANANANA
55Peercoin PPCpeercoin 1346NANANANANA
66Novacoin NVCnovacoin 1842NANANANANA
[10]:
# returns last market data of all active cryptocurrencies
path <- 'cryptocurrency/listings/latest'
query <- list(convert = 'EUR')
last_marketcap <- get_coinmarketcap_data(base_url, path, query)
head(last_marketcap$data)
A data.frame: 6 × 24
idnamesymbolslugnum_market_pairsdate_addedtagsmax_supplycirculating_supplytotal_supplyplatform.symbolplatform.slugplatform.token_addressquote.EUR.pricequote.EUR.volume_24hquote.EUR.percent_change_1hquote.EUR.percent_change_24hquote.EUR.percent_change_7dquote.EUR.market_capquote.EUR.last_updated
<int><chr><chr><chr><int><chr><list><dbl><dbl><dbl><chr><chr><chr><dbl><dbl><lgl><lgl><lgl><dbl><chr>
1 1Bitcoin BTC bitcoin 79192013-04-28T00:00:00.000Zmineable2.1e+07 17906012 17906012NA NA NA8708.442730312507935648NANANA1559334800302019-08-30T18:51:01.000Z
21027Ethereum ETH ethereum 56292015-08-07T00:00:00.000Zmineable NA 107537936 107537936NA NA NA 153.6859725 5260772807NANANA 165270723362019-08-30T18:51:01.000Z
3 52XRP XRP ripple 4492013-08-04T00:00:00.000Z1.0e+114293286696799991366793NA NA NA 0.2318186 844359719NANANA 99526384542019-08-30T18:51:01.000Z
41831Bitcoin CashBCH bitcoin-cash 3782017-07-23T00:00:00.000Zmineable2.1e+07 17975975 17975975NA NA NA 256.0351264 1281819319NANANA 46024810312019-08-30T18:51:01.000Z
5 2Litecoin LTC litecoin 5382013-04-28T00:00:00.000Zmineable8.4e+07 63147124 63147124NA NA NA 58.6260748 2207389317NANANA 37020680162019-08-30T18:51:01.000Z
6 825Tether USDTtether 30162015-02-25T00:00:00.000Z NA 4008269411 4095057493OMNIomni31 0.913871414536357084NANANA 36630427622019-08-30T18:51:01.000Z
[11]:
# returns last market data of Bitcoin
path <- 'cryptocurrency/quotes/latest'
query <- list(id = 1, convert = 'EUR')
last_bitcoin <- get_coinmarketcap_data(base_url, path, query)
last_bitcoin$data$`1`
last_bitcoin$data$`1`$`quote`$EUR
$id
1
$name
'Bitcoin'
$symbol
'BTC'
$slug
'bitcoin'
$num_market_pairs
7919
$date_added
'2013-04-28T00:00:00.000Z'
$tags
'mineable'
$max_supply
21000000
$circulating_supply
17906012
$total_supply
17906012
$is_active
1
$is_market_cap_included_in_calc
1
$platform
NULL
$cmc_rank
1
$is_fiat
0
$last_updated
'2019-08-30T18:51:28.000Z'
$quote
$EUR =
$price
8708.44273026964
$volume_24h
12507935648.1974
$percent_change_1h
NULL
$percent_change_24h
NULL
$percent_change_7d
NULL
$market_cap
155933480029.521
$last_updated
'2019-08-30T18:51:01.000Z'
$price
8708.44273026964
$volume_24h
12507935648.1974
$percent_change_1h
NULL
$percent_change_24h
NULL
$percent_change_7d
NULL
$market_cap
155933480029.521
$last_updated
'2019-08-30T18:51:01.000Z'
[12]:
# returns historical data from a time_start to a time_end
path <- 'cryptocurrency/quotes/historical'
query <- list(id = 1, convert = 'EUR', time_start = '2020-05-21T12:14', time_end = '2020-05-21T12:44')
data <- get_coinmarketcap_data(base_url, path, query)
head(data$data)
$id
1
$name
'Bitcoin'
$symbol
'BTC'
$is_fiat
0
$quotes
A data.frame: 6 × 2
timestampquote.EUR.timestamp
<chr><chr>
12020-05-21T12:19:03.000Z2020-05-21T12:19:03.000Z
22020-05-21T12:24:02.000Z2020-05-21T12:24:02.000Z
32020-05-21T12:29:03.000Z2020-05-21T12:29:03.000Z
42020-05-21T12:34:03.000Z2020-05-21T12:34:03.000Z
52020-05-21T12:39:01.000Z2020-05-21T12:39:01.000Z
62020-05-21T12:44:02.000Z2020-05-21T12:44:02.000Z

Quandl library

OHLC (Open High Low Close) data by Quandl library (doc), for example crude oil

[13]:
library(Quandl)
oil_data <- Quandl(code = "CHRIS/CME_QM1")
head(oil_data) # returns all free columns names
A data.frame: 6 × 9
DateOpenHighLowLastChangeSettleVolumePrevious Day Open Interest
<date><dbl><dbl><dbl><dbl><dbl><dbl><dbl><dbl>
12021-01-0850.92552.75050.82552.750 1.4152.24107701833
22021-01-0750.52551.27550.40050.950 0.2050.83108141800
32021-01-0649.82550.92549.47550.525 0.7050.63166751908
42021-01-0547.40050.20047.27549.850 2.3149.93234731799
52021-01-0448.40049.85047.20047.350-0.9047.62218451350
62020-12-3148.32548.57547.77548.450 0.1248.52 66981446
[14]:
#getPrice(oil_data) # returns all free columns values
#getPrice(oil_data, symbol = "CHRIS/CME_QM1", prefer = "Open$") # returns open values of a specific symbol
getPrice(oil_data, prefer = "Open$") # returns open values
  1. 50.925
  2. 50.525
  3. 49.825
  4. 47.4
  5. 48.4
  6. 48.325
  7. 48.125
  8. 47.7
  9. 48.2
  10. 48.1
  11. 46.775
  12. 47.85
  13. 49.3
  14. 48.35
  15. 47.825
  16. 47.55
  17. 47.05
  18. 46.65
  19. 47
  20. 45.725
  21. 45.55
  22. 45.6
  23. 46.125
  24. 45.625
  25. 45
  26. 44.375
  27. 45.05
  28. 45.45
  29. 45.9
  30. 44.8
  31. 42.825
  32. 42.55
  33. 41.85
  34. 41.6
  35. 41.325
  36. 41.525
  37. 40.175
  38. 40.9
  39. 41.425
  40. 41.8
  41. 39.825
  42. 37.325
  43. 38.5
  44. 39.1
  45. 38.15
  46. 37.025
  47. 35.325
  48. 36.1
  49. 37.475
  50. 38.95
  51. 38.625
  52. 39.75
  53. 40.6
  54. 40.025
  55. 41.25
  56. 40.95
  57. 40.9
  58. 40.875
  59. 41.15
  60. 40.225
  61. 39.525
  62. 40.45
  63. 41.325
  64. 39.975
  65. 39.875
  66. 39.375
  67. 37
  68. 38.525
  69. 39.85
  70. 39.225
  71. 40.575
  72. 40.125
  73. 40.175
  74. 39.6
  75. 39.725
  76. 39.875
  77. 40.85
  78. 40.95
  79. 40.175
  80. 38.325
  81. 37.3
  82. 37.325
  83. 37.1
  84. 37.8
  85. 36.85
  86. 39.45
  87. 41.25
  88. 41.575
  89. 43.05
  90. 42.825
  91. 42.925
  92. 43
  93. 43.475
  94. 43.375
  95. 42.375
  96. 42.425
  97. 42.75
  98. 42.95
  99. 42.65
  100. 42.65
  101. 42.15
  102. 42.325
  103. 42.55
  104. 41.575
  105. 42
  106. 41.55
  107. 41.925
  108. 42.225
  109. 41.525
  110. 40.775
  111. 40.375
  112. 40.4
  113. 41.325
  114. 41.1
  115. 41.675
  116. 41.325
  117. 41.1
  118. 41.925
  119. 41.55
  120. 40.8
  121. 40.45
  122. 40.75
  123. 40.95
  124. 40.55
  125. 39.625
  126. 40.375
  127. 39.6
  128. 40.825
  129. 40.45
  130. 40.6
  131. 40.375
  132. 39.775
  133. 39.825
  134. 39.625
  135. 37.95
  136. 39.075
  137. 38.05
  138. 39.975
  139. 40.65
  140. 39.05
  141. 38.875
  142. 37.625
  143. 37.95
  144. 37.1
  145. 35.975
  146. 36.25
  147. 39.05
  148. 38.425
  149. 38.2
  150. 39.4
  151. 37.325
  152. 36.725
  153. 36.825
  154. 35.525
  155. 35.075
  156. 33.675
  157. 32.025
  158. 34.1
  159. 33.3
  160. 33.95
  161. 33.5
  162. 31.9
  163. 32.35
  164. 30
  165. 27.575
  166. 25.7
  167. 25.35
  168. 24.575
  169. 24.425
  170. 23.5
  171. 24
  172. 25.5
  173. 21.15
  174. 19.1
  175. 19.075
  176. 15.6
  177. 13.275
  178. 13
  179. 16.875
  180. 16.825
  181. 14.3
  182. 13
  183. 21.3
  184. 17.75
  185. 20.025
  186. 20.125
  187. 20.7
  188. 22.4
  189. 24.55
  190. 26.25
  191. 24.25
  192. 26.35
  193. 26.5
  194. 24.8
  195. 21.225
  196. 20.1
  197. 20.25
  198. 21
  199. 23.3
  200. 24.15
  201. 56.1
  202. 55.375
  203. 55.325
  204. 57.25
  205. 59.225
  206. 63.35
  207. 63
  208. 65.525
  209. 66.725
  210. 67.4
  211. 67.6
  212. 69.25
  213. 66.125
  214. 73.5
  215. 73.85
  216. 75.5
  217. 76.575
  218. 76.325
  219. 74.325
  220. 74.225
  221. 75.525
  222. 75.95
  223. 74.3
  224. 76.9
  225. 77.5
  226. 77.275
  227. 78.475
  228. 77.9
  229. 78.9
  230. 77.4
  231. 78.175
  232. 80.6
  233. 81
  234. 81.925
  235. 81.575
  236. 80.65
  237. 81.225
  238. 81.925
  239. 80.4
  240. 82.525
  241. 81.85
  242. 82.4
  243. 83.175
  244. 80.95
  245. 82.3
  246. 85.025
  247. 85.3
  248. 84.4
  249. 87.7
  250. 88.45
  251. 90.5
  252. 89.75
  253. 91.35
  254. 90.725
  255. 91.375
  256. 94.3
  257. 93.3
  258. 92.525
  259. 92.9
  260. 91.65
  261. 90.7
  262. 91.625
  263. 91.95
  264. 94
  265. 94.725
  266. 92.75
  267. 92.125
  268. 93.05
  269. 91.7
  270. 92.775
  271. 93.15
  272. 93.45
  273. 94.525
  274. 95.1
  275. 93.225
  276. 95.875
  277. 94.575
  278. 93.725
  279. 93.875
  280. 93.35
  281. 93.375
  282. 93.9
  283. 93.475
  284. 92.85
  285. 93.925
  286. 97.2
  287. 95.525
  288. 97.35
  289. 97.175
  290. 97.85
  291. 97.45
  292. 97.6
  293. 96.9
  294. 97.6
  295. 98.4
  296. 97.675
  297. 97.65
  298. 99.525
  299. 101
  300. 101.575
  301. 101.875
  302. 102.05
  303. 103.2
  304. 102
  305. 102.75
  306. 101.725
  307. 103.75
  308. 101.475
  309. 100.2
  310. 100.95
  311. 100.525
  312. 102.85
  313. 101.95
  314. 103.5
  315. 103.4
  316. 103.8
  317. 104.275
  318. 105.225
  319. 105.55
  320. 105.775
  321. 105.65
  322. 106.7
  323. 106.025
  324. 106
  325. 106.9
  326. 106.075
  327. 105.7
  328. 106.625
  329. 106.575
  330. 106.9
  331. 106.825
  332. 104.5
  333. 104.325
  334. 104.5
  335. 102.7
  336. 102.425
  337. 102.375
  338. 102.775
  339. 102.45
  340. 102.875
  341. 103.45
  342. 103.075
  343. 104.15
  344. 104.375
  345. 103.75
  346. 103.825
  347. 102.925
  348. 102.1
  349. 101.6
  350. 101.575
  351. 102
  352. 101.925
  353. 100.6
  354. 100.025
  355. 100.25
  356. 100.775
  357. 99.825
  358. 99.425
  359. 99.95
  360. 99.2
  361. 99.7
  362. 100.775
  363. 100.85
  364. 100.25
  365. 101.925
  366. 101.5
  367. 101.9
  368. 103.625
  369. 103.65
  370. 103.825
  371. 103.825
  372. 103.6
  373. 103.65
  374. 103.35
  375. 103.4
  376. 102.325
  377. 100.7
  378. 101
  379. 100.4
  380. 99.3
  381. 99.675
  382. 101.5
  383. 101.575
  384. 101.325
  385. 100.275
  386. 99.2
  387. 99.425
  388. 99.45
  389. 98.675
  390. 99.15
  391. 98.575
  392. 98.025
  393. 99.35
  394. 98.2
  395. 98.1
  396. 99.5
  397. 100.925
  398. 101.9
  399. 101
  400. 103.325