Tushare Day6—— 第18章 资产收益计算(嘻嘻嘻)

本文通过Python的Tushare库和ffn包详细介绍了如何计算股票的单期、多期收益率,包括简单收益率、年化收益率和连续复利收益率,并探讨了资产风险的多种度量方法,如方差、下行偏差、VaR和最大回撤。
摘要由CSDN通过智能技术生成

资产收益计算

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) #设置第一行为nan.,数据往下移动
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值