Search code examples
rsubsetquantmod

How to subset a list of stocks to get a new list based on quartile values of volume data of last day


I'm using getSymbols to download data from a few stocks.

I have interest in the volume data, so I split volume data column into (0-4) to allocate each volume data to its correspondent quartile (1,2,3,4) in a new column called xxx.Volqrank

Now I'd like to look into the column xxx.Volqrank, in the last row available in the list and tell me which tickers have a value of 3.

The desired result I'm looking for is ,to get a new list with the stocks that its quartile volume data was 3 in the last available data of the list.

#Reproducible example

library(quantmod)

library(xts)

Symbols <-     c("XOM","MSFT","JNJ","IBM","MRK","BAC","DIS","ORCL","LW","NYT","YELP")

start_date=as.Date("2018-06-01")


getSymbols(Symbols,from=start_date)


# Put all stocks in big list, by checking which xts objects are in the global environment.

stock_data = sapply(.GlobalEnv, is.xts)

all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))


#function to split volume data quartiles into 0-4 results

Volume_q_rank <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Volqrank")
column_names <- c(names(x), stock_name)
x$volqrank <- as.integer(cut(quantmod::Vo(x),
                               quantile(quantmod::Vo(x),probs=0:4/4),include.lowest=TRUE))
x <- setNames(x, column_names)
return(x)
}

all_stocks <- lapply(all_stocks, Volume_q_rank)

My initial approach was to use

lapply(all_stocks, function(x) which(x[, grep("\\.Volqrank",names(x))]==3

But it is not working, any ideas?

I'd like the new list to be used later again for download data of the stocks , but this time using different interval (i.e 5 min )

Any help will be appreciated.

Thanks.


Solution

  • One way of getting what you want that you mention in the comments is like this:

    sapply returns a named vector with TRUE or FALSE per stock. Using names with which will return the names of the stocks that you are interested in.

    stock3 <- sapply(all_stocks, function(x) {last(x[, grep("\\.Volqrank",names(x))]) == 3})
    stocks_with3 <- names(which(stock3 == TRUE))