各种股票收益率(returns)的计算方法
Contents
各种股票收益率(returns)的计算方法¶

Fig. 16 股票收益率¶
前言
我们一般对股票的理解一般只停留于各大交易软件如东方财富、通达信等所提供的基本信息。 股票的日线图除了告诉我们股价的走势之外,股民基本上将其用于MACD、KDJ等技术分析上。 事实上, 我们可以利用股票的历史价格可以算出股票的走势是否正态分布、 回报率(Returns)、波动率(Volatility)、偏度(skewness) 和 峰度(kurtosis)。
本文将介绍收益率的计算方法。
实现过程¶
import tushare as ts
import pandas as pd
import numpy as np
import scipy.stats
import matplotlib.pyplot as plt
plt.style.use('seaborn-white')
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
pro = ts.pro_api('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') #这里需要填写你注册好的Tushare的TOKEN凭证
通过调用tushare获取股票600377(宁沪高速)的股票数据,这里不设置日期,那么默认获取Tushare提供的历史数据。
ticker_data = pro.daily(ts_code='600377.SH')
print('数据数目:',len(ticker_data))
ticker_data.head(5)
数据数目: 5000
ts_code | trade_date | open | high | low | close | pre_close | change | pct_chg | vol | amount | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 600377.SH | 20220527 | 8.31 | 8.33 | 8.24 | 8.31 | 8.31 | 0.00 | 0.0000 | 56267.33 | 46660.525 |
1 | 600377.SH | 20220526 | 8.20 | 8.32 | 8.18 | 8.31 | 8.20 | 0.11 | 1.3415 | 65676.90 | 54313.733 |
2 | 600377.SH | 20220525 | 8.08 | 8.22 | 8.05 | 8.20 | 8.09 | 0.11 | 1.3597 | 60573.68 | 49462.806 |
3 | 600377.SH | 20220524 | 8.27 | 8.32 | 8.08 | 8.09 | 8.27 | -0.18 | -2.1765 | 48194.94 | 39510.515 |
4 | 600377.SH | 20220523 | 8.23 | 8.28 | 8.18 | 8.27 | 8.21 | 0.06 | 0.7308 | 56377.31 | 46416.005 |
从上面可以看出,序号并不是以时间作为单位的。那么我们首先需要将trade_date转为datetime格式,然后设置为序号以便于画图。
ticker_data['trade_date'] = pd.to_datetime(ticker_data['trade_date'],format='%Y%m%d')
ticker_data.set_index('trade_date', inplace=True)
returns = ticker_data["close"].pct_change().dropna()
plt.figure(figsize=(15, 5))
plt.title("股票代码:600377 - 宁沪高速", weight='bold')
ticker_data['close'].plot()
<AxesSubplot:title={'center':'股票代码:600377 - 宁沪高速'}, xlabel='trade_date'>
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20195 (\N{CJK UNIFIED IDEOGRAPH-4EE3}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30721 (\N{CJK UNIFIED IDEOGRAPH-7801}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 23425 (\N{CJK UNIFIED IDEOGRAPH-5B81}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 27818 (\N{CJK UNIFIED IDEOGRAPH-6CAA}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 39640 (\N{CJK UNIFIED IDEOGRAPH-9AD8}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 36895 (\N{CJK UNIFIED IDEOGRAPH-901F}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)

下面将画出每日收盘价的百分比变化图:
plt.figure(figsize=(15, 5))
ticker_data["close"].pct_change().plot()
plt.title("股票代码:600377 - 宁沪高速", weight='bold');
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 32929 (\N{CJK UNIFIED IDEOGRAPH-80A1}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 31080 (\N{CJK UNIFIED IDEOGRAPH-7968}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 20195 (\N{CJK UNIFIED IDEOGRAPH-4EE3}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 30721 (\N{CJK UNIFIED IDEOGRAPH-7801}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 23425 (\N{CJK UNIFIED IDEOGRAPH-5B81}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 27818 (\N{CJK UNIFIED IDEOGRAPH-6CAA}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 39640 (\N{CJK UNIFIED IDEOGRAPH-9AD8}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)
/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/IPython/core/pylabtools.py:151: UserWarning: Glyph 36895 (\N{CJK UNIFIED IDEOGRAPH-901F}) missing from current font.
fig.canvas.print_figure(bytes_io, **kw)

从上面可以看出,由于A股市场的每日涨跌幅限制,可以看到宁沪高速最大的涨跌幅为10%, 但其中有一个数据是超出10%(介于2016年至2017年坐标轴之间),估计是高开涨停。上图也可以理解为股票收益的波动图.
普通累计收益¶
plt.figure(figsize=(15, 5))
rets_add_one = returns + 1
cumulative_rets = rets_add_one.cumprod()-1
cumulative_rets.plot()
<AxesSubplot:xlabel='trade_date'>

print('累计收益:',cumulative_rets[-1])
累计收益: 0.13237063778579028