在《R的极客理想》中看到一个MACD的简单策略。觉得还行,记下来以后优化用。我添加了一些注释帮助大家理解证券交易和R语言代码。
一次都装上,备用
> pkgs <- c("quantmod","zoo","xts","TTR","PerformanceAnalytics","ggplot2")
> install.packages(pkgs)
There is a binary version available but the source version is later:
binary source needs_compilation
zoo 1.8-6 1.8-7 TRUE
Binaries will be installed
……
trying URL 'https://mirrors.tongji.edu.cn/CRAN/bin/windows/contrib/3.6/ggplot2_3.2.1.zip'
Content type 'application/zip' length 3974945 bytes (3.8 MB)
downloaded 3.8 MB
package ‘quantmod’ successfully unpacked and MD5 sums checked
package ‘zoo’ successfully unpacked and MD5 sums checked
package ‘xts’ successfully unpacked and MD5 sums checked
package ‘TTR’ successfully unpacked and MD5 sums checked
package ‘PerformanceAnalytics’ successfully unpacked and MD5 sums checked
package ‘ggplot2’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\chang\AppData\Local\Temp\RtmpygTUIm\downloaded_packages
>
载入quantmod包,数据源是Yahoo,当然外国网站,你懂的,有时候不是能用的。下载全球五大指数数据。
指数名称 |
英文简称 |
---|---|
标普500 |
GSPC |
日经225 |
N225 |
恒生指数 |
HSI |
富时新加坡指STI数 |
STI |
上证指数 |
SSE |
> library(quantmod)
载入需要的程辑包:xts
载入需要的程辑包:zoo
载入程辑包:‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
Registered S3 method overwritten by 'xts':
method from
as.zoo.xts zoo
载入需要的程辑包:TTR
Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo
Version 0.4-0 included new data defaults. See ?getSymbols.
> options(stringsAsFactors = FALSE)
> # 指定股票代码,从yahoo下载
> # 开始日期,自动判断最近交易日,不用自己费心
> symbols <- c("^GSPC","^N225","^HSI","^STI","000001.SS")
> suppressWarnings(getSymbols(symbols,src = "yahoo",from="2012-01-01"))
>
‘getSymbols’ currently uses auto.assign=TRUE by default, but will
use auto.assign=FALSE in 0.5-0. You will still be able to use
‘loadSymbols’ to automatically load data. getOption("getSymbols.env")
and getOption("getSymbols.auto.assign") will still be 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 details.
[1] "^GSPC" "^N225" "^HSI" "^STI" "000001.SS"
> head(`000001.SS`)
000001.SS.Open 000001.SS.High 000001.SS.Low 000001.SS.Close 000001.SS.Volume 000001.SS.Adjusted
2012-01-04 2211.995 2217.520 2168.644 2169.390 49200 2169.390
2012-01-05 2160.897 2183.404 2145.556 2148.452 58800 2148.452
2012-01-06 2148.147 2164.322 2132.626 2163.395 50600 2163.395
2012-01-09 2164.741 2226.218 2148.453 2225.890 76800 2225.890
2012-01-10 2221.832 2288.628 2218.275 2285.744 109600 2285.744
2012-01-11 2282.913 2290.644 2265.185 2276.046 84400 2276.046
> # 准备用收益率替代指数价格,方便对比比较
> df <- merge(GSPC$GSPC.Adjusted,N225$N225.Adjusted,HSI$HSI.Adjusted,STI$STI.Adjusted,`000001.SS`$`000001.SS.Adjusted`)
> names(df) <- c("GSPC","N225","HSI","STI","SSE")
> head(df)
GSPC N225 HSI STI SSE
2012-01-03 1277.06 NA 18877.41 2688.36 NA
2012-01-04 1277.30 8560.11 18727.31 2711.02 2169.390
2012-01-05 1281.06 8488.71 18813.41 2713.02 2148.452
2012-01-06 1277.81 8390.35 18593.06 2715.59 2163.395
2012-01-09 1280.70 NA 18865.72 2691.28 2225.890
2012-01-10 1292.08 8422.26 19004.28 2719.83 2285.744
> g <- ggplot(aes(x = Index,y = Value,color = Series),data = fortify(df,melt = TRUE)) geom_line(size = 1) scale_y_continuous(breaks = seq(1000,30000,4000)) ggtitle("Gloabel Index")
> g
很明显,直接的价格无法比较,所以我们都转化为收益率。顺便算一下年收益率。
> library(PerformanceAnalytics)
载入程辑包:‘PerformanceAnalytics’
The following object is masked from ‘package:graphics’:
legend
> ret_df <- Return.calculate(df,method="discrete")
> chart.CumReturns(ret_df,legend.loc = "topleft",main="Cumulative Daily Returns for Gloable Index")
> Return.annualized(ret_df)
GSPC N225 HSI STI SSE
Annualized Return 0.1157303 0.1510661 0.05739009 0.03016343 0.0374123
>
指数 | 年收益率 |
---|---|
GSPC | 0.1157303 |
N225 | 0.1510661 |
HSI |
0.05739009 |
STI |
0.03016343 |
SSE |
0.0374123 |
这个策略很简单,就是一根30日简单均线的趋势跟踪策略。
># MACD模型
>MACD <- function(dt,n=30){
names(dt) <- c('close')
#均线
dat <- na.locf(dt) #na.locf删掉数据里面的所有NA
dat$ma <- SMA(dat$close,n)
#买卖信号
sig_buy <- which(dat$ma-dat$close>0)
sig_sell <- which(dat$ma-dat$close<0)
sig_buy <- sig_buy[which(diff(sig_buy)>1)]
sig_sell <- sig_sell[which(diff(sig_sell)>1)]
if(first(sig_sell) #用MACD计算一遍历史数据,计算每日收益
> macd_ret <- lapply(df,function(col) MACD(col,30))
> #算年收益率
> t(do.call(rbind.data.frame,lapply(macd_ret, Return.annualized)))
GSPC N225 HSI STI SSE
close 0.2082891 0.1823697 0.1879596 0.03251933 0.1429962
>
指数 | 指数年收益率 | MACD策略年收益率 |
---|---|---|
GSPC | 0.1157303 |
0.2082891 |
N225 | 0.1510661 |
0.1823697 |
HSI |
0.05739009 | 0.1879596 |
STI |
0.03016343 | 0.03251933 |
SSE |
0.0374123 |
0.1429962 |
改善明显啊 。
从2012年到2020年,居然美股达到了年化20%的收益率。巴菲特啊。日本和香港也能18%。A股票也很争气,14%相当漂亮啊。
感谢阅读,欢迎关注和留言