R语言用quantstrat包获取股票、债券、基金、黄金、原油、指数、外汇和全球经济数据

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

载入需要的程辑包:TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
载入需要的程辑包:blotter
载入需要的程辑包:FinancialInstrument
载入需要的程辑包:PerformanceAnalytics

载入程辑包:‘PerformanceAnalytics’

The following object is masked from ‘package:graphics’:

    legend

载入需要的程辑包:foreach

yahoo的证券数据

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

> 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.Adjusted
2018-01-02    170.16    172.30   169.26     172.26    25555900      166.8040
2018-01-03    172.53    174.55   171.96     172.23    29517900      166.7750
2018-01-04    172.54    173.47   172.08     173.03    22434600      167.5496
2018-01-05    173.44    175.37   173.05     175.00    23660000      169.4572
2018-01-08    174.35    175.61   173.93     174.35    20567800      168.8278
2018-01-09    174.55    175.06   173.41     174.33    21584000      168.8085
> chartSeries(AAPL)

获取工商银行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.Adjusted
2018-01-02           6.19           6.39          6.11            6.18        537466207           5.667023
2018-01-03           6.18           6.32          6.12            6.16        349361124           5.648683
2018-01-04           6.18           6.18          6.06            6.07        435136522           5.566154
2018-01-05           6.09           6.11          6.06            6.08        281784265           5.575324
2018-01-08           6.09           6.10          6.04            6.07        218487604           5.566154
2018-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.Adjusted
2018-01-02         6.32         6.48        6.32          6.48      424057742         5.860297
2018-01-03         6.50         6.54        6.46          6.50      411577497         5.878385
2018-01-04         6.51         6.66        6.49          6.57      391732554         5.941690
2018-01-05         6.62         6.62        6.50          6.56      291961164         5.932646
2018-01-08         6.62         6.62        6.53          6.55      256489358         5.923603
2018-01-09         6.59         6.59        6.50          6.50      290694432         5.878385
> chartSeries(`1398.HK`)
> chartSeries(`601398.SS`)

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

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.Adjusted
2018-01-02 24809.35 24864.19 24741.70  24824.01  341130000     24824.01
2018-01-03 24850.45 24941.92 24825.55  24922.68  456790000     24922.68
2018-01-04 24964.86 25105.96 24963.27  25075.13  403280000     25075.13
2018-01-05 25114.92 25299.79 25112.01  25295.87  358020000     25295.87
2018-01-08 25308.40 25311.99 25235.41  25283.00  341390000     25283.00
2018-01-09 25312.05 25439.78 25308.41  25385.80  333490000     25385.80
> chartSeries(DJI)

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.Adjusted
2018-01-02       3314.031       3349.053      3314.031        3348.326           202300           3348.326
2018-01-03       3347.743       3379.915      3345.289        3369.108           213800           3369.108
2018-01-04       3371.000       3392.826      3365.295        3385.710           207000           3385.710
2018-01-05       3386.464       3402.069      3380.245        3391.750           213100           3391.750
2018-01-08       3391.553       3412.731      3384.559        3409.480           236200           3409.480
2018-01-09       3406.112       3417.228      3403.587        3413.900           191500           3413.900
> chartSeries(`000001.SS`)

获取比特币数据

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.Adjusted
2018-01-01      14112.2      14112.2     13154.7       13657.2    10291200000          13657.2
2018-01-02      13625.0      15444.6     13163.6       14982.1    16846600192          14982.1
2018-01-03      14978.2      15572.8     14844.5       15201.0    16871900160          15201.0
2018-01-04      15270.7      15739.7     14522.2       15599.2    21783199744          15599.2
2018-01-05      15477.2      17705.2     15202.8       17429.5    23840899072          17429.5
2018-01-06      17462.1      17712.4     16764.6       17527.0    18314600448          17527.0
> chartSeries(`BTC-USD`)

获取黄金价格数据

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.Adjusted
2019-12-30    1517.7    1518.4   1517.5     1517.8     3597128        1517.8
2019-12-31    1526.5    1529.0   1519.2     1520.0   117342599        1520.0
2020-01-02    1521.9    1534.0   1520.9     1531.2   138920019        1531.2
2020-01-03    1541.4    1556.6   1540.9     1555.2   252835424        1555.2
2020-01-05    1562.7    1590.9   1562.3     1571.3     1601769        1571.3
2020-01-06    1578.4    1581.5   1562.7     1565.9   351767794        1565.9
> chartSeries(`GC=F`)

获取原油价格数据

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.Adjusted
2020-01-24     54.39     54.41    54.15      54.20    34447364         54.20
2020-01-26     53.70     53.71    52.15      52.64     1009546         52.64
2020-01-27     52.89     53.54    52.13      52.97   332069958         52.97
2020-01-28     53.08     54.08    52.68      53.82   279037102         53.82
2020-01-29     54.14     54.37    52.81      53.10   277368615         53.10
2020-01-30     52.79     52.99    51.67      52.85   308221904         52.85
> chartSeries(`CL=F`)

R语言用quantstrat包获取股票、债券、基金、黄金、原油、指数、外汇和全球经济数据_第1张图片

获取债券价格数据

美国十年期国债

> 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.Adjusted
2018-01-02    2.433    2.480   2.425     2.465          0        2.465
2018-01-03    2.451    2.465   2.440     2.447          0        2.447
2018-01-04    2.473    2.487   2.449     2.453          0        2.453
2018-01-05    2.465    2.480   2.436     2.476          0        2.476
2018-01-07       NA       NA      NA        NA         NA           NA
2018-01-08    2.471    2.489   2.462     2.480          0        2.480
> chartSeries(TNX)

获取外汇价格数据

欧元兑美元

> 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.Adjusted
2018-01-01      1.200495      1.201504     1.199904       1.200495               0          1.200495
2018-01-02      1.201086      1.208094     1.200855       1.201158               0          1.201158
2018-01-03      1.206200      1.206709     1.200495       1.206345               0          1.206345
2018-01-04      1.201129      1.209190     1.200495       1.201043               0          1.201043
2018-01-05      1.206622      1.208459     1.202154       1.206884               0          1.206884
2018-01-08      1.203949      1.205400     1.195972       1.203746               0          1.203746
> chartSeries(`EURUSD=X`)

获取基金数据

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.Adjusted
2018-01-02      29.60      29.60     29.60       29.60            0       23.86189
2018-01-03      29.91      29.91     29.91       29.91            0       24.11180
2018-01-04      30.08      30.08     30.08       30.08            0       24.24884
2018-01-05      30.36      30.36     30.36       30.36            0       24.47456
2018-01-08      30.49      30.49     30.49       30.49            0       24.57936
2018-01-09      30.56      30.56     30.56       30.56            0       24.63579
> chartSeries(VMRGX)

FRED的经济数据

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

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

> getSymbols(
+     Symbols = c("CPIAUCSL","MYAGM2CNM189N"),
+     src = "FRED",
+     from = "2018-01-01",
+     to = "2020-02-24",
+     index.class = "POSIXct"
+ )
[1] "CPIAUCSL"      "MYAGM2CNM189N"
> head(CPIAUCSL)
           CPIAUCSL
1947-01-01    21.48
1947-02-01    21.62
1947-03-01    22.00
1947-04-01    22.00
1947-05-01    21.95
1947-06-01    22.08
> plot(CPIAUCSL)
> head(MYAGM2CNM189N)
           MYAGM2CNM189N
1998-12-01  1.044985e+13
1999-01-01  1.055003e+13
1999-02-01  1.077784e+13
1999-03-01  1.084382e+13
1999-04-01  1.092183e+13
1999-05-01  1.100609e+13
> plot(MYAGM2CNM189N)

感谢阅读,欢迎关注和留言
量化投资与期货外汇散仙,基金保险水平也拿的出手

你可能感兴趣的:(人工智能)