数据获取
import pandas_datareader as pdralibaba = pdr.get_data_yahoo('BABA')alibaba.head()
结果:
alibaba.shape#(789, 6)alibaba.tail()
结果:
alibaba.describe()#汇总信息,基本信息
结果:
alibaba.info()#也是基本信息
结果:
<class 'pandas.core.frame.DataFrame'>DatetimeIndex: 789 entries, -09-19 to -11-03Data columns (total 6 columns):Open 789 non-null float64High 789 non-null float64Low789 non-null float64Close 789 non-null float64Adj Close 789 non-null float64Volume 789 non-null int64dtypes: float64(5), int64(1)memory usage: 43.1 KB
View Code
历史趋势分析
# 基本信息import numpy as npimport pandas as pdfrom pandas import Series, DataFrame# 股票数据的读取import pandas_datareader as pdr# 可视化import matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline# timefrom datetime import datetime
start = datetime(,9,20)alibaba = pdr.get_data_yahoo('BABA', start=start)amazon = pdr.get_data_yahoo('AMZN', start=start)
alibaba.to_csv('../homework/BABA.csv')amazon.to_csv('../homework/AMZN.csv')
alibaba.head()
结果:
alibaba['Adj Close'].plot(legend=True)
结果:<matplotlib.axes._subplots.AxesSubplot at 0x11d910588>
alibaba['Volume'].plot(legend=True)
结果:<matplotlib.axes._subplots.AxesSubplot at 0x120ebcda0>
alibaba['Adj Close'].plot()amazon['Adj Close'].plot()
结果:<matplotlib.axes._subplots.AxesSubplot at 0x120ee9a90>
alibaba.head()
结果:
alibaba['high-low'] = alibaba['High'] - alibaba['Low']alibaba.head()
结果:
alibaba['high-low'].plot()
结果:<matplotlib.axes._subplots.AxesSubplot at 0x120fdcf98>
# daily return 每天变化情况alibaba['daily-return'] = alibaba['Adj Close'].pct_change() #.pct_change() 实现每天每天变化情况alibaba['daily-return'].plot(figsize=(10,4),linestyle='--',marker='o')#figsize图大小,marker每个数据点类型
结果:<matplotlib.axes._subplots.AxesSubplot at 0x121504d68>
alibaba['daily-return'].plot(kind='hist')#可以看出,基本上在-0.025到.025之间,所以股票价格稳定
结果:<matplotlib.axes._subplots.AxesSubplot at 0x12169c630>
sns.distplot(alibaba['daily-return'].dropna(),bins=100,color='purple')
结果:<matplotlib.axes._subplots.AxesSubplot at 0x121b15f98>
风险分析
# 基本信息import numpy as npimport pandas as pdfrom pandas import Series, DataFrame# 股票数据的读取import pandas_datareader as pdr# 可视化import matplotlib.pyplot as pltimport seaborn as sns%matplotlib inline# timefrom datetime import datetime
start = datetime(,1,1)company = ['AAPL','GOOG','MSFT','AMZN','FB']#5家公司top_tech_df = pdr.get_data_yahoo(company, start=start)['Adj Close']
top_tech_df.to_csv('../homework/top5.csv')
top_tech_df.head()
结果:
top_tech_dr = top_tech_df.pct_change()#股票数据每天的变化top_tech_dr.head()
结果:
top_tech_df.plot()#GOOG与AMZN曲线比较相似
结果:<matplotlib.axes._subplots.AxesSubplot at 0x122293fd0>
top_tech_df[['AAPL','FB','MSFT']].plot()#画出其中三列
结果:<matplotlib.axes._subplots.AxesSubplot at 0x1225960b8>
sns.jointplot('AMZN','GOOG', top_tech_dr, kind='scatter')#kind='scatter'表示散点图;看点是否在一条斜率为45度的直线上来观察两家股票的相关性,注意坐标大小代表的是上升或者下降
结果:<seaborn.axisgrid.JointGrid at 0x124dd0f98>
sns.jointplot('MSFT','FB', top_tech_dr, kind='scatter')
结果:<seaborn.axisgrid.JointGrid at 0x125928940>
sns.pairplot(top_tech_dr.dropna())#.dropna()表示去掉NaN
结果:<seaborn.axisgrid.PairGrid at 0x125fa27f0>
top_tech_dr['AAPL'].quantile(0.52)#结果:-0.0001447090809730694
top_tech_dr['MSFT'].quantile(0.05)#有(1-%5)的信心每天的变化最大为-0.021328913487084877 (最大亏损为%2)
结果:-0.021328913487084877
vips = pdr.get_data_yahoo('VIPS', start=start)['Adj Close']vips.plot()
结果:<matplotlib.axes._subplots.AxesSubplot at 0x127b0f7b8>
vips.pct_change().quantile(0.2)#-0.02181125767068397