Search code examples
pythonpandasfinance

Pandas Python - Creating an indexed performance tracker


It's my first time posting so thanks a million in advance for any solutions to my problem!

I am trying to create a function which takes fund_name as an argument and creates an indexed performance tally (starting at 100) in a new column within the fund_name df. The df input is similar to this image.

and the desired output can be seen in this image.

The code I have used is below, but results in the index beginning at zero rather than 100:

def test(fund_name):
   fund_name['Indexed_Performance'] = ''
   fund_name['Indexed_Performance'] = pd.to_numeric(fund_name['Indexed_Performance'])
   fund_name.loc[0, 'Indexed_Performance'] = fund_name.loc[0, 'Monthly_Performance']
   for i in range(1, len(fund_name)):
       if fund_name.loc[i, 'Indexed_Performance'] < 10:
           fund_name.loc[i, 'Indexed_Performance'] = 100 * (fund_name.loc[i, 'Monthly_Performance'] + 1)
       else:
           fund_name.loc[i, 'Indexed_Performance'] = fund_name.loc[i-1, 'Indexed_Performance'] * (fund_name.loc[i, 'Monthly_Performance'] + 1)

Thanks again in advance for any solutions to this problem!


Solution

  • I am not sure what you mean exactly, especially in the if statement, but maybe it's as simple as this?

    import numpy as np
    
    def test(df):
        perfs = 1 + df['Monthly_Performance']
        df['Indexed_Performance'] = np.cumprod(perfs) * 100  # Cumulative product of ratios
    

    which outputs

        Monthly_Performance  Indexed_Performance
    0                 -0.05            95.000000
    1                  0.10           104.500000
    2                  0.02           106.590000
    3                 -0.06           100.194600
    4                  0.00           100.194600
    5                 -0.04            96.186816
    6                  0.07           102.919893
    7                 -0.07            95.715501
    8                 -0.01            94.758346
    9                  0.07           101.391430
    10                 0.10           111.530573
    11                 0.07           119.337713
    12                 0.06           126.497976
    13                -0.03           122.703036
    14                 0.10           134.973340
    15                -0.04           129.574406
    16                -0.03           125.687174
    17                -0.06           118.145944
    18                 0.06           125.234700
    

    If not can you give more details about how you compute your performance index ?