I'm trying to calculate specific quartile number (in this example Q2) of column data with the positive gap opening value of a stock list.
I try to explain you my approach:
Load my tickers list from a .csv file, create a list with all of them (OK)
library(quantmod)
Tickers <- read.csv("nasdaq_tickers_list.csv", stringsAsFactors = FALSE)
getSymbols(Tickers$Tickers,from="2018-08-01", src="yahoo" )
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
I have the following function working fine to calculate the quartiles of a column (Stock_name.Postitivegap) and split them into their corresponding quartile rank:
Posgapqrank <- 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$posgapqrank <- as.integer(cut(x[, grep(".Positivegap", colnames(x))],quantile(x[,grep(".Positivegap",colnames(x))],probs=0:4/4),include.lowest=TRUE))
x <- setNames(x, column_names)
return(x)
}
Now I'd like a function to calculate an specific quantile of the same original data column , ".Positivegap" i.e. Q2
For that purpose I introduced the 0.25 in the quartile function, but getting error... any help here?
Q2 <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Q2")
column_names <- c(names(x), stock_name)
x$gapq2 <- as.integer(quantile(x[,grep(".Positivegap",colnames(x))],0.25)))
x <- setNames(x, column_names)
return(x)
}
Thank you very much for any comment.
getSymbols("SQ", from="2018-01-01", src="yahoo")
quantile(SQ$SQ.Volume, 0.25)
#How do I ad a new column to the SQ dataset with the Q2 volume data for each day?
I created 2 functions. Q2 and rolling_Q2.
I tested them on the grep of the column name ".Volume" and both work. The Q2 function will calculate the Q2 from the whole dataset you give it. The rolling_Q2 will calculate the value of the Q2 based on a rolling window. Default 22.
Q2 <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Q2")
column_names <- c(names(x), stock_name)
x$gapq2 <- as.integer(quantile(x[,grep(".Positivegap",colnames(x))], 0.25))
x <- setNames(x, column_names)
return(x)
}
rolling_Q2 <- function(x, width = 22) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".Q2")
column_names <- c(names(x), stock_name)
x$gapq2 <- rollapply(x[,grep(".Positivegap",colnames(x))], width = width, FUN = function(x) as.integer(quantile(x, 0.25)))
x <- setNames(x, column_names)
return(x)
}