我有一个包含公司名称的数据字段,例如
company <- c("Microsoft", "Apple", "Cloudera", "Ford")
> company
Company
1 Microsoft
2 Apple
3 Cloudera
4 Ford
等等。
包tm.plugin.webmining
允许您从Yahoo!查询数据基于股票代码的融资:
require(tm.plugin.webmining)
results <- WebCorpus(YahooFinanceSource("MSFT"))
我错过了中间步骤。如何根据公司名称以编程方式查询票证符号?
答案 0 :(得分:6)
我无法用tm.plugin.webmining
软件包做到这一点,但我想出了一个粗略的解决方案 - 拉动&amp; amp;解析此Web文件中的数据:ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt。 我说粗略因为某些原因我的感谢@ thelatemail的评论,这似乎更顺畅:httr::content(httr::GET(...))
来电每次都不起作用 - 我认为它与网址类型(ftp://
)有关但我不这样做那么多的网页刮,所以我无法解释这一点。它在我的Linux上似乎比我的Mac更好,但这可能无关紧要。无论如何,这就是我所得到的:
library(quantmod) ## optional
symbolData <- read.csv(
"ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt",
sep="|")
##
> head(symbolData,10)
Symbol Security.Name Market.Category Test.Issue Financial.Status Round.Lot.Size
1 AAIT iShares MSCI All Country Asia Information Technology Index Fund G N N 100
2 AAL American Airlines Group, Inc. - Common Stock Q N N 100
3 AAME Atlantic American Corporation - Common Stock G N N 100
4 AAOI Applied Optoelectronics, Inc. - Common Stock G N N 100
5 AAON AAON, Inc. - Common Stock Q N N 100
6 AAPL Apple Inc. - Common Stock Q N N 100
7 AAVL Avalanche Biotechnologies, Inc. - Common Stock G N N 100
8 AAWW Atlas Air Worldwide Holdings - Common Stock Q N N 100
9 AAXJ iShares MSCI All Country Asia ex Japan Index Fund G N N 100
10 ABAC Aoxin Tianli Group, Inc. - Common Shares S N N 100
修改强>
根据@ GSee的建议,获得源数据的(可能)更健壮的方法是使用包stockSymbols()
中的TTR
函数:
> symbolData2 <- stockSymbols(exchange="NASDAQ")
Fetching NASDAQ symbols...
> ##
> head(symbolData2)
Symbol Name LastSale MarketCap IPOyear Sector
1 AAIT iShares MSCI All Country Asia Information Technology Index Fun 34.556 6911200 NA <NA>
2 AAL American Airlines Group, Inc. 40.500 29164164453 NA Transportation
3 AAME Atlantic American Corporation 4.020 83238028 NA Finance
4 AAOI Applied Optoelectronics, Inc. 20.510 303653114 2013 Technology
5 AAON AAON, Inc. 18.420 1013324613 NA Capital Goods
6 AAPL Apple Inc. 103.300 618546661100 1980 Technology
Industry Exchange
1 <NA> NASDAQ
2 Air Freight/Delivery Services NASDAQ
3 Life Insurance NASDAQ
4 Semiconductors NASDAQ
5 Industrial Machinery/Components NASDAQ
6 Computer Manufacturing NASDAQ
我不知道您是否只想从名称中获取股票代码,但如果您还在寻找实际的股价信息,您可以这样做:
namedStock <- function(name="Microsoft",
start=Sys.Date()-365,
end=Sys.Date()-1){
ticker <- symbolData[agrep(name,symbolData[,2]),1]
getSymbols(
Symbols=ticker,
src="yahoo",
env=.GlobalEnv,
from=start,to=end)
}
##
## an xts object named MSFT will be added to
## the global environment, no need to assign
## to an object
namedStock()
##
> str(MSFT)
An ‘xts’ object on 2013-09-03/2014-08-29 containing:
Data: num [1:251, 1:6] 31.8 31.4 31.1 31.3 31.2 ...
- attr(*, "dimnames")=List of 2
..$ : NULL
..$ : chr [1:6] "MSFT.Open" "MSFT.High" "MSFT.Low" "MSFT.Close" ...
Indexed by objects of class: [Date] TZ: UTC
xts Attributes:
List of 2
$ src : chr "yahoo"
$ updated: POSIXct[1:1], format: "2014-09-02 21:51:22.792"
> chartSeries(MSFT)
就像我说的那样,这不是最干净的解决方案,但希望它可以帮助你。另请注意,我的数据来源是拉动在纳斯达克(大多数主要公司)交易的公司,但您可以轻松地将其与其他来源合并。