|
import streamlit as st |
|
import numpy as np |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
def calculate_pv(cashflows, rate): |
|
pv = sum(cf / (1 + rate) ** i for i, cf in enumerate(cashflows)) |
|
return pv |
|
|
|
|
|
st.title('Cashflow Present Value Calculator') |
|
|
|
|
|
rate = st.slider('Discount Rate (%)', min_value=0.0, max_value=100.0, value=15.0, step=0.1)/100 |
|
|
|
|
|
n = st.number_input("How many years?", min_value = 0, step=1) |
|
|
|
|
|
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(',')]) |
|
|
|
|
|
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) |
|
|