Write a code that will ask me these things: amount of gold bought in tola, date when bought, price per tola when bought and current price of tola and current date. Then will will calculate and output following things: total profit till today, average monthly earning, average daily earning. Also draw graph of daily and monthly earnings.
To upload files, please first save the app
import streamlit as st
import pandas as pd
from datetime import datetime
import plotly.express as px
import plotly.graph_objects as go
st.title("Gold Investment Calculator")
# Input section
col1, col2 = st.columns(2)
with col1:
gold_tola = st.number_input("Amount of gold bought (in tolas)", min_value=0.0, value=1.0)
purchase_date = st.date_input("Date when bought", value=datetime.now())
purchase_price = st.number_input("Price per tola when bought (in your currency)", min_value=0.0, value=100000.0)
with col2:
current_price = st.number_input("Current price per tola (in your currency)", min_value=0.0, value=120000.0)
current_date = st.date_input("Current date", value=datetime.now())
if st.button("Calculate"):
# Basic calculations
total_investment = gold_tola * purchase_price
current_value = gold_tola * current_price
total_profit = current_value - total_investment
# Calculate days and months between dates
days_diff = (current_date - purchase_date).days
months_diff = days_diff / 30.44 # average month length
# Calculate averages
daily_profit = total_profit / days_diff if days_diff > 0 else 0
monthly_profit = total_profit / months_diff if months_diff > 0 else 0
# Display results
st.header("Investment Analysis")
metrics_col1, metrics_col2, metrics_col3 = st.columns(3)
with metrics_col1:
st.metric("Total Profit", f"{total_profit:,.2f}")
with metrics_col2:
st.metric("Average Monthly Earning", f"{monthly_profit:,.2f}")
with metrics_col3:
st.metric("Average Daily Earning", f"{daily_profit:,.2f}")
# Create date range for plotting
date_range = pd.date_range(start=purchase_date, end=current_date)
# Create daily earnings dataframe
daily_df = pd.DataFrame({
'Date': date_range,
'Earnings': [daily_profit] * len(date_range)
})
# Create monthly earnings dataframe
monthly_df = daily_df.set_index('Date').resample('M').sum().reset_index()
# Plotting
st.subheader("Daily Earnings Trend")
fig_daily = px.line(daily_df, x='Date', y='Earnings',
title='Daily Earnings Over Time')
st.plotly_chart(fig_daily)
st.subheader("Monthly Earnings Trend")
fig_monthly = px.bar(monthly_df, x='Date', y='Earnings',
title='Monthly Earnings Over Time')
st.plotly_chart(fig_monthly)
# Additional investment details
st.subheader("Investment Details")
details_col1, details_col2 = st.columns(2)
with details_col1:
st.write("Investment Duration:")
st.write(f"Days: {days_diff}")
st.write(f"Months: {months_diff:.1f}")
with details_col2:
st.write("Investment Values:")
st.write(f"Initial Investment: {total_investment:,.2f}")
st.write(f"Current Value: {current_value:,.2f}")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?