我正在尝试从Yahoo Finance获得股票的调整后收盘价。
来自excel文件April2018.xlsx的一些数据如下所示
dput(head(data,20)):
structure(list(Warrant.Code = c(11880, 12311, 13423, 13437, 14271,
14704, 14770, 14812, 14924, 15285, 16189, 16575, 16584, 16595,
16713, 16736, 16744, 17081, 17082, 17158), Warrant.Name = structure(c(14L,
161L, 21L, 15L, 50L, 144L, 114L, 107L, 116L, 23L, 195L, 30L,
76L, 150L, 45L, 131L, 91L, 18L, 41L, 179L), .Label = c("JP-AAC @EC1804A",
"JP-AAC @EC1805C", "JP-AAC @EP1805A", "JP-AAC @EP1805B", "JP-ACMT@EC1804A",
"JP-AGBK@EC1805A", "JP-AIA @EC1804A", "JP-AIA @EC1804C", "JP-AIA @EC1805A",
"JP-AIA @EC1805B", "JP-AIA @EP1804B", "JP-BOCL@EC1804B", "JP-BOCL@EC1805A",
"JP-BOCL@EC1805B", "JP-BOCL@EP1804A", "JP-BRIC@EC1805A", "JP-BYD @EC1804A",
"JP-BYD @EC1804B", "JP-BYD @EC1805A", "JP-CCB @EC1804A", "JP-CCB @EC1805A",
"JP-CGDN@EC1805A", "JP-CGHL@EC1805A", "JP-CGNP@EC1804A", "JP-CGS @EC1805A",
"JP-CICC@EC1805A", "JP-CKA @EC1804A", "JP-CKH @EC1805A", "JP-CMB @EC1804A",
"JP-CMB @EC1805A", "JP-CMB @EC1805B", "JP-CMBC@EC1805A", "JP-CMOB@EC1804A",
"JP-CMOB@EC1804B", "JP-CMOB@EC1804C", "JP-CMOB@EC1805B", "JP-CMOB@EP1805A",
"JP-CNBM@EC1805A", "JP-COVS@EC1805A", "JP-CRCC@EC1805A", "JP-CSCH@EC1804A",
"JP-CTEL@EC1804A", "JP-CTEL@EC1805A", "JP-CTIH@EC1805A", "JP-GAC @EC1804A",
"JP-GEG @EC1804A", "JP-GEG @EC1804B", "JP-GEG @EC1805B", "JP-GEG @EP1805A",
"JP-GR&F@EC1805A", "JP-GWMC@EC1804A", "JP-HKEX@EC1804A", "JP-HKEX@EC1805B",
"JP-HKEX@EC1805D", "JP-HKEX@EC1805E", "JP-HKEX@EP1805A", "JP-HKEX@EP1805C",
"JP-HSBC@EC1804A", "JP-HSBC@EC1804B", "JP-HSBC@EC1804C", "JP-HSBC@EC1804D",
"JP-HSBC@EC1805A", "JP-HSBC@EC1805B", "JP-HSCI@EC1805A", "JP-HTSE@EC1804A",
"JP-HTSE@EC1805A", "JP-HUAP@EC1804A", "JP-ICBC@EC1804B", "JP-ICBC@EC1805A",
"JP-ICBC@EP1804A", "JP-ICBC@EP1805B", "JP-IGG @EC1804A", "JP-IGG @EC1804B",
"JP-IGG @EC1805A", "JP-KECL@EC1805A", "JP-L&F @EC1805A", "JP-MI&S@EC1805A",
"JP-NCI @EC1804A", "JP-NWD @EC1804A", "JP-NWD @EC1804B", "JP-POLY@EC1805A",
"JP-PSBC@EC1804A", "JP-SHEC@EC1805A", "JP-SHKP@EC1804A", "JP-SHKP@EC1805A",
"JP-SHKP@EP1804B", "JP-SJM @EC1804A", "JP-SJM @EC1805A", "JP-SMIC@EC1804A",
"JP-SMIC@EC1805A", "JP-WYNN@EC1805A", "JP-ZTE @EC1804A", "JP-ZTE @EC1804B",
"JPAMCS3@EC1804A", "JPAMCS3@EC1804B", "JPANGAN@EC1805A", "JPASMPT@EC1804A",
"JPBOCHK@EC1804B", "JPBOCHK@EC1805A", "JPBOCHK@EP1804A", "JPBYDEI@EC1804A",
"JPBYDEI@EC1805A", "JPCEAIR@EC1804A", "JPCINDA@EC1804A", "JPCITBK@EC1805A",
"JPCKINF@EC1804A", "JPCLIFE@EC1804A", "JPCLIFE@EC1804B", "JPCLIFE@EC1805A",
"JPCLIFE@EC1805B", "JPCMOLY@EC1804A", "JPCMOLY@EC1805A", "JPCNOOC@EC1804B",
"JPCNOOC@EC1805A", "JPCNOOC@EP1804A", "JPCNOOC@EP1805B", "JPCP&CC@EC1805A",
"JPCP&CC@EC1805B", "JPCP&CC@EC1805C", "JPCSA50@EC1804A", "JPCSA50@EC1805A",
"JPCSA50@EC1805B", "JPCSHCL@EC1804A", "JPCSHCL@EC1805A", "JPCSHEN@EC1804A",
"JPFOSUN@EC1804A", "JPFOSUN@EC1804B", "JPFTA50@EC1804A", "JPFTA50@EC1805A",
"JPGEELY@EC1804A", "JPGEELY@EC1804C", "JPGEELY@EC1804E", "JPGEELY@EC1804G",
"JPGEELY@EC1805A", "JPGEELY@EC1805B", "JPGEELY@EP1804B", "JPGEELY@EP1804D",
"JPGEELY@EP1804F", "JPGFSEC@EC1804A", "JPGJINT@EC1804A", "JPHLAND@EC1804A",
"JPHLAND@EC1805A", "JPKINGB@EC1804A", "JPKINGB@EC1805A", "JPKINGB@EC1805B",
"JPKINGD@EC1805A", "JPKSOFT@EC1804A", "JPKSOFT@EC1805A", "JPLENOV@EC1804A",
"JPLFPPT@EC1804A", "JPLFPPT@EC1805A", "JPLININ@EC1804A", "JPMEITU@EC1805A",
"JPMELCO@EC1804A", "JPNDPHL@EC1804A", "JPPETCH@EC1805A", "JPPETCH@EC1805B",
"JPPICCG@EC1805A", "JPPINAN@EC1804A", "JPPINAN@EC1804C", "JPPINAN@EC1805A",
"JPPINAN@EC1805B", "JPPINAN@EC1805C", "JPPINAN@EC1805D", "JPPINAN@EC1805E",
"JPPINAN@EC1805H", "JPPINAN@EC1805I", "JPPINAN@EP1804B", "JPPINAN@EP1805F",
"JPPINAN@EP1805G", "JPSANDS@EC1804A", "JPSANDS@EC1805A", "JPSPDRG@EC1804A",
"JPSUNAC@EC1804A", "JPTENCT@EC1804A", "JPTENCT@EC1804C", "JPTENCT@EC1804D",
"JPTENCT@EC1804E", "JPTENCT@EC1804F", "JPTENCT@EC1805A", "JPTENCT@EC1805B",
"JPTENCT@EC1805C", "JPTENCT@EC1805D", "JPTENCT@EC1805F", "JPTENCT@EC1805G",
"JPTENCT@EC1805H", "JPTENCT@EC1805I", "JPTENCT@EC1805K", "JPTENCT@EC1805L",
"JPTENCT@EP1804B", "JPTENCT@EP1804G", "JPTENCT@EP1805E", "JPTENCT@EP1805J",
"JPVANKE@EC1804A", "JPVANKE@EC1805A", "JPWANWA@EC1805A", "JPZHJIN@EC1805A"
), class = "factor"), Trade.Date = structure(c(17651, 17651,
17651, 17651, 17651, 17651, 17651, 17651, 17651, 17651, 17651,
17651, 17651, 17651, 17651, 17651, 17651, 17651, 17651, 17651
), class = "Date"), Issuer = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "JP", class = "factor"),
Underlying = c(3988, 2318, 939, 3988, 2777, 148, 883, 2628,
883, 384, 2202, 3968, 494, 960, 2238, 175, 1128, 1211, 1668,
700), Call.Put = structure(c(1L, 1L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Call",
"Put"), class = "factor"), Maturity.Date = structure(c(17682,
17681, 17653, 17651, 17682, 17654, 17682, 17651, 17665, 17653,
17653, 17669, 17653, 17651, 17651, 17651, 17653, 17651, 17651,
17651), class = "Date"), Strike.Currency = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "HKD", class = "factor"), Strike = c(4.68,
69.69, 8.08, 3.83, 22.22, 48.48, 10.88, 28, 10.36, 25.52,
34.34, 32.88, 4.84, 26.62, 22.3, 19.19, 24.319, 90.9, 2.52,
408.18)), .Names = c("Warrant.Code", "Warrant.Name", "Trade.Date",
"Issuer", "Underlying", "Call.Put", "Maturity.Date", "Strike.Currency",
"Strike"), row.names = c(NA, 20L), class = "data.frame")
下面是我的代码:
install.packages("quantmod")
install.packages("xlsx")
library(quantmod)
library(xlsx)
data=read.xlsx("C:/Users/Asus/Desktop/April2018.xlsx",sheetName="April2018")
dput(head(data,20))
data[,5]=paste(data[,5],".HK",sep="")
getSymbols(data[1,5],from=data[,7],to=data[,7],src="yahoo")
price=Ad(cat("`",getSymbols(data[1,5],from=data[,7],to=data[,7],src="yahoo"),"`",sep=""))
我想要的是下面的代码,这样我就不必手动键入股票报价:
price=Ad(`3988.HK`)
但是,我不确定为什么它不适用于
price=Ad(cat("`",getSymbols(data[1,5],from=data[,7],to=data[,7],src="yahoo"),"`",sep=""))
错误是: Ad(cat(“`”,getSymbols(data [1,5],从= data [,7],到= data [,: 下标超出范围:没有包含“已调整”的列名
答案 0 :(得分:0)
问题出在您使用Ad()
上。
该错误是因为它在内部运行has.Ad()
并获得了TRUE结果。
在这种情况下,这是因为您误解了如何为该命令指定输入。运行getSymbols
后,attach()
编辑了同名的xts
对象,然后直接在其上运行Ad()
,即
getSymbols('IBM',src='yahoo')
Ad(IBM)
或者您的情况是这样的:
getSymbols(data[1,5],from=data[,7],to=data[,7],src="yahoo")
# "3988.HK"
# Apparently that also loads an xts() with attach(), even tho that's a
# really bad practice
str(`3988.HK`)
An ‘xts’ object on 2018-05-31/2018-05-31 containing: Data: num [1, 1:6] 4.11 4.13 4.07 4.10 4.85e+08 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:6] "3988.HK.Open" "3988.HK.High" "3988.HK.Low" "3988.HK.Close" ... Indexed by objects of class: [Date] TZ: UTC xts Attributes: List of 2 $ src : chr "yahoo" $ updated: POSIXct[1:1], format: "2018-07-01 14:51:15"
Ad(`3988.HK`)
3988.HK.Adjusted 2018-05-31 4.1
就可以了。