import datetime
import pandas_datareader.data as web
import numpy as np
import pandas as pd
start = datetime.datetime(2016, 1, 1) # or start = '1/1/2016' or '2016-1-1'
end = datetime.date.today()
# 从雅虎财经拉取的苹果股价
DAX = web.DataReader('^GDAXI', 'yahoo', start, end)
DAX.info()
DatetimeIndex: 963 entries, 2016-01-04 to 2019-10-22
Data columns (total 6 columns):
High 963 non-null float64
Low 963 non-null float64
Open 963 non-null float64
Close 963 non-null float64
Volume 963 non-null float64
Adj Close 963 non-null float64
dtypes: float64(6)
memory usage: 52.7 KB
DAX.tail()
Out[5]:
High Low ... Volume Adj Close
Date ...
2019-10-16 12698.370117 12603.830078 ... 99475900.0 12670.110352
2019-10-17 12814.490234 12647.860352 ... 99302200.0 12654.950195
2019-10-18 12700.379883 12603.169922 ... 88676100.0 12633.599609
2019-10-21 12787.349609 12646.280273 ... 80978200.0 12747.959961
2019-10-22 12791.309570 12730.700195 ... 79430700.0 12754.690430
[5 rows x 6 columns]
参数 | 格式 | 描述 |
---|---|---|
name | 字符串 | 数据集名称——通常是股票代码 |
data_source | 如“yahoo” | “yahoo”:Yahoo! Finance, ”google”:Google Finance, ”fred”:St. Louis FED (FRED), ”famafrench”:Kenneth French’s data library, ”edgar-index”:the SEC’s EDGAR Index |
start | 字符串/日期时间/None | 范围左界(默认”2010/1/1”) |
end | 字符串/日期时间/None | 范围右界(默认当天) |
为了更好地概览指数的历史,使用plot方法生成图表
DAX['Close'].plot(figsize=(6,5))
DAX['Return'] = np.log(DAX['Close'] / DAX['Close'].shift(1))
DAX.tail()
Out[15]:
High Low ... Adj Close Return
Date ...
2019-10-16 12698.370117 12603.830078 ... 12670.110352 0.003187
2019-10-17 12814.490234 12647.860352 ... 12654.950195 -0.001197
2019-10-18 12700.379883 12603.169922 ... 12633.599609 -0.001689
2019-10-21 12787.349609 12646.280273 ... 12747.959961 0.009011
2019-10-22 12791.309570 12730.700195 ... 12754.690430 0.000528
[5 rows x 7 columns]
补充:
- shift方法,它相对于索引向前或者向后移动Series或者DataFrame对象
- %time DAX[‘Return’] = np.log(DAX[‘Close’] / DAX[‘Close’].shift(1)) 可用于计算运行该行代码花费的时间
DAX[['Close', 'Return']].plot(subplots=True, style='b', figsize=(8, 5))
理论知识:
波动率聚集:
波动率不是长,期恒定的,既有高波动率时期(正收益率和负收益率都很高),也有低波动率时期
杠杆效应:
一般来说,波动性和股票市场收益是负相关的,当市场下跌时,波动性升高
#DataFrame/Series.rolling(window, min_periods=None, freq=None, center=False, win_type=None, axis=0)
#window:移动窗口的大小
DAX['42d'] = DAX['Close'].rolling(window=42).mean()
DAX['252d'] = DAX['Close'].rolling(window=252).mean()
DAX[['Close','42d','252d']].tail()
Out[16]:
Close 42d 252d
Date
2019-10-25 12894.509766 12325.037900 11760.310896
2019-10-28 12941.709961 12354.578148 11766.797363
2019-10-29 12939.620117 12380.786249 11773.698157
2019-10-30 12910.230469 12403.904111 11779.947165
2019-10-31 12905.269531 12426.558617 11786.367323
包含两种典型股价图表:
DAX[['Close','42d','252d']].plot(figsize=(8,6))
在期权交易者严重,对数收益率的移动历史标准差——移动历史波动率
更重要
import math
DAX['Mov_Vol'] = DAX['Return'].rolling(252).std() * math.sqrt(252)
DAX[['Close','Mov_Vol','Return']].plot(subplots=1, style='b', figsize=(8,6))