Load intraday data into R for processing with quantmod

I need to modify this sample code to use it with intraday data, which I should get from here and here . As far as I understand, the code in this example works well with any historical data (or not?), So my problem boils down to the issue of loading the source data in the required format (I mean daily or intraday).

As I also understand from the answers to this question , it is not possible to load intraday data using getSymbols() . I tried to download this data to my hard drive and get it using the read.csv() function, but this approach did not work. Finally, I found several solutions to this problem in various articles (for example, here ), but they all seem very complex and "artificial",

So my question is, how do I load intraday data into a given code gracefully and correctly from a programmer’s point of view without reinventing the wheel?

PS I am very new to time series analysis in R and quantstratum, so if my question seems unclear, let me know what you need to know in order to answer it.

+5
source share
1 answer

I don’t know how to do this without “reinventing the wheel”, because I don’t know about the existing solutions. This is pretty easy to do with a custom function.

 intradataYahoo <- function(symbol, ...) { # ensure xts is available stopifnot(require(xts)) # construct URL URL <- paste0("http://chartapi.finance.yahoo.com/instrument/1.0/", symbol, "/chartdata;type=quote;range=1d/csv") # read the metadata from the top of the file and put it into a usable list metadata <- readLines(paste(URL, collapse=""), 17)[-1L] # split into name/value pairs, set the names as the first element of the # result and the values as the remaining elements metadata <- strsplit(metadata, ":") names(metadata) <- sub("-","_",sapply(metadata, `[`, 1)) metadata <- lapply(metadata, function(x) strsplit(x[-1L], ",")[[1]]) # convert GMT offset to numeric metadata$gmtoffset <- as.numeric(metadata$gmtoffset) # read data into an xts object; timestamps are in GMT, so we don't set it # explicitly. I would set it explicitly, but timezones are provided in # an ambiguous format (eg "CST", "EST", etc). Data <- as.xts(read.zoo(paste(URL, collapse=""), sep=",", header=FALSE, skip=17, FUN=function(i) .POSIXct(as.numeric(i)))) # set column names and metadata (as xts attributes) colnames(Data) <- metadata$values[-1L] xtsAttributes(Data) <- metadata[c("ticker","Company_Name", "Exchange_Name","unit","timezone","gmtoffset")] Data } 

I would think to add something like this to quantmod, but it will need to be tested. I wrote this in less than 15 minutes, so I'm sure there will be some problems.

+9
source

Source: https://habr.com/ru/post/1211323/


All Articles