股票价格中后取复权的计算

后复权

import pandas as pd
pd.set_option('expand_frame_repr',False)
df = pd.read_csv('.\data\sz300001.csv',encoding='gbk',)
df = df[['date','code','open','high','low','close','涨跌幅']]
df.sort_values(by=['date'],inplace=True)
df['涨跌幅2'] = df['close'].pct_change() #根据当日价格/前一个交易日价格 获得的涨幅(在复权日会产生错误)
# print(df1[abs(df1['涨跌幅2'] - df1['涨跌幅'])>0.001]) # 打印除权日的信息
df['复权因子'] = (df['涨跌幅']+1).cumprod() #基于真实涨跌幅得出,后复权价 = 上市价格*复权因子 --->后复权价
#上市价格计算
init_price = df.iloc[0]['close']/(1+df.iloc[0]['涨跌幅']) #上市价格23.800006382728984
#后复权价计算
df['fq_close'] = df['复权因子']*init_price
#根据等式: fq_open/fq_close = open/close ==> open_fq = (open/close)*fq_close
df['fq_open'] = ((df['open']/df['close'])*df['fq_close'])

print(df)

前复权

import pandas as pd
pd.set_option('expand_frame_repr',False)


#读入数据
df = pd.read_csv('.\data\sh600000p1.csv',encoding='gbk')
df2 =pd.read_csv('.\data\sh600000p2.csv',encoding='gbk')
#合并
df = df.append(df2)

#去重、更新列名、排序、重置索引
df.drop_duplicates(subset=['交易日期'],inplace=True)
df = df[['交易日期','股票代码','开盘价','收盘价','涨跌幅']]
# 批量替换列名,原来的列名太长了,容易让数据无法对齐,不利于观察
df.columns = ['date','code','open','close','ad']
# df.sort_values(by=['date'], inplace=True)
df.reset_index(inplace=True, drop=True)
#开始复权:计算复权因子
# df['复权因子'] = (df['涨跌幅'] + 1).cumprod()
# apd(复权价):after price divisor
# df['apd'] = (df['ad']+1).cumprod()

#获取当日收盘价 df['close']
#前复权因子:(1/(1+今日涨跌幅)) ==> 1/(1+df['ad])
#昨日收盘价 = 当日收盘价 * 复权因子

df.sort_values(by=['date'], ascending=0)  #倒序处理,因为找不到累除函数
df['apd'] = (1/(1+df['ad'])).cumprod()*df.iloc[0]['close'] #前复权

print(df)

备注:ad为英文的涨跌幅,读取数据时已经把标题处理成英文了

csv文档链接:
链接:https://pan.baidu.com/s/1vzqGYLqi2IzoJK_yPQ9-9A
提取码:ytrb

你可能感兴趣的:(数据分析与量化交易,pandas,量化交易)