R code snippet : Read Historical Prices of Cryptocurrencies
[This article was first published on SH Fintech Modeling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
This post shows how to read prices of cryptocurrencies given symbols as a string. Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Read historical prices of cryptocurrencies
Pairs trading also can be applied to cryptocurrencies. It is, therefore, a starting point of pairs trading backtest to collect daily prices of them. I collected the symbols of major cryptocurrencies at
https://finance.yahoo.com/cryptocurrencies/
Some limitations : data lengths
It is worth noting that the quantmod R package we used in this work does not provide the full or longer history of crypto prices. For example the first historical data of BTC begins at 2014-09-17 and ETH at 2017-11-09. The available sample periods of some coins are similar to or less than that of ETH.
R code
The following R code retrieves historical daily prices of major cryptocurrencies given their symbols as of 2022-08-13.
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 | #========================================================# # Quantitative ALM, Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee # # https://kiandlee.blogspot.com #——————————————————–# # read prices of cryptocurrencies #========================================================# graphics.off(); rm(list = ls()) library(quantmod) #————————————————- # Major cryptocurrencies, as of 2022-08-13 #————————————————- vstr_crypto <– “ code , name BTC-USD , Bitcoin USD ETH-USD , Ethereum USD USDT-USD , Tether USD USDC-USD , USD Coin USD BNB-USD , Binance Coin USD ADA-USD , Cardano USD XRP-USD , XRP USD BUSD-USD , Binance USD USD SOL-USD , Solana USD HEX-USD , HEX USD DOT-USD , Polkadot USD DOGE-USD , Dogecoin USD AVAX-USD , Avalanche USD MATIC-USD , Polygon USD DAI-USD , Dai USD WTRX-USD , Wrapped TRON USD SHIB-USD , SHIBA INU USD STETH-USD , Lido stETH USD UNI1-USD , Uniswap USD TRX-USD , TRON USD ETC-USD , Ethereum Classic USD WBTC-USD , Wrapped Bitcoin USD LEO-USD , UNUS SED LEO USD LTC-USD , Litecoin USD NEAR-USD , NEAR-USD “ #——————————————- # split symbols and make vector #——————————————- df <– read.table(text = vstr_crypto, sep = “,”, header = TRUE) df <– as.data.frame(df) df$code <– gsub(“[\t\r\n ,]”,“”,df$code) df$name <– gsub(“[\t\r\n ,]”,“”,df$name) df nc <– nrow(df) # number of crypto #——————————————- # read price information #——————————————- # limitation of data length # BTC : from 2014-09-17 # ETH and some coins : from 2017-11-09 # others : short period sdate <– as.Date(“2017-11-09”) edate <– as.Date(“2022-08-12”) getSymbols(df$code,from=sdate,to=edate) #——————————————- # collect only adjusted prices #——————————————- price <– NULL for(i in 1:nc) { eval(parse(text=paste0( “price <- cbind(price,`", df$code[i],“`[,6])”))) } # modify column name as only symbol colnames(price) <– gsub(“.USD.Adjusted”, “”, colnames(price)) #——————————————- # print time series of daily prices #——————————————- head(price) tail(price) | cs |
Running the above R code displays the status of data reading process as follows.
Finally, we can get the collection of individual cryptocurrency prices.
To leave a comment for the author, please follow the link and comment on their blog: SH Fintech Modeling.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.