繁簡切換您正在訪問的是FX168財經網,本網站所提供的內容及信息均遵守中華人民共和國香港特別行政區當地法律法規。

FX168财经网>人物频道>帖子

银行 PE轮动

作者/sdeewew 2019-07-29 20:33 0 来源: FX168财经网人物频道

目标改成 ROE

新手教程—收益率计算¶

以下内容主要介绍收益率计算

1 收益率计算¶

#导入需要的程序包
import pandas as pd
import seaborn as sns
df = get_price(get_industry_stocks('A01'), fields=('close',))['close']
df.head()
000998.XSHE 002041.XSHE 002772.XSHE 300087.XSHE 300189.XSHE 300511.XSHE 600108.XSHG 600313.XSHG 600354.XSHG 600359.XSHG 600371.XSHG 600506.XSHG 600540.XSHG 600598.XSHG 601118.XSHG
2015-01-05 19.44 12.71 NaN 4.00 2.71 NaN 9.51 4.50 8.81 10.92 10.55 11.33 6.28 9.34 8.88
2015-01-06 19.84 12.92 NaN 4.13 2.78 NaN 9.75 4.62 8.75 11.08 10.72 11.62 6.39 9.65 9.15
2015-01-07 19.68 12.85 NaN 4.08 2.76 NaN 9.98 4.70 8.85 11.07 10.70 11.54 6.37 9.68 9.03
2015-01-08 20.22 13.03 NaN 4.18 2.77 NaN 9.77 4.74 8.89 11.12 10.76 11.81 6.39 9.70 8.85
2015-01-09 19.86 12.87 NaN 4.06 2.72 NaN 9.41 4.59 8.60 10.80 10.47 11.73 6.30 9.58 8.61
rets = df/df.shift(1) - 1#shift起平移作用
rets.head()
000998.XSHE 002041.XSHE 002772.XSHE 300087.XSHE 300189.XSHE 300511.XSHE 600108.XSHG 600313.XSHG 600354.XSHG 600359.XSHG 600371.XSHG 600506.XSHG 600540.XSHG 600598.XSHG 601118.XSHG
2015-01-05 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-06 0.020576 0.016522 NaN 0.032500 0.025830 NaN 0.025237 0.026667 -0.006810 0.014652 0.016114 0.025596 0.017516 0.033191 0.030405
2015-01-07 -0.008065 -0.005418 NaN -0.012107 -0.007194 NaN 0.023590 0.017316 0.011429 -0.000903 -0.001866 -0.006885 -0.003130 0.003109 -0.013115
2015-01-08 0.027439 0.014008 NaN 0.024510 0.003623 NaN -0.021042 0.008511 0.004520 0.004517 0.005607 0.023397 0.003140 0.002066 -0.019934
2015-01-09 -0.017804 -0.012279 NaN -0.028708 -0.018051 NaN -0.036847 -0.031646 -0.032621 -0.028777 -0.026952 -0.006774 -0.014085 -0.012371 -0.027119
returns = df.pct_change().dropna()
returns.head()
000998.XSHE 002041.XSHE 002772.XSHE 300087.XSHE 300189.XSHE 300511.XSHE 600108.XSHG 600313.XSHG 600354.XSHG 600359.XSHG 600371.XSHG 600506.XSHG 600540.XSHG 600598.XSHG 601118.XSHG
# 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')
<matplotlib.legend.Legend at 0x7f20e9e4bad0>
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')
<matplotlib.legend.Legend at 0x7f20e9a59a90>
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')
<matplotlib.legend.Legend at 0x7f20e9a7a2d0>
 
分享到:
举报财经168客户端下载

全部回复

0/140

投稿 您想发表你的观点和看法?

更多人气分析师

  • 张亦巧

    人气2192文章4145粉丝45

    暂无个人简介信息

  • 王启蒙现货黄金

    人气296文章3215粉丝8

    本人做分析师以来,并专注于贵金属投资市场,尤其是在现货黄金...

  • 指导老师

    人气1864文章4423粉丝52

    暂无个人简介信息

  • 李冉晴

    人气2320文章3821粉丝34

    李冉晴,专业现贷实盘分析师。

  • 梁孟梵

    人气2176文章3177粉丝39

    qq:2294906466 了解群指导添加微信mfmacd

  • 张迎妤

    人气1896文章3305粉丝34

    个人专注于行情技术分析,消息面解读剖析,给予您第一时间方向...

  • 金泰铬J

    人气2328文章3925粉丝51

    投资问答解咨询金泰铬V/信tgtg67即可获取每日的实时资讯、行情...

  • 金算盘

    人气2696文章7761粉丝125

    高级分析师,混过名校,厮杀于股市和期货、证券市场多年,专注...

  • 金帝财神

    人气4760文章8329粉丝119

    本文由资深分析师金帝财神微信:934295330,指导黄金,白银,...

FX168财经

FX168财经学院

FX168财经

FX168北美