资产收益计算
import numpy as np
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
%matplotlib inline
from scipy import stats
import matplotlib
import tushare as ts
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['font.family']='sans-serif'
matplotlib.rcParams['axes.unicode_minus'] = False
通过接口导入茅台股2019至今的数据
pro = ts.pro_api('my_token')
df = pro.daily(ts_code='600519.SH', start_date='20190101', end_date='20210220')
stock = df.set_index(["trade_date"])
stock.head(10)
|
ts_code |
open |
high |
low |
close |
pre_close |
change |
pct_chg |
vol |
amount |
trade_date |
|
|
|
|
|
|
|
|
|
|
20210219 |
600519.SH |
2451.16 |
2496.66 |
2381.6 |
2460.00 |
2471.00 |
-11.00 |
-0.4452 |
59385.46 |
1.452516e+07 |
20210218 |
600519.SH |
2587.98 |
2627.88 |
2465.0 |
2471.00 |
2601.00 |
-130.00 |
-4.9981 |
65912.32 |
1.670414e+07 |
20210210 |
600519.SH |
2485.00 |
2601.20 |
2485.0 |
2601.00 |
2456.43 |
144.57 |
5.8854 |
61370.57 |
1.567593e+07 |
20210209 |
600519.SH |
2368.80 |
2456.43 |
2350.0 |
2456.43 |
2368.80 |
87.63 |
3.6993 |
33296.55 |
7.972930e+06 |
20210208 |
600519.SH |
2337.00 |
2378.88 |
2313.0 |
2368.80 |
2313.00 |
55.80 |
2.4125 |
35785.73 |
8.420759e+06 |
20210205 |
600519.SH |
2325.00 |
2364.60 |
2291.0 |
2313.00 |
2320.85 |
-7.85 |
-0.3382 |
39729.06 |
9.253566e+06 |
20210204 |
600519.SH |
2191.00 |
2330.00 |
2191.0 |
2320.85 |
2189.91 |
130.94 |
5.9792 |
63851.01 |
1.459693e+07 |
20210203 |
600519.SH |
2150.00 |
2198.27 |
2140.0 |
2189.91 |
2145.00 |
44.91 |
2.0937 |
38714.15 |
8.384049e+06 |
20210202 |
600519.SH |
2112.22 |
2149.99 |
2102.1 |
2145.00 |
2109.32 |
35.68 |
1.6915 |
34582.25 |
7.372234e+06 |
20210201 |
600519.SH |
2130.00 |
2160.00 |
2095.0 |
2109.32 |
2116.18 |
-6.86 |
-0.3242 |
29340.59 |
6.219639e+06 |
获取收盘价列并错位合并求单期收益率
获取收盘价列
close=stock.close
close.head(10)
trade_date
20210219 2460.00
20210218 2471.00
20210210 2601.00
20210209 2456.43
20210208 2368.80
20210205 2313.00
20210204 2320.85
20210203 2189.91
20210202 2145.00
20210201 2109.32
Name: close, dtype: float64
索引设置为时间列
close.index=pd.to_datetime(close.index)
close.index.name="Date"
close.head(10)
Date
2021-02-19 2460.00
2021-02-18 2471.00
2021-02-10 2601.00
2021-02-09 2456.43
2021-02-08 2368.80
2021-02-05 2313.00
2021-02-04 2320.85
2021-02-03 2189.91
2021-02-02 2145.00
2021-02-01 2109.32
Name: close, dtype: float64
下移得到延迟一期收盘价
lagclose=close.shift(1)
lagclose.head(10)
Date
2021-02-19 NaN
2021-02-18 2460.00
2021-02-10 2471.00
2021-02-09 2601.00
2021-02-08 2456.43
2021-02-05 2368.80
2021-02-04 2313.00
2021-02-03 2320.85
2021-02-02 2189.91
2021-02-01 2145.00
Name: close, dtype: float64
close.head(10)
Date
2021-02-19 2460.00
2021-02-18 2471.00
2021-02-10 2601.00
2021-02-09 2456.43
2021-02-08 2368.80
2021-02-05 2313.00
2021-02-04 2320.85
2021-02-03 2189.91
2021-02-02 2145.00
2021-02-01 2109.32
Name: close, dtype: float64
lagclose.head(10)
Date
2021-02-19 NaN
2021-02-18 2460.00
2021-02-10 2471.00
2021-02-09 2601.00
2021-02-08 2456.43
2021-02-05 2368.80
2021-02-04 2313.00
2021-02-03 2320.85
2021-02-02 2189.91
2021-02-01 2145.00
Name: close, dtype: float64
将收盘价与延迟一期收盘价合并为表格
CalcClose=pd.DataFrame({
"close":close,"lagclose":lagclose})
CalcClose.head(10)
|
close |
lagclose |
Date |
|
|
2021-02-19 |
2460.00 |
NaN |
2021-02-18 |
2471.00 |
2460.00 |
2021-02-10 |
2601.00 |
2471.00 |
2021-02-09 |
2456.43 |
2601.00 |
2021-02-08 |
2368.80 |
2456.43 |
2021-02-05 |
2313.00 |
2368.80 |
2021-02-04 |
2320.85 |
2313.00 |
2021-02-03 |
2189.91 |
2320.85 |
2021-02-02 |
2145.00 |
2189.91 |
2021-02-01 |
2109.32 |
2145.00 |
计算延迟一期收益率
dayret1=(close-lagclose)/lagclose
dayret1.name="dayret1"
dayret1.head(10)
Date
2021-02-19 NaN
2021-02-18 0.004472
2021-02-10 0.052610
2021-02-09 -0.055582
2021-02-08 -0.035674
2021-02-05 -0.023556
2021-02-04 0.003394
2021-02-03 -0.056419
2021-02-02 -0.020508
2021-02-01 -0.016634
Name: dayret1, dtype: float64
计算多期收益
将收盘价、延迟一期收盘价及收益率合并为一个表格
calcret=pd.merge(CalcClose