R金融数据分析之quantmod包抓取股票行情数据小例子

浏览: 1663

抓去四家公司的全部股票行情数据
new.environment <- new.env()
library(quantmod)
## Loading required package: Defaults
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
getSymbols(c("AAPL", "ORCL", "MSFT", "GOOG"), src = "yahoo", env = new.environment)
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
## [1] "AAPL" "ORCL" "MSFT" "GOOG"
# 查看抓取的全量数据情况
str(get("AAPL", env = new.environment))
## An 'xts' object on 2007-01-03/2014-04-21 containing:
##   Data: num [1:1837, 1:6] 86.3 84 85.8 86 86.5 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "AAPL.Open" "AAPL.High" "AAPL.Low" "AAPL.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2014-04-23 01:31:15"
str(get("ORCL", env = new.environment))
## An 'xts' object on 2007-01-03/2014-04-21 containing:
##   Data: num [1:1837, 1:6] 17.2 17.6 17.6 17.6 17.9 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "ORCL.Open" "ORCL.High" "ORCL.Low" "ORCL.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2014-04-23 01:31:17"
str(get("MSFT", env = new.environment))
## An 'xts' object on 2007-01-03/2014-04-21 containing:
##   Data: num [1:1837, 1:6] 29.9 29.7 29.6 29.6 30 ...
##  - 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-04-23 01:31:18"
str(get("GOOG", env = new.environment))
## An 'xts' object on 2014-03-27/2014-04-21 containing:
##   Data: num [1:17, 1:6] 568 561 567 559 600 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "GOOG.Open" "GOOG.High" "GOOG.Low" "GOOG.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2014-04-23 01:31:18"
# 查看四家公司前六情况
head(get("AAPL", env = new.environment))
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2007-01-03     86.29     86.58    81.90      83.80    44225700
## 2007-01-04     84.05     85.95    83.82      85.66    30259300
## 2007-01-05     85.77     86.20    84.40      85.05    29812200
## 2007-01-08     85.96     86.53    85.28      85.47    28468100
## 2007-01-09     86.45     92.98    85.15      92.57   119617800
## 2007-01-10     94.75     97.80    93.45      97.00   105460000
##            AAPL.Adjusted
## 2007-01-03         80.54
## 2007-01-04         82.33
## 2007-01-05         81.75
## 2007-01-08         82.15
## 2007-01-09         88.97
## 2007-01-10         93.23
head(get("ORCL", env = new.environment))
##            ORCL.Open ORCL.High ORCL.Low ORCL.Close ORCL.Volume
## 2007-01-03     17.22     17.78    17.10      17.51    52241700
## 2007-01-04     17.55     17.87    17.30      17.68    33559800
## 2007-01-05     17.62     17.76    17.44      17.64    36154800
## 2007-01-08     17.63     17.93    17.45      17.86    31018100
## 2007-01-09     17.93     17.98    17.65      17.82    31417000
## 2007-01-10     17.66     17.80    17.55      17.77    27822400
##            ORCL.Adjusted
## 2007-01-03         16.66
## 2007-01-04         16.82
## 2007-01-05         16.78
## 2007-01-08         16.99
## 2007-01-09         16.95
## 2007-01-10         16.90
head(get("MSFT", env = new.environment))
##            MSFT.Open MSFT.High MSFT.Low MSFT.Close MSFT.Volume
## 2007-01-03     29.91     30.25    29.40      29.86    76935100
## 2007-01-04     29.70     29.97    29.44      29.81    45774500
## 2007-01-05     29.63     29.75    29.45      29.64    44607200
## 2007-01-08     29.65     30.10    29.53      29.93    50220200
## 2007-01-09     30.00     30.18    29.73      29.96    44636600
## 2007-01-10     29.80     29.89    29.43      29.66    55017400
##            MSFT.Adjusted
## 2007-01-03         25.27
## 2007-01-04         25.23
## 2007-01-05         25.08
## 2007-01-08         25.33
## 2007-01-09         25.35
## 2007-01-10         25.10
head(get("GOOG", env = new.environment))
##            GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume
## 2014-03-27     568.0     568.0    552.9      558.5       13100
## 2014-03-28     561.2     566.4    558.7      560.0       41100
## 2014-03-31     566.9     567.0    556.9      557.0       10800
## 2014-04-01     558.7     568.5    558.7      567.2        7900
## 2014-04-02     600.0     604.8    562.2      567.0      146700
## 2014-04-03     569.9     587.3    564.1      569.7     5085200
##            GOOG.Adjusted
## 2014-03-27         558.5
## 2014-03-28         560.0
## 2014-03-31         557.0
## 2014-04-01         567.2
## 2014-04-02         567.0
## 2014-04-03         569.7
1)求出Apple公司在2013.1-2013.10的股票总成交量使用


getSymbols("AAPL", src = "yahoo", from = "2013-01-01", to = "2013-10-31")
## [1] "AAPL"
# 查看抓取数据的情况
summary(AAPL)
##      Index              AAPL.Open     AAPL.High      AAPL.Low  
##  Min.   :2013-01-02   Min.   :388   Min.   :400   Min.   :385  
##  1st Qu.:2013-03-19   1st Qu.:434   1st Qu.:438   1st Qu.:429  
##  Median :2013-06-04   Median :455   Median :458   Median :449  
##  Mean   :2013-06-03   Mean   :460   Mean   :464   Mean   :454  
##  3rd Qu.:2013-08-17   3rd Qu.:488   3rd Qu.:492   3rd Qu.:484  
##  Max.   :2013-10-31   Max.   :554   Max.   :555   Max.   :542  
##    AAPL.Close   AAPL.Volume       AAPL.Adjusted
##  Min.   :390   Min.   : 6965200   Min.   :381  
##  1st Qu.:432   1st Qu.:10935450   1st Qu.:423  
##  Median :453   Median :13426000   Median :443  
##  Mean   :459   Mean   :15257112   Mean   :450  
##  3rd Qu.:488   3rd Qu.:17623850   3rd Qu.:482  
##  Max.   :549   Max.   :52173300   Max.   :532
# 计算2013年1月到10月的股票总成交量
sum(Vo(AAPL))
## [1] 3.219e+09
2)找出这些股票暴涨暴跌的时间点(例如开盘价或收盘价比前一天涨跌幅度超过2%),通过搜索引擎寻找是什么原因导致这些暴涨暴跌,观察(或用程序分析)数据,看就暴涨暴跌事件是否有可以利用的买卖规律


# 查看AAPL涨跌幅超过2%的情况
AAPL <- Delt(Cl(get("AAPL", env = new.environment)))
length(AAPL[which(AAPL > 0.02), ])
## [1] 266
plot(AAPL[which(AAPL > 0.02), ])
 



# 查看ORCL涨跌幅超过2%的情况
ORCL <- Delt(Cl(get("ORCL", env = new.environment)))
length(ORCL[which(ORCL > 0.02), ])
## [1] 209
plot(ORCL[which(ORCL > 0.02), ])

 


# 查看MSFT涨跌幅超过2%的情况
MSFT <- Delt(Cl(get("MSFT", env = new.environment)))
length(MSFT[which(MSFT > 0.02), ])
## [1] 185
plot(MSFT[which(MSFT > 0.02), ])

 


# 查看GOOG涨跌幅超过2%的情况
GOOG <- Delt(Cl(get("GOOG", env = new.environment)))
length(GOOG[which(GOOG > 0.02), ])
## [1] 2
plot(GOOG[which(GOOG > 0.02), ])
## Error: missing value where TRUE/FALSE needed
GOOG股票为出现收盘价涨跌幅超过2%的情况(其股票从2014年3月27号统计至今),其他三家股票(从2007年统计至今)的涨跌幅超过2%的情况来看,波动频数与波动浮动较大的均处于2008年、2009年,主要是由于全球经济动荡原因所致。


3)截取一段时间内这四家公司股价数据(注意分红派息除权对股价的影响),用R中的相关性分析判断股价之间的相关性,或者用R基础课程第八周所讲的MIC指标对其进行分析


# 查看能抓取GOOG股票信息的时间段
periodicity(get("GOOG", env = new.environment))
## Daily periodicity from 2014-03-27 to 2014-04-21
# 故对四家公司的股票抓取时间都统一为从2014-03-27到2014-04-21
getSymbols(c("AAPL", "ORCL", "MSFT", "GOOG"), src = "yahoo", env = new.environment, 
    from = "2014-03-27", to = "2014-04-21")
## [1] "AAPL" "ORCL" "MSFT" "GOOG"
# 将四家公司股票的每天调整价格整理在一个数据框中
m <- cbind(Ad(get("AAPL", env = new.environment)), Ad(get("ORCL", env = new.environment)), 
    Ad(get("MSFT", env = new.environment)), Ad(get("GOOG", env = new.environment)))
# 利用corr.test()函数计算四家公司股票的调整价格的相关系数和概率,并画出相关系数图
library(psych)
corr.test(as.data.frame(m))
## Call:corr.test(x = as.data.frame(m))
## Correlation matrix 
##               AAPL.Adjusted ORCL.Adjusted MSFT.Adjusted GOOG.Adjusted
## AAPL.Adjusted          1.00          0.51          0.70          0.71
## ORCL.Adjusted          0.51          1.00          0.84          0.57
## MSFT.Adjusted          0.70          0.84          1.00          0.76
## GOOG.Adjusted          0.71          0.57          0.76          1.00
## Sample Size 
## [1] 17
## Probability values (Entries above the diagonal are adjusted for multiple tests.) 
##               AAPL.Adjusted ORCL.Adjusted MSFT.Adjusted GOOG.Adjusted
## AAPL.Adjusted          0.00          0.04          0.01          0.01
## ORCL.Adjusted          0.04          0.00          0.00          0.04
## MSFT.Adjusted          0.00          0.00          0.00          0.00
## GOOG.Adjusted          0.00          0.02          0.00          0.00
## 
##  To see confidence intervals of the correlations, print with the short=FALSE option
library(corrplot)
corrplot.mixed(cor(m), lower = "ellipse", upper = "circle")
 



从相关系数中容易看出股票ORCL与MSFT间的相关系数达到0.84,属于强相关;APPL与MSFT、GOOG股票间的相关系数也超过0.7,属于明显相关。

顶1

推荐 0
本文由 谢佳标 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册