目标改成 ROE
新手教程—收益率计算¶
以下内容主要介绍收益率计算
1 收益率计算¶
#导入需要的程序包
import pandas as pd
import seaborn as sns
df = get_price(get_industry_stocks('A01'), fields=('close',))['close']
df.head()
rets = df/df.shift(1) - 1#shift起平移作用
rets.head()
returns = df.pct_change().dropna()
returns.head()
# pandas.ols在0.20已被移除
## 2 移动窗口回归
# 使用普通最小二乘法(OLS)拟合曲线,得到回归系数及各类参数
# y = returns['300087.XSHE']
# x = returns.ix[:, ['300189.XSHE']]
# model = pd.ols(y=y, x=x)
# model
# model = pd.ols(y=y, x=x, window=5)
# model.beta.info()
# model.beta['300189.XSHE'].plot()
# 画出移动平均线、指数平滑移动平均线进行分析
df = get_price(get_industry_stocks('A01'), fields=('close',))['close']
plt.figure(figsize=[18,5])
df['000998.XSHE'].plot()
pd.rolling_mean(df['000998.XSHE'],20).plot(label='20 day moving average')
pd.rolling_mean(df['000998.XSHE'],5).plot(label='5 day moving average')
plt.legend(loc='best')
df = get_price(get_industry_stocks('A01'), fields=('close',))['close']
plt.figure(figsize=[18,5])
df['000998.XSHE'].plot()
pd.rolling_mean(df['000998.XSHE'],20).plot(label='5 day moving average')
pd.ewma(df['000998.XSHE'],5).plot(label='5 day exponential moving average')
plt.legend(loc='best')
df = get_price(get_industry_stocks('A01'), fields=('close',))['close']
plt.figure(figsize=[18,5])
df['000998.XSHE'].plot()
pd.ewma(df['000998.XSHE'],20).plot(label='20 day exponential moving average')
pd.ewma(df['000998.XSHE'],5).plot(label='5 day exponential moving average')
plt.legend(loc='best')