Search code examples
pythonmathstatisticsfinance

Perform a cross column calculation in Python


Context

I am trying to build a portfolio dashboard following this example, only instead of Excel, I am using Python. I am currently not sure how to conduct from 3:47 onwards, cross calculating to arrive at the next period balance.

Problem

Is there a way to conduct this in Python? I tried a for loop but it returned the same number iterated over the number of forward periods. Below is the example:

date_range = pd.date_range(start=today, periods=period_of_investments, freq=contribution_periods)
returns_port = 12
rs = []
balance_total = []
for one in range(len(date_range))):
    return_loss = (returns_port/period_of_investments)*capital_insert
    rs.append(return_loss)
    period_one_balance = capital_insert+return_loss
    period_two_return_loss = (returns_port/period_of_investments)*(period_one_balance + capital_insert)
    period_two_balance = period_one_balance + capital_insert + period_two_return_loss
    balance_total.append(period_two_balance)

Solution

  • I did not watch the video but I will explain how to write a Python code for the following problem, which is similar to the one in the video.
    Suppose you want to calculate the return of investment of a fixed monthly deposit for the next 20 years with a fixed interest rate.
    The first step is understanding how pd.date_range() works. If you started at the beginning of this month the whole period would be pd.date_rage(start='4-1-2021', periods='240', freq='1m') (240 comes from 20 years, 12 month each). Basically, we are calculating the return at the end of each month.

    import pandas as pd
    portfolio = pd.DataFrame(columns=['Date', 'Investment', 'Return/Loss', 'Balance'])
    interest_rate = 0.121
    monthly_deposit = 500
    
    dates = pd.date_range(start="3-31-2021", periods=240, freq='1m')
    investment = [monthly_deposit]*len(dates)
    return_losses = []
    balances = []
    current_balance = 500
    for date in dates:
        current_return_loss = (interest_rate/12)*current_balance
        return_losses.append(round(current_return_loss,2))
        balances.append(round(current_balance + current_return_loss))
        current_balance += (current_return_loss + monthly_deposit)
    
    portfolio['Date'] = pd.to_datetime(dates)
    portfolio['Investment'] = investment
    portfolio['Return/Loss'] = return_losses
    portfolio['Balance'] = balances
    
    balance_at_end = balances[-1]
    
    print(portfolio.head(10))
    print(balance_at_end)
    

    You will get the following result, which is identical to the video:

     Date  Investment  Return/Loss  Balance
    0 2021-03-31         500         5.04      505
    1 2021-04-30         500        10.13     1015
    2 2021-05-31         500        15.28     1530
    3 2021-06-30         500        20.47     2051
    4 2021-07-31         500        25.72     2577
    5 2021-08-31         500        31.02     3108
    6 2021-09-30         500        36.38     3644
    7 2021-10-31         500        41.79     4186
    8 2021-11-30         500        47.25     4733
    9 2021-12-31         500        52.77     5286
    506397