[英]How to use Cl() with character?
I have a list of symbols in a csv file and I want to screen for the ones with closing price higher than 5. 我在csv文件中有一个交易品种列表,我想筛选收盘价高于5的交易品种。
I already ran getSymbols for all items in the csv file. 我已经为csv文件中的所有项目运行了getSymbols。
My test runs into error here. 我的测试在这里出错。 I think it's because Cl() doesn't accept characters as arguments. 我认为这是因为Cl()不接受字符作为参数。 How can I convert character to xts? 如何将字符转换为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: 假设您已经阅读了如上所述的getSymbols
价格系列,那么:
# 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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.