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

pic

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 20220609 8.23 8.28 8.19 8.23 8.25 -0.02 -0.2424 52379.64 43138.404
1 600377.SH 20220608 8.22 8.27 8.15 8.25 8.19 0.06 0.7326 65288.11 53543.805
2 600377.SH 20220607 8.13 8.21 8.08 8.19 8.13 0.06 0.7380 81555.11 66554.661
3 600377.SH 20220606 8.36 8.39 8.11 8.13 8.37 -0.24 -2.8674 118437.62 96760.291
4 600377.SH 20220602 8.41 8.41 8.33 8.37 8.41 -0.04 -0.4756 49257.52 41240.142

从上面可以看出,序号并不是以时间作为单位的。那么我们首先需要将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)
../_images/stock-return-calculation_6_2.png

下面将画出每日收盘价的百分比变化图:

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)
../_images/stock-return-calculation_8_1.png

从上面可以看出,由于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'>
../_images/stock-return-calculation_11_1.png
print('累计收益:',cumulative_rets[-1])
累计收益: 0.14216281895503435

对数累计收益(log returns)#

log_ret = np.log(returns+1)
cumulative_rets = log_ret.cumsum()
plt.figure(figsize=(15, 5))
plt.plot(cumulative_rets)
plt.show()
../_images/stock-return-calculation_14_0.png
print('累计收益:',cumulative_rets[-1])
累计收益: 0.1329236745869773

来源:cnVaR.cn#