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 other 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 90 91 92 93 94 95 96 | #========================================================# # Quantitative Financial Econometrics & Derivatives # ML/DL using R, Python, Tensorflow by Sang-Heon Lee # # https://shleeai.blogspot.com #--------------------------------------------------------# # read prices of cryptocurrencies #========================================================# graphics.off(); rm(list = ls()) library(quantmod) library(stringr) # trim #------------------------------------------------- # Major cryptocurrencies, as of 2022-08-13 #------------------------------------------------- vstr_symbol <- " Symbol , 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 Symbol and make vector #------------------------------------------- df <- read.table(text = str_trim(vstr_symbol), sep = ",", header = TRUE) df <- as.data.frame(df); df df$Symbol <- str_trim(gsub("[\t\r\n,]", "", df$Symbol)) df$Name <- str_trim(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$Symbol,from=sdate,to=edate) #------------------------------------------- # collect only adjusted prices #------------------------------------------- price <- NULL for(i in 1:nc) { eval(parse(text=paste0( #"price <- cbind(price,`",df$Symbol[i],"`[,6])"))) "price <- cbind(price,`", gsub("\\^","",df$Symbol[i]),"`[,6])"))) } # modify column name as only symbol colnames(price) <- gsub(".USD.Adjusted", "", colnames(price)) # convert to data.frame with the first column as Date df.price <- cbind(time=time(price), as.data.frame(price)) rownames(df.price) <- NULL #------------------------------------------- # print time series of daily prices #------------------------------------------- head(df.price) tail(df.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.
Thank you for these very useful posts!
ReplyDeleteThank you ^^
Delete