首页 > 财经问答 > r语言如何获取股票(R语言用quantstrat包获取股票)

r语言如何获取股票(R语言用quantstrat包获取股票)

来源:2021-09-12 17:43

R语言可以非常轻松的获得证券(股票、债券、基金、期货(黄金、原油等)、期权),指数、外汇和美联储提供的各种经济数据。我来详细说一下。

quantstrat包的金融数据很全


  1. yahoo提供的各种证券数据,股票、债券、基金、期货、期权、指数都有,非常全。你在雅虎财经页面上看见的我们都能拿到。缺点是咱们这边访问速度非常慢。
  2. Google也提供了相同的数据。但是2018年之后已经不能用了。看老资料的时候一定要注意。
  3. OANDA提供了180天的外汇数据。访问也不是速度的问题。你懂的。
  4. FRED是美联储提供的经济数据。利率、GDP、国债收益率之类的都在这里找。速度慢点,但是能用。
  5. Rdata文件。放什么随意,速度看你的硬盘速度。
  6. CSV文件
  7. MySql数据库

我们先载入quantstrat包

> library(quantstrat)载入需要的程辑包:quantmod载入需要的程辑包:xts载入需要的程辑包:zoo载入程辑包:‘zoo’The following objects are masked from ‘package:base’: as.Date, as.Date.numeric载入需要的程辑包:TTRVersion 0.4-0 included new data defaults. See ?getSymbols.载入需要的程辑包:blotter载入需要的程辑包:FinancialInstrument载入需要的程辑包:PerformanceAnalytics载入程辑包:‘PerformanceAnalytics’The following object is masked from ‘package:graphics’: legend载入需要的程辑包:foreach


yahoo的证券数据

r语言如何获取股票

获取美股苹果公司股票数据


> getSymbols( Symbols = "AAPL", src = "yahoo", from = "2018-01-01", to = "2020-02-24", index.class = "POSIXct")[1] "AAPL"

主要参数:

  1. Symbols证券代码字符型向量
  2. scr数据源,字符型
  3. from开始日期,字符型
  4. to结束日期,字符型
  5. index.class索引类型,字符型

我们看一下股价数据和图表

> head(AAPL) AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted2018-01-02 170.16 172.30 169.26 172.26 25555900 166.80402018-01-03 172.53 174.55 171.96 172.23 29517900 166.77502018-01-04 172.54 173.47 172.08 173.03 22434600 167.54962018-01-05 173.44 175.37 173.05 175.00 23660000 169.45722018-01-08 174.35 175.61 173.93 174.35 20567800 168.82782018-01-09 174.55 175.06 173.41 174.33 21584000 168.8085> chartSeries(AAPL)r语言如何获取股票

获取工商银行A股和港股数据


> getSymbols(+ Symbols = c("601398.SS","1398.HK"),+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "601398.SS" "1398.HK" > head(`601398.SS`) 601398.SS.Open 601398.SS.High 601398.SS.Low 601398.SS.Close 601398.SS.Volume 601398.SS.Adjusted2018-01-02 6.19 6.39 6.11 6.18 537466207 5.6670232018-01-03 6.18 6.32 6.12 6.16 349361124 5.6486832018-01-04 6.18 6.18 6.06 6.07 435136522 5.5661542018-01-05 6.09 6.11 6.06 6.08 281784265 5.5753242018-01-08 6.09 6.10 6.04 6.07 218487604 5.5661542018-01-09 6.07 6.11 6.04 6.09 167185706 5.584494> head(`1398.HK`) 1398.HK.Open 1398.HK.High 1398.HK.Low 1398.HK.Close 1398.HK.Volume 1398.HK.Adjusted2018-01-02 6.32 6.48 6.32 6.48 424057742 5.8602972018-01-03 6.50 6.54 6.46 6.50 411577497 5.8783852018-01-04 6.51 6.66 6.49 6.57 391732554 5.9416902018-01-05 6.62 6.62 6.50 6.56 291961164 5.9326462018-01-08 6.62 6.62 6.53 6.55 256489358 5.9236032018-01-09 6.59 6.59 6.50 6.50 290694432 5.878385> chartSeries(`1398.HK`)> chartSeries(`601398.SS`)r语言如何获取股票r语言如何获取股票

获取道琼斯指数和上证指数数据

Dow Jones Industrial Average

> getSymbols(+ Symbols = "^DJI",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "^DJI"

看一下数据

> head(DJI) DJI.Open DJI.High DJI.Low DJI.Close DJI.Volume DJI.Adjusted2018-01-02 24809.35 24864.19 24741.70 24824.01 341130000 24824.012018-01-03 24850.45 24941.92 24825.55 24922.68 456790000 24922.682018-01-04 24964.86 25105.96 24963.27 25075.13 403280000 25075.132018-01-05 25114.92 25299.79 25112.01 25295.87 358020000 25295.872018-01-08 25308.40 25311.99 25235.41 25283.00 341390000 25283.002018-01-09 25312.05 25439.78 25308.41 25385.80 333490000 25385.80> chartSeries(DJI)r语言如何获取股票

A股大家最关心的上证指数,也有~

SSE Composite Index

代码一样

> getSymbols(+ Symbols = "000001.SS",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "000001.SS"> head(`000001.SS`) 000001.SS.Open 000001.SS.High 000001.SS.Low 000001.SS.Close 000001.SS.Volume 000001.SS.Adjusted2018-01-02 3314.031 3349.053 3314.031 3348.326 202300 3348.3262018-01-03 3347.743 3379.915 3345.289 3369.108 213800 3369.1082018-01-04 3371.000 3392.826 3365.295 3385.710 207000 3385.7102018-01-05 3386.464 3402.069 3380.245 3391.750 213100 3391.7502018-01-08 3391.553 3412.731 3384.559 3409.480 236200 3409.4802018-01-09 3406.112 3417.228 3403.587 3413.900 191500 3413.900> chartSeries(`000001.SS`)r语言如何获取股票

获取比特币数据

CoinMarketCap价格

> getSymbols(+ Symbols = "BTC-USD",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "BTC-USD"

看一下数据

> head(`BTC-USD`) BTC-USD.Open BTC-USD.High BTC-USD.Low BTC-USD.Close BTC-USD.Volume BTC-USD.Adjusted2018-01-01 14112.2 14112.2 13154.7 13657.2 10291200000 13657.22018-01-02 13625.0 15444.6 13163.6 14982.1 16846600192 14982.12018-01-03 14978.2 15572.8 14844.5 15201.0 16871900160 15201.02018-01-04 15270.7 15739.7 14522.2 15599.2 21783199744 15599.22018-01-05 15477.2 17705.2 15202.8 17429.5 23840899072 17429.52018-01-06 17462.1 17712.4 16764.6 17527.0 18314600448 17527.0> chartSeries(`BTC-USD`)r语言如何获取股票

获取黄金价格数据

COMEX黄金价格

> getSymbols(+ Symbols = "GC=F",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "GC=F"

看一下数据

> head(`GC=F`) GC=F.Open GC=F.High GC=F.Low GC=F.Close GC=F.Volume GC=F.Adjusted2019-12-30 1517.7 1518.4 1517.5 1517.8 3597128 1517.82019-12-31 1526.5 1529.0 1519.2 1520.0 117342599 1520.02020-01-02 1521.9 1534.0 1520.9 1531.2 138920019 1531.22020-01-03 1541.4 1556.6 1540.9 1555.2 252835424 1555.22020-01-05 1562.7 1590.9 1562.3 1571.3 1601769 1571.32020-01-06 1578.4 1581.5 1562.7 1565.9 351767794 1565.9> chartSeries(`GC=F`)r语言如何获取股票

获取原油价格数据

NY Mercantile原油价格

> getSymbols(+ Symbols = "CL=F",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "CL=F"

看一下数据

> head(`CL=F`) CL=F.Open CL=F.High CL=F.Low CL=F.Close CL=F.Volume CL=F.Adjusted2020-01-24 54.39 54.41 54.15 54.20 34447364 54.202020-01-26 53.70 53.71 52.15 52.64 1009546 52.642020-01-27 52.89 53.54 52.13 52.97 332069958 52.972020-01-28 53.08 54.08 52.68 53.82 279037102 53.822020-01-29 54.14 54.37 52.81 53.10 277368615 53.102020-01-30 52.79 52.99 51.67 52.85 308221904 52.85> chartSeries(`CL=F`)r语言如何获取股票

获取债券价格数据

美国十年期国债

> getSymbols(+ Symbols = "^TNX",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "^TNX"

看一下数据

> head(TNX) TNX.Open TNX.High TNX.Low TNX.Close TNX.Volume TNX.Adjusted2018-01-02 2.433 2.480 2.425 2.465 0 2.4652018-01-03 2.451 2.465 2.440 2.447 0 2.4472018-01-04 2.473 2.487 2.449 2.453 0 2.4532018-01-05 2.465 2.480 2.436 2.476 0 2.4762018-01-07 NA NA NA NA NA NA2018-01-08 2.471 2.489 2.462 2.480 0 2.480> chartSeries(TNX)r语言如何获取股票

获取外汇价格数据

欧元兑美元

> getSymbols(+ Symbols = "EURUSD=X",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "EURUSD=X"

看有一下数据

> head(`EURUSD=X`) EURUSD=X.Open EURUSD=X.High EURUSD=X.Low EURUSD=X.Close EURUSD=X.Volume EURUSD=X.Adjusted2018-01-01 1.200495 1.201504 1.199904 1.200495 0 1.2004952018-01-02 1.201086 1.208094 1.200855 1.201158 0 1.2011582018-01-03 1.206200 1.206709 1.200495 1.206345 0 1.2063452018-01-04 1.201129 1.209190 1.200495 1.201043 0 1.2010432018-01-05 1.206622 1.208459 1.202154 1.206884 0 1.2068842018-01-08 1.203949 1.205400 1.195972 1.203746 0 1.203746> chartSeries(`EURUSD=X`)r语言如何获取股票

获取基金数据

Vanguard Morgan Growth Fund

> getSymbols(+ Symbols = "VMRGX",+ src = "yahoo",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "VMRGX"

看一下数据

> head(VMRGX) VMRGX.Open VMRGX.High VMRGX.Low VMRGX.Close VMRGX.Volume VMRGX.Adjusted2018-01-02 29.60 29.60 29.60 29.60 0 23.861892018-01-03 29.91 29.91 29.91 29.91 0 24.111802018-01-04 30.08 30.08 30.08 30.08 0 24.248842018-01-05 30.36 30.36 30.36 30.36 0 24.474562018-01-08 30.49 30.49 30.49 30.49 0 24.579362018-01-09 30.56 30.56 30.56 30.56 0 24.63579> chartSeries(VMRGX)r语言如何获取股票

FRED的经济数据

FRED是美联储给大家的免费经济数据库。大家需要的很多基本面数据都可以获得。

r语言如何获取股票

获取美国城市CPI和中国M2数据

r语言如何获取股票

> getSymbols(+ Symbols = c("CPIAUCSL","MYAGM2CNM189N"),+ src = "FRED",+ from = "2018-01-01",+ to = "2020-02-24",+ index.class = "POSIXct"+ )[1] "CPIAUCSL" "MYAGM2CNM189N"> head(CPIAUCSL) CPIAUCSL1947-01-01 21.481947-02-01 21.621947-03-01 22.001947-04-01 22.001947-05-01 21.951947-06-01 22.08> plot(CPIAUCSL)> head(MYAGM2CNM189N) MYAGM2CNM189N1998-12-01 1.044985e+131999-01-01 1.055003e+131999-02-01 1.077784e+131999-03-01 1.084382e+131999-04-01 1.092183e+131999-05-01 1.100609e+13> plot(MYAGM2CNM189N)r语言如何获取股票r语言如何获取股票


感谢阅读,欢迎关注和留言

量化投资与期货外汇散仙,基金保险水平也拿的出手


r语言如何获取股票
标签:财经问答