File size: 1,490 Bytes
a2323bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# Function to compute present value
def calculate_pv(cashflows, rate):
    pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows))
    return pv

# Streamlit app layout
st.title('Cashflow Present Value Calculator')

# Input for the discount rate
rate = st.slider('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100

# Inputs for the number of years
n = st.number_input("How many years?", min_value = 0, step=1)

# Inputs for the cashflows
cf_list = [0] * (n+1)
cf_dict = pd.DataFrame({"cashflows": cf_list}).astype(int)
cf_df = st.text_area("Enter Cashflows (comma-separated)", value=', '.join(map(str, cf_list)))

cashflows = np.array([float(cf.strip()) for cf in cf_df.split(',')])

# Button to calculate PV
if st.button('Calculate Present Value'):
    pv = calculate_pv(cashflows, rate)
    st.write(f'The Present Value is: ${pv:,.2f}')
    
    # Plotting cashflow diagram
    fig, ax = plt.subplots(figsize=(8, 6))
    colors = ['green' if cf > 0 else 'red' for cf in cashflows]
    ax.plot(range(n+1), cashflows, marker='o', color='b', linestyle='-')
    ax.fill_between(range(n+1), cashflows, color=colors, alpha=0.3)
    ax.set_title('Cashflow Diagram')
    ax.set_xlabel('Period')
    ax.set_ylabel('Cashflow')
    for i, cf in enumerate(cashflows):
        ax.text(i, cf, f'{cf:,.2f}', ha='center', va='bottom')
    ax.grid(True)
    st.pyplot(fig)