[Financial Quantification] Python realizes the calculation and visualization of the cumulative rate of return based on the rate of return

insert image description here

1 theory

Financial products (principal 100 yuan)

Day 1: 3% : (1+3%) ✖ 100 = 103
Day 2: 2% : (1+2%) ✖ Above = 103 +2.06
Day 3: 5% : (1+5%) ✖ Above = Profit ✖ Above
Day 4: 6%: (1+6%) ✖ Above = Profit ✖ Above
... Accumulated income = cumulative product of (1+day’s yield)-1
Why does the calculation formula here need to subtract 1? ? Because our above formulas all include the principal, for example, 103 should be subtracted from 100, only 3 yuan is our profit, so here we need to subtract 1 to delete the principal

2 achieve

import pandas as pd
# 计算累计收益率
def calculate_cum_prof(data):
    data['cum_profit'] = pd.DataFrame(1+data['profit_pct']).cumprod()-1
    data['cum_profit'].plot()
    return data

Guess you like

Origin blog.csdn.net/weixin_43935696/article/details/132219703