收益率
在股票市场中大家十分关注收益率的日度变化值。每天的市场都会开盘和收盘(除开周六、日),计算收益率的方法可以基于开盘情况也可以基于收盘情况,不过通常的做法还是以收盘价格为基准来计算收益率。在收益率中的计算可以分为简单收益率(或者叫百分数收益率)和对数收益率,相对于简单收益率,用得更多的是对数收益率,因为对数收益率具有严谨的“对称性”,同时也具有更容易的统计特性。
简单收益率:
对数收益率:
此处输入图片的描述
股票数据获取
> library(quantmod)
> library(zoo)
> library(xts)
> library(TTR)
> getSymbols("AAPL", from="2017-01-01", to=Sys.Date(), src="yahoo")
[1] "AAPL"
> head(AAPL)
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2017-01-03 28.9500 29.0825 28.6900 29.0375 115127600 27.27764
2017-01-04 28.9625 29.1275 28.9375 29.0050 84472400 27.24711
2017-01-05 28.9800 29.2150 28.9525 29.1525 88774400 27.38567
2017-01-06 29.1950 29.5400 29.1175 29.4775 127007600 27.69097
2017-01-09 29.4875 29.8575 29.4850 29.7475 134247600 27.94460
2017-01-10 29.6925 29.8450 29.5750 29.7775 97848400 27.97279
> library(tseries)
> goog <- get.hist.quote(instrument="GOOG", start="2017-01-01", end="2017-07-01", quote="AdjClose")
time series starts 2017-01-03
time series ends 2017-06-30
> head(goog)
Adjusted
2017-01-03 786.14
2017-01-04 786.90
2017-01-05 794.02
2017-01-06 806.15
2017-01-09 806.65
2017-01-10 804.79
绘制股票图
K 线图
中国习惯是涨红跌绿
···
chartSeries(AAPL, up.col='red', dn.col='green', theme="white")
···
技术分析图
addBBands() 布林线指标,一般而言,股价的运动总是围绕某一价值中枢(如均线、成本线等)在一定的范围内变动,布林线指标正是在上述条件的基础上,引进了“股价信道”的概念,其认为股价信道的宽窄随着股价波动幅度的大小而变化,二期股价信道又具有变异性,它会随着股价的变化而自动调整。正是由于它的灵活性、直观性和趋势性的特点,BOLL (布林)指标渐渐成为投资者广为应用的市场上热门指标。、、
chartSeries(AAPL, up.col='red', dn.col='green', theme="white")
addBBands(n=14, sd=2, draw='bands')
image.png
ADX无法告诉你趋势的发展方向。但是,如果趋势存在,ADX可以衡量趋势的强度。ADX读数上升,代表趋势转强;如果ADX读数下降,趋势转弱。 缺点:单就ADX本身来说,由于指标落后价格走势,所以算不上很好的指标,不适合单就ADX进行操作。不过如果和其他指标配合运用,ADX可以确认市场是否存在趋势,并衡量趋势的强度。
chartSeries(AAPL, up.col='red', dn.col='green', theme="white")
addADX()
image.png
|ADX>= 30|趋势就可以是为强劲 |20 <= ADX < 30|属于中性读数 |ADX< 20|代表市场动能偏。期间内,行情来回游走,没有明显的方向
addMACD( ) 指数平滑异同移动平均线 这是一个常用的震荡指标,由Gerald Appel发明。用序列的快速移动平均线减去慢速移动平均线,可用来识别市场趋势。
> chartSeries(AAPL, up.col='red', dn.col='green', theme="white")
> addMACD()
image.png
> chartSeries(AAPL, up.col='red', dn.col='green', theme="white", TA=c(addBBands(), addMACD(), addVo()))
image.png
计算某支股票日度收益率
简单收益率
> close <- AAPL[, 4]
> close1 <- lag(close, 1)
> head(close1)
AAPL.Close
2017-01-03 NA
2017-01-04 29.0375
2017-01-05 29.0050
2017-01-06 29.1525
2017-01-09 29.4775
2017-01-10 29.7475
> calclose <- merge(close, close1)
> simplerate <- (close-close1)/close1
> names(simplerate) <- "simplerate"
> calrate <- merge(calclose, simplerate)
> head(calrate)
AAPL.Close AAPL.Close.1 simplerate
2017-01-03 29.0375 NA NA
2017-01-04 29.0050 29.0375 -0.001119277
2017-01-05 29.1525 29.0050 0.005085365
2017-01-06 29.4775 29.1525 0.011148306
2017-01-09 29.7475 29.4775 0.009159460
2017-01-10 29.7775 29.7475 0.001008522
对数收益率
> library(PerformanceAnalytics)
> rate <- periodReturn(close, period="daily", type="log")
> head(rate)
daily.returns
2017-01-03 0.000000000
2017-01-04 -0.001119904
2017-01-05 0.005072478
2017-01-06 0.011086622
2017-01-09 0.009117767
2017-01-10 0.001008014
抓取多只股票
> library(quantmod)
> new.environment <- new.env()
> getSymbols(c("AAPL", "ORCL", "MSFT", "GOOG"), src="yahoo", new.environment)
[1] "AAPL" "ORCL" "MSFT" "GOOG"
> str(get("AAPL", env=new.environment))
An ‘xts’ object on 2007-01-03/2020-11-13 containing:
Data: num [1:3493, 1:6] 3.08 3 3.06 3.07 3.09 ...
- 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: "2020-11-15 18:34:09"
> str(get("ORCL", env=new.environment))
An ‘xts’ object on 2007-01-03/2020-11-13 containing:
Data: num [1:3493, 1:6] 17.2 17.5 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: "2020-11-15 18:34:10"
> str(get("MSFT", env=new.environment))
An ‘xts’ object on 2007-01-03/2020-11-13 containing:
Data: num [1:3493, 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: "2020-11-15 18:34:11"
> str(get("GOOG", env=new.environment))
An ‘xts’ object on 2007-01-03/2020-11-13 containing:
Data: num [1:3493, 1:6] 232 234 240 243 242 ...
- 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: "2020-11-15 18:34:13"
求出股票总成交量
> getSymbols("AAPL", src="yahoo", from="2017-01-01", to="2017-07-01")
[1] "AAPL"
> summary(AAPL)
Index AAPL.Open AAPL.High AAPL.Low
Min. :2017-01-03 Min. :28.95 Min. :29.08 Min. :28.69
1st Qu.:2017-02-16 1st Qu.:33.88 1st Qu.:33.98 1st Qu.:33.71
Median :2017-04-03 Median :35.47 Median :35.67 Median :35.25
Mean :2017-04-02 Mean :34.93 Mean :35.14 Mean :34.73
3rd Qu.:2017-05-17 3rd Qu.:36.44 3rd Qu.:36.79 3rd Qu.:36.24
Max. :2017-06-30 Max. :39.00 Max. :39.16 Max. :38.76
AAPL.Close AAPL.Volume AAPL.Adjusted
Min. :29.00 Min. : 56985200 Min. :27.25
1st Qu.:33.88 1st Qu.: 82251600 1st Qu.:31.96
Median :35.45 Median : 94852000 Median :33.45
Mean :34.96 Mean :109157856 Mean :33.00
3rd Qu.:36.57 3rd Qu.:125997600 3rd Qu.:34.56
Max. :39.02 Max. :447940000 Max. :36.97
> sum(Vo(AAPL))
[1] 13644732000
分析股票暴涨暴跌的时间点
查看各公司涨跌幅超过 2% 的情况
> AAPL <- Delt(Cl(get("AAPL", env=new.environment)))
> length(AAPL[which(AAPL>0.02),])
[1] 422
> plot(AAPL[which(AAPL>0.02),])
image.png
> ORCL <- Delt(Cl(get("ORCL", env=new.environment)))
> length(ORCL[which(ORCL>0.02),])
[1] 296
> plot(ORCL[which(ORCL>0.02),])
image.png
> MSFT <- Delt(Cl(get("MSFT", env=new.environment)))
> length(MSFT[which(MSFT>0.02),])
[1] 322
> plot(MSFT[which(MSFT>0.02),])
image.png
> GOOG <- Delt(Cl(get("GOOG", env=new.environment)))
> length(GOOG[which(GOOG>0.02),])
[1] 299
> plot(GOOG[which(GOOG>0.02),])
image.png
相关性判断
调整数据
截取一段时间内这四家公司股价数据(注意分红派息除权对股价的影响)
> periodicity(get("GOOG", env=new.environment))
Daily periodicity from 2017-01-03 to 2017-06-30
> getSymbols(c("AAPL", "ORCL", "MSFT", "GOOG"), src="yahoo", env=new.environment, from="2017-01-03", to="2017-07-01")
[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)))
分析判断相关性并绘图
> 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.81 0.84 0.80
ORCL.Adjusted 0.81 1.00 0.81 0.74
MSFT.Adjusted 0.84 0.81 1.00 0.96
GOOG.Adjusted 0.80 0.74 0.96 1.00
Sample Size
[1] 125
Probability values (Entries above the diagonal are adjusted for multiple tests.)
AAPL.Adjusted ORCL.Adjusted MSFT.Adjusted GOOG.Adjusted
AAPL.Adjusted 0 0 0 0
ORCL.Adjusted 0 0 0 0
MSFT.Adjusted 0 0 0 0
GOOG.Adjusted 0 0 0 0
To see confidence intervals of the correlations, print with the short=FALSE option
> library(corrplot)
> corrplot.mixed(cor(m), lower="ellipse", upper="circle")
image.png
从相关系数中容易看出股票 GOOG 与 MSFT 间的相关系数快达到了 1 ,属于强相关;APPL 与 MSFT、GOOG 、ORCL 股票间的相关系数也超过 0.7,属于明显相关。
网友评论