# R七种武器之金融数据分析quantmod(第12期) 第一周 书面作业
# http://www.dataguru.cn/myclassnew.php?mod=new_basicforlesson&op=stuwork&stuid=41391
#
# quantmod的ETL函数下载Apple,Microsoft,Oracle,Google四家公司全量股票行情数据
# 1)求出Apple公司在2013.1-2013.10的股票总成交量使用
# 2)找出这些股票暴涨暴跌的时间点(例如开盘价或收盘价比前一天涨跌幅度超过2%),通过搜索引擎寻找是什么原因导致这些暴涨暴跌,观察(或用程序分析)数据,看就暴涨暴跌事件是否有可以利用的买卖规律
# 3)截取一段时间内这四家公司股价数据(注意分红派息除权对股价的影响),用R中的相关性分析判断股价之间的相关性,或者用R基础课程第八周所讲的MIC指标对其进行分析
#
# 学生:patdelphi
# 2015-9-7

library(quantmod)
{
#下载Apple,Microsoft,Oracle,Google四家公司全量股票行情数据
getSymbols("AAPL")
save(AAPL,file="AAPL.RDATA")
cat("Get Apple data done, total days is ", length(index(AAPL)),"\n",sep="")

getSymbols("MSFT")
save(MSFT,file="MSFT.RDATA")
cat("Get Microsoft data done, total days is ", length(index(AAPL)),"\n",sep="")

getSymbols("ORCL")
save(ORCL,file="ORCL.RDATA")
cat("Get Oracle data done, total days is ", length(index(AAPL)),"\n",sep="")

getSymbols("GOOGL")
save(GOOGL,file="GOOGL.RDATA")
cat("Get Google data done, total days is ", length(index(AAPL)),"\n",sep="")

# 1)求出Apple公司在2013.1-2013.10的股票总成交量使用
AAPL2013 <- AAPL['2013-01/2013-10']
cat("Sum of total transaction of AAPL from 2013-1-1 to 2013-10-31 is ", colSums(AAPL2013[,5]),"\n",sep="")

# 2)找出这些股票暴涨暴跌的时间点(例如开盘价或收盘价比前一天涨跌幅度超过2%),通过搜索引擎寻找是什么原因导致这些暴涨暴跌,观察(或用程序分析)数据,看就暴涨暴跌事件是否有可以利用的买卖规律
load("AAPL.RDATA")
AAPL_Boom_Slump <- AAPL[which(abs(ClCl(AAPL)) > .02)]
cat("Total boom/slump days of Apple from 2013-1-1 till now is ", length(index(AAPL_Boom_Slump)),"\n",sep="")

load("MSFT.RDATA")
MSFT_Boom_Slump <- MSFT[which(abs(ClCl(MSFT)) > .02)]
cat("Total boom/slump days of Microsoft from 2013-1-1 till now is ", length(index(MSFT_Boom_Slump)),"\n",sep="")

load("ORCL.RDATA")
ORCL_Boom_Slump <- ORCL[which(abs(ClCl(ORCL)) > .02)]
cat("Total boom/slump days of Oracle from 2013-1-1 till now is ", length(index(ORCL_Boom_Slump)),"\n",sep="")

load("GOOGL.RDATA")
GOOGL_Boom_Slump <- GOOGL[which(abs(ClCl(GOOGL)) > .02)]
cat("Total boom/slump days of Google from 2013-1-1 till now is ", length(index(GOOGL_Boom_Slump)),"\n",sep="")

# 3)截取一段时间内这四家公司股价数据(注意分红派息除权对股价的影响),用R中的相关性分析判断股价之间的相关性,或者用R基础课程第八周所讲的MIC指标对其进行分析
CombineAll =cbind(Cl(AAPL),Cl(ORCL),Cl(MSFT),Cl(GOOGL))
cat("The correlation check result of 4 stocks is ","\n",sep="");cor(CombineAll)

#End
}
##     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 ?getSymbols for more details.
## Warning in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=",
## from.m, : downloaded length 159316 != reported length 200
## Get Apple data done, total days is 2186
## Warning in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=",
## from.m, : downloaded length 137370 != reported length 200
## Get Microsoft data done, total days is 2186
## Warning in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=",
## from.m, : downloaded length 137006 != reported length 200
## Get Oracle data done, total days is 2186
## Warning in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=",
## from.m, : downloaded length 160095 != reported length 200
## Get Google data done, total days is 2186
## Sum of total transaction of AAPL from 2013-1-1 to 2013-10-31 is 22534754200
## Total boom/slump days of Apple from 2013-1-1 till now is 549
## Total boom/slump days of Microsoft from 2013-1-1 till now is 389
## Total boom/slump days of Oracle from 2013-1-1 till now is 431
## Total boom/slump days of Google from 2013-1-1 till now is 410
## The correlation check result of 4 stocks is
##              AAPL.Close ORCL.Close  MSFT.Close GOOGL.Close
## AAPL.Close   1.00000000  0.3423986 -0.02350826   0.6331178
## ORCL.Close   0.34239863  1.0000000  0.77078428   0.4969353
## MSFT.Close  -0.02350826  0.7707843  1.00000000   0.3863248
## GOOGL.Close  0.63311779  0.4969353  0.38632484   1.0000000