基金定投回测达到收益率清仓后重新定投计算

现在能够计算出基金定投的数据,现在问题不知道怎么确定清仓后重新计算收益率!

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

df = pd.read_excel('fund/110022.xlsx')[['date','close']]
df['date'] = pd.to_datetime(df['date'])

df['week'] = df['date'].dt.dayofweek + 1
df = df.set_index('date')
#df = df['20110523':]
df = df.resample('M').last()
df = df[df['week'].isin([1])]
money = 500 # 定投金额
fee = 0.001 # 手续费

rate_list = []
rate = 0
df['quote'] = df['close'].diff()
for i in range(len(df)):
    df['cum_quote'] = df['quote'].cumsum()
    if df.iloc[i]['cum_quote'] >= 0.2:
        rate_list.append(df.index[i])
        df.iloc[i]['cum_quote'] = 0
        df['cum_quote'] = df['quote'].cumsum()
    #rate_list.append(df.index[i])
rate_list
#df.head(50)

#sum_money = len(df) * money
#share = (money - money * fee) / df['close']
#end_close = df['close'][-1]
#end_money = share.sum() * end_close - sum_money

# 每周一定投250
#print(f'投入总期数:{len(df)}')
#print(f'投入总资金:{sum_money}')
#print(f'累计总份额:{round(share.sum(),2)}')
#print(f'盈利金额{round(end_money,2)}')
#df.to_excel('110022.xlsx',index=False)
from pylab import *
import pandas as pd
import matplotlib.pyplot as plt

mpl.rcParams['font.sans-serif'] = ['SimHei']

df = pd.read_excel('/sdcard/备份/161725.xlsx')
df = df.set_index(df['date'])
fee = 0.001
df['cum_money'] = df['money'].cumsum()
df['share'] = round((df['money'] - df['money'] * fee) /df['close'],2)
df['cum_share'] = df['share'].cumsum()
df['av_price'] = round(df['cum_money'] / df['cum_share'],4)
df['income'] = df['cum_money'].shift(1) * df['close'] - df['cum_money'].shift(1)
df['rate'] = df['income'] / df['cum_money'].shift(1)
df
# 画图
df[['close','av_price']].plot(marker='o')

有没有高手能帮忙解答一下感谢!