Chapter8.金融时间序列
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['font.serif'] = ['KaiTi']
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题,或者转换负号为字符串
sns.set_style("darkgrid",{
"font.sans-serif":['KaiTi', 'Arial']})
一、文章结构
1)经济数据
2)移动统计量,如:移动平均
3)相关性分析
4)高频数据
ps:本章节使用pandas 可以阅读《python for analysis》进一步了解
二、经济数据
#使用pandas读取数据更为方便
data = pd.read_csv(r"C:\Users\Administrator\Desktop\source\tr_eikon_eod_data.csv",index_col=0,parse_dates=True)
data #index_col 指定索引列为第几列 parse_dates 是否将类似时间的文本数据,解析为 date数据
AAPL.O | MSFT.O | INTC.O | AMZN.O | GS.N | SPY | .SPX | .VIX | EUR= | XAU= | GDX | GLD | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | ||||||||||||
2010-01-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.4323 | 1096.35 | NaN | NaN |
2010-01-04 | 30.572827 | 30.950 | 20.88 | 133.90 | 173.08 | 113.33 | 1132.99 | 20.04 | 1.4411 | 1120.00 | 47.71 | 109.80 |
2010-01-05 | 30.625684 | 30.960 | 20.87 | 134.69 | 176.14 | 113.63 | 1136.52 | 19.35 | 1.4368 | 1118.65 | 48.17 | 109.70 |
2010-01-06 | 30.138541 | 30.770 | 20.80 | 132.25 | 174.26 | 113.71 | 1137.14 | 19.16 | 1.4412 | 1138.50 | 49.34 | 111.51 |
2010-01-07 | 30.082827 | 30.452 | 20.60 | 130.00 | 177.67 | 114.19 | 1141.69 | 19.06 | 1.4318 | 1131.90 | 49.10 | 110.82 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
2018-06-25 | 182.170000 | 98.390 | 50.71 | 1663.15 | 221.54 | 271.00 | 2717.07 | 17.33 | 1.1702 | 1265.00 | 22.01 | 119.89 |
2018-06-26 | 184.430000 | 99.080 | 49.67 | 1691.09 | 221.58 | 271.60 | 2723.06 | 15.92 | 1.1645 | 1258.64 | 21.95 | 119.26 |
2018-06-27 | 184.160000 | 97.540 | 48.76 | 1660.51 | 220.18 | 269.35 | 2699.63 | 17.91 | 1.1552 | 1251.62 | 21.81 | 118.58 |
2018-06-28 | 185.500000 | 98.630 | 49.25 | 1701.45 | 223.42 | 270.89 | 2716.31 | 16.85 | 1.1567 | 1247.88 | 21.93 | 118.22 |
2018-06-29 | 185.110000 | 98.610 | 49.71 | 1699.80 | 220.57 | 271.28 | 2718.37 | 16.09 | 1.1683 | 1252.25 | 22.31 | 118.65 |
2216 rows × 12 columns
data.head()
AAPL.O | MSFT.O | INTC.O | AMZN.O | GS.N | SPY | .SPX | .VIX | EUR= | XAU= | GDX | GLD | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
Date | ||||||||||||
2010-01-01 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | 1.4323 | 1096.35 | NaN | NaN |
2010-01-04 | 30.572827 | 30.950 | 20.88 | 133.90 | 173.08 | 113.33 | 1132.99 | 20.04 | 1.4411 | 1120.00 | 47.71 | 109.80 |
2010-01-05 | 30.625684 | 30.960 | 20.87 | 134.69 | 176.14 | 113.63 | 1136.52 | 19.35 | 1.4368 | 1118.65 | 48.17 | 109.70 |
2010-01-06 | 30.138541 | 30.770 | 20.80 | 132.25 | 174.26 | 113.71 | 1137.14 | 19.16 | 1.4412 | 1138.50 | 49.34 | 111.51 |
2010-01-07 | 30.082827 | 30.452 | 20.60 | 130.00 | 177.67 | 114.19 | 1141.69 | 19.06 | 1.4318 | 1131.90 | 49.10 | 110.82 |
data.plot(figsize=(10,12),subplots=True) #横坐标自动按年划分,十分智能化
array([<matplotlib.axes._subplots.AxesSubplot object at 0x0F134390>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0F140690>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EF48AF0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EF65D90>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EEB2FF0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EECFCF0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EEDDF50>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EF071D0>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EF07750>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EF78A70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EFB7E70>,
<matplotlib.axes._subplots.AxesSubplot object at 0x0EFD5B30>],
dtype=object)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tmrZRX3H-1581857844288)(output_9_1.png)]
instruments = ['Apple Stock', 'Microsoft Stock',
'Intel Stock', 'Amazon Stock', 'Goldman Sachs Stock',
'SPDR S&P 500 ETF Trust',</