我试图找到并比较r中两只股票的每日收益和损失百分比。这是代码
library(quantmod)
stockData <- new.env() #Make a new environment for quantmod to store data in
tickers <- c("AAPL","GOOG")
#Set start date
start_date=as.Date("2014-01-01")
getSymbols(tickers, src="yahoo", env=stockData,from=start_date)
for (tick in tickers) {
x <- get(tick, pos=stockData) # get data from stockData environment
x$gl<-((Cl(x)-Op(x))/Op(x))*100 #Daily gain loss percentage
}
我可以计算个别股票的每日收益/损失百分比,但我不知道如何进一步分析多个股票的收益/损失百分比。
实施例
if AAPL(gain/loss percentage) is greater than GOOG(gain/loss percentage) then 1 else -1