I have a list of symbols in a csv file and I want to screen for the ones with closing price higher than 5.
I already ran getSymbols for all items in the csv file.
My test runs into error here. I think it's because Cl() doesn't accept characters as arguments. How can I convert character to xts?
> library(quantmod)
> passingset
V1
1 AAB
2 AAR-UN
3 AAV
4 ABT
5 ABX
(...)
> class(passingset)
[1] "data.frame"
> class(passingset$V1)
[1] "character"
> #Remove closing price < 5
> for (i in 1:nrow(passingset)){
+ company <- passingset$V1[i]
+ if(Cl(company)[length(Cl(company))] < 5){
+ passingset <- passingset[!(passingset$V1 == company),, drop = FALSE]
+ } else
+ i = i + 1
+
+ rm(company)
+ }
**Error in Cl(company) :
subscript out of bounds: no column name containing "Close"**
No need for looping!
Assuming you have read the price series with getSymbols
as you mentioned above then:
# an example set of tickers:
pasingset <- data.frame(V1=c('CWB','EEM','VTI','NAK','GIG'),stringsAsFactors = F)
# this gets you the last closings of your tickers
lastClose <- sapply(pasingset$V1,function(x) xts::last(Cl(get(x))))
# shows the tickers which have a close > 5
pasingset$V1[which(lastClose > 5)]
[1] "CWB" "EEM" "VTI"