在《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’