在社区的帮助下,我创建了从.csv文件列表中下载多个代码的代码,并为所有这些代码创建了每日范围函数。现在我想创建一个类似的函数来计算每个股票代码的每日百分比变化(%)并应用于所有这些。
这是我的方法:
library(quantmod)
Tickers <- read.csv("nasdaq_tickers_list.csv", stringsAsFactors = FALSE)
getSymbols(Tickers$Tickers,from="2018-01-01", src="yahoo" )
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
Percentage <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".percentage")
column_names <- c(names(x), stock_name)
x$percentage <- quantmod::Close(today) - quantmod::Close(yesterday)/100
x <- setNames(x, column_names)
return(x)
}
all_stocks <- lapply(all_stocks, percentage_change)
有关如何创建%函数的任何帮助?
非常感谢。
创建像下面这样的功能就可以了
percentage_change <- function(x) {
stock_name <- stringi::stri_extract(names(x)[1], regex = "^[A-Z]+")
stock_name <- paste0(stock_name, ".%change")
column_names <- c(names(x), stock_name)
x$change <- (quantmod::Cl(x) - quantmod::Lag(quantmod::Cl(x)))/quantmod::Lag(quantmod::Cl(x)) * 100
x <- setNames(x, column_names)
return(x)
}
all_stocks <- lapply(all_stocks, percentage_change)
head(all_stocks$MSFT)
MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted MSFT.%change
2007-01-03 29.91 30.25 29.40 29.86 76935100 22.67236 NA
2007-01-04 29.70 29.97 29.44 29.81 45774500 22.63439 -0.1674548
2007-01-05 29.63 29.75 29.45 29.64 44607200 22.50531 -0.5702784
2007-01-08 29.65 30.10 29.53 29.93 50220200 22.72550 0.9784110
2007-01-09 30.00 30.18 29.73 29.96 44636600 22.74828 0.1002305
2007-01-10 29.80 29.89 29.43 29.66 55017400 22.52049 -1.0013318
您也可以使用以下内容,但标题是每日。对于列表中的每个股票,都会返回。
all_stocks <- lapply(all_stocks , function(x) merge(x, dailyReturn(x, leading = FALSE) * 100))
head(all_stocks$MSFT)
MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume MSFT.Adjusted daily.returns
2007-01-03 29.91 30.25 29.40 29.86 76935100 22.67236 NA
2007-01-04 29.70 29.97 29.44 29.81 45774500 22.63439 -0.1674548
2007-01-05 29.63 29.75 29.45 29.64 44607200 22.50531 -0.5702784
2007-01-08 29.65 30.10 29.53 29.93 50220200 22.72550 0.9784110
2007-01-09 30.00 30.18 29.73 29.96 44636600 22.74828 0.1002305
2007-01-10 29.80 29.89 29.43 29.66 55017400 22.52049 -1.0013318
使用data.table
的替代方案,您可以尝试:
library(quantmod)
setSymbolLookup(QQQ='yahoo',SPY='google')
getSymbols(c('QQQ', 'SPY'),from="2018-01-01", src ="yahoo" )
stock_data = sapply(.GlobalEnv, is.xts)
all_stocks <- do.call(list, mget(names(stock_data)[stock_data]))
library(data.table)
lapply(all_stocks,
function(x) as.data.table(x)[, paste0(gsub("\\..*","",names(x)[1]), '.prctg') :=
dailyReturn(x, leading = FALSE)*100])
$`QQQ`
index QQQ.Open QQQ.High QQQ.Low QQQ.Close QQQ.Volume QQQ.Adjusted QQQ.prctg
1: 2018-01-02 156.56 158.53 156.17 158.49 32573300 157.8953 NA
2: 2018-01-03 158.64 160.17 158.61 160.03 29383600 159.4296 0.9716663
3: 2018-01-04 160.58 160.79 160.08 160.31 24776100 159.7085 0.1749666
4: 2018-01-05 161.07 162.03 160.77 161.92 26992300 161.3125 1.0043042
5: 2018-01-08 161.92 162.63 161.86 162.55 23159100 161.9401 0.3890841
---
174: 2018-09-10 182.15 182.25 180.73 181.72 26132000 181.7200 0.3368119
175: 2018-09-11 180.99 183.42 180.52 183.12 30116500 183.1200 0.7704127
176: 2018-09-12 182.85 182.98 181.01 182.58 36204000 182.5800 -0.2948848
177: 2018-09-13 183.72 184.88 183.64 184.53 31133200 184.5300 1.0680233
178: 2018-09-14 184.66 184.95 183.26 183.99 31215200 183.9900 -0.2926321
$SPY
index SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted SPY.prctg
1: 2018-01-02 267.84 268.81 267.40 268.77 86655700 266.5012 NA
2: 2018-01-03 268.96 270.64 268.96 270.47 90070400 268.1868 0.63251556
3: 2018-01-04 271.20 272.16 270.54 271.61 80636400 269.3172 0.42148260
4: 2018-01-05 272.51 273.56 271.95 273.42 83524000 271.1119 0.66640702
5: 2018-01-08 273.31 274.10 272.98 273.92 57319200 271.6077 0.18286884
---
174: 2018-09-10 288.74 289.04 287.88 288.10 50210900 288.1000 0.17385257
175: 2018-09-11 287.37 289.55 286.98 289.05 50530500 289.0500 0.32974036
176: 2018-09-12 289.06 289.80 288.23 289.12 59810800 289.1200 0.02421969
177: 2018-09-13 290.32 291.04 290.00 290.83 51034200 290.8300 0.59144716
178: 2018-09-14 291.06 291.27 290.00 290.88 54962300 290.8800 0.01719836