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
- 'AMZN.Open'
- 'AMZN.High'
- 'AMZN.Low'
- 'AMZN.Close'
- 'AMZN.Volume'
- '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"))
Trade Time | Last | Change | % Change | Open | High | Low | Volume | |
---|---|---|---|---|---|---|---|---|
<dttm> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <int> | |
EURUSD=X | 2021-01-11 12:21:43 | 1.2162491 | -0.006693244 | -0.5473087 | 1.2226434 | 1.2239902 | 1.2158055 | 0 |
USDJPY=X | 2021-01-11 12:22:10 | 104.2400000 | 0.291000370 | 0.2799453 | 103.9490000 | 104.2490000 | 103.6870000 | 0 |
CADUSD=X | 2021-01-11 12:22:10 | 0.7822278 | -0.006048977 | -0.7673632 | 0.7882768 | 0.7886435 | 0.7822095 | 0 |
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)
id | name | symbol | slug | is_active | rank | platform.id | platform.name | platform.symbol | platform.slug | platform.token_address | |
---|---|---|---|---|---|---|---|---|---|---|---|
<int> | <chr> | <chr> | <chr> | <int> | <int> | <int> | <chr> | <chr> | <chr> | <chr> | |
1 | 1 | Bitcoin | BTC | bitcoin | 1 | 1 | NA | NA | NA | NA | NA |
2 | 2 | Litecoin | LTC | litecoin | 1 | 5 | NA | NA | NA | NA | NA |
3 | 3 | Namecoin | NMC | namecoin | 1 | 310 | NA | NA | NA | NA | NA |
4 | 4 | Terracoin | TRC | terracoin | 1 | 926 | NA | NA | NA | NA | NA |
5 | 5 | Peercoin | PPC | peercoin | 1 | 346 | NA | NA | NA | NA | NA |
6 | 6 | Novacoin | NVC | novacoin | 1 | 842 | NA | NA | NA | NA | NA |
[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)
id | name | symbol | slug | num_market_pairs | date_added | tags | max_supply | circulating_supply | total_supply | ⋯ | platform.symbol | platform.slug | platform.token_address | quote.EUR.price | quote.EUR.volume_24h | quote.EUR.percent_change_1h | quote.EUR.percent_change_24h | quote.EUR.percent_change_7d | quote.EUR.market_cap | quote.EUR.last_updated | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
<int> | <chr> | <chr> | <chr> | <int> | <chr> | <list> | <dbl> | <dbl> | <dbl> | ⋯ | <chr> | <chr> | <chr> | <dbl> | <dbl> | <lgl> | <lgl> | <lgl> | <dbl> | <chr> | |
1 | 1 | Bitcoin | BTC | bitcoin | 7919 | 2013-04-28T00:00:00.000Z | mineable | 2.1e+07 | 17906012 | 17906012 | ⋯ | NA | NA | NA | 8708.4427303 | 12507935648 | NA | NA | NA | 155933480030 | 2019-08-30T18:51:01.000Z |
2 | 1027 | Ethereum | ETH | ethereum | 5629 | 2015-08-07T00:00:00.000Z | mineable | NA | 107537936 | 107537936 | ⋯ | NA | NA | NA | 153.6859725 | 5260772807 | NA | NA | NA | 16527072336 | 2019-08-30T18:51:01.000Z |
3 | 52 | XRP | XRP | ripple | 449 | 2013-08-04T00:00:00.000Z | 1.0e+11 | 42932866967 | 99991366793 | ⋯ | NA | NA | NA | 0.2318186 | 844359719 | NA | NA | NA | 9952638454 | 2019-08-30T18:51:01.000Z | |
4 | 1831 | Bitcoin Cash | BCH | bitcoin-cash | 378 | 2017-07-23T00:00:00.000Z | mineable | 2.1e+07 | 17975975 | 17975975 | ⋯ | NA | NA | NA | 256.0351264 | 1281819319 | NA | NA | NA | 4602481031 | 2019-08-30T18:51:01.000Z |
5 | 2 | Litecoin | LTC | litecoin | 538 | 2013-04-28T00:00:00.000Z | mineable | 8.4e+07 | 63147124 | 63147124 | ⋯ | NA | NA | NA | 58.6260748 | 2207389317 | NA | NA | NA | 3702068016 | 2019-08-30T18:51:01.000Z |
6 | 825 | Tether | USDT | tether | 3016 | 2015-02-25T00:00:00.000Z | NA | 4008269411 | 4095057493 | ⋯ | OMNI | omni | 31 | 0.9138714 | 14536357084 | NA | NA | NA | 3663042762 | 2019-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 timestamp quote.EUR.timestamp <chr> <chr> 1 2020-05-21T12:19:03.000Z 2020-05-21T12:19:03.000Z 2 2020-05-21T12:24:02.000Z 2020-05-21T12:24:02.000Z 3 2020-05-21T12:29:03.000Z 2020-05-21T12:29:03.000Z 4 2020-05-21T12:34:03.000Z 2020-05-21T12:34:03.000Z 5 2020-05-21T12:39:01.000Z 2020-05-21T12:39:01.000Z 6 2020-05-21T12:44:02.000Z 2020-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
Date | Open | High | Low | Last | Change | Settle | Volume | Previous Day Open Interest | |
---|---|---|---|---|---|---|---|---|---|
<date> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | <dbl> | |
1 | 2021-01-08 | 50.925 | 52.750 | 50.825 | 52.750 | 1.41 | 52.24 | 10770 | 1833 |
2 | 2021-01-07 | 50.525 | 51.275 | 50.400 | 50.950 | 0.20 | 50.83 | 10814 | 1800 |
3 | 2021-01-06 | 49.825 | 50.925 | 49.475 | 50.525 | 0.70 | 50.63 | 16675 | 1908 |
4 | 2021-01-05 | 47.400 | 50.200 | 47.275 | 49.850 | 2.31 | 49.93 | 23473 | 1799 |
5 | 2021-01-04 | 48.400 | 49.850 | 47.200 | 47.350 | -0.90 | 47.62 | 21845 | 1350 |
6 | 2020-12-31 | 48.325 | 48.575 | 47.775 | 48.450 | 0.12 | 48.52 | 6698 | 1446 |
[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
- 50.925
- 50.525
- 49.825
- 47.4
- 48.4
- 48.325
- 48.125
- 47.7
- 48.2
- 48.1
- 46.775
- 47.85
- 49.3
- 48.35
- 47.825
- 47.55
- 47.05
- 46.65
- 47
- 45.725
- 45.55
- 45.6
- 46.125
- 45.625
- 45
- 44.375
- 45.05
- 45.45
- 45.9
- 44.8
- 42.825
- 42.55
- 41.85
- 41.6
- 41.325
- 41.525
- 40.175
- 40.9
- 41.425
- 41.8
- 39.825
- 37.325
- 38.5
- 39.1
- 38.15
- 37.025
- 35.325
- 36.1
- 37.475
- 38.95
- 38.625
- 39.75
- 40.6
- 40.025
- 41.25
- 40.95
- 40.9
- 40.875
- 41.15
- 40.225
- 39.525
- 40.45
- 41.325
- 39.975
- 39.875
- 39.375
- 37
- 38.525
- 39.85
- 39.225
- 40.575
- 40.125
- 40.175
- 39.6
- 39.725
- 39.875
- 40.85
- 40.95
- 40.175
- 38.325
- 37.3
- 37.325
- 37.1
- 37.8
- 36.85
- 39.45
- 41.25
- 41.575
- 43.05
- 42.825
- 42.925
- 43
- 43.475
- 43.375
- 42.375
- 42.425
- 42.75
- 42.95
- 42.65
- 42.65
- 42.15
- 42.325
- 42.55
- 41.575
- 42
- 41.55
- 41.925
- 42.225
- 41.525
- 40.775
- 40.375
- 40.4
- 41.325
- 41.1
- 41.675
- 41.325
- 41.1
- 41.925
- 41.55
- 40.8
- 40.45
- 40.75
- 40.95
- 40.55
- 39.625
- 40.375
- 39.6
- 40.825
- 40.45
- 40.6
- 40.375
- 39.775
- 39.825
- 39.625
- 37.95
- 39.075
- 38.05
- 39.975
- 40.65
- 39.05
- 38.875
- 37.625
- 37.95
- 37.1
- 35.975
- 36.25
- 39.05
- 38.425
- 38.2
- 39.4
- 37.325
- 36.725
- 36.825
- 35.525
- 35.075
- 33.675
- 32.025
- 34.1
- 33.3
- 33.95
- 33.5
- 31.9
- 32.35
- 30
- 27.575
- 25.7
- 25.35
- 24.575
- 24.425
- 23.5
- 24
- 25.5
- 21.15
- 19.1
- 19.075
- 15.6
- 13.275
- 13
- 16.875
- 16.825
- 14.3
- 13
- 21.3
- 17.75
- 20.025
- 20.125
- 20.7
- 22.4
- 24.55
- 26.25
- 24.25
- 26.35
- 26.5
- 24.8
- 21.225
- 20.1
- 20.25
- 21
- 23.3
- 24.15
- ⋯
- 56.1
- 55.375
- 55.325
- 57.25
- 59.225
- 63.35
- 63
- 65.525
- 66.725
- 67.4
- 67.6
- 69.25
- 66.125
- 73.5
- 73.85
- 75.5
- 76.575
- 76.325
- 74.325
- 74.225
- 75.525
- 75.95
- 74.3
- 76.9
- 77.5
- 77.275
- 78.475
- 77.9
- 78.9
- 77.4
- 78.175
- 80.6
- 81
- 81.925
- 81.575
- 80.65
- 81.225
- 81.925
- 80.4
- 82.525
- 81.85
- 82.4
- 83.175
- 80.95
- 82.3
- 85.025
- 85.3
- 84.4
- 87.7
- 88.45
- 90.5
- 89.75
- 91.35
- 90.725
- 91.375
- 94.3
- 93.3
- 92.525
- 92.9
- 91.65
- 90.7
- 91.625
- 91.95
- 94
- 94.725
- 92.75
- 92.125
- 93.05
- 91.7
- 92.775
- 93.15
- 93.45
- 94.525
- 95.1
- 93.225
- 95.875
- 94.575
- 93.725
- 93.875
- 93.35
- 93.375
- 93.9
- 93.475
- 92.85
- 93.925
- 97.2
- 95.525
- 97.35
- 97.175
- 97.85
- 97.45
- 97.6
- 96.9
- 97.6
- 98.4
- 97.675
- 97.65
- 99.525
- 101
- 101.575
- 101.875
- 102.05
- 103.2
- 102
- 102.75
- 101.725
- 103.75
- 101.475
- 100.2
- 100.95
- 100.525
- 102.85
- 101.95
- 103.5
- 103.4
- 103.8
- 104.275
- 105.225
- 105.55
- 105.775
- 105.65
- 106.7
- 106.025
- 106
- 106.9
- 106.075
- 105.7
- 106.625
- 106.575
- 106.9
- 106.825
- 104.5
- 104.325
- 104.5
- 102.7
- 102.425
- 102.375
- 102.775
- 102.45
- 102.875
- 103.45
- 103.075
- 104.15
- 104.375
- 103.75
- 103.825
- 102.925
- 102.1
- 101.6
- 101.575
- 102
- 101.925
- 100.6
- 100.025
- 100.25
- 100.775
- 99.825
- 99.425
- 99.95
- 99.2
- 99.7
- 100.775
- 100.85
- 100.25
- 101.925
- 101.5
- 101.9
- 103.625
- 103.65
- 103.825
- 103.825
- 103.6
- 103.65
- 103.35
- 103.4
- 102.325
- 100.7
- 101
- 100.4
- 99.3
- 99.675
- 101.5
- 101.575
- 101.325
- 100.275
- 99.2
- 99.425
- 99.45
- 98.675
- 99.15
- 98.575
- 98.025
- 99.35
- 98.2
- 98.1
- 99.5
- 100.925
- 101.9
- 101
- 103.325