Write a python code for colab 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.
Drop files here
or click to upload
import streamlit as st
from datetime import datetime
import pandas as pd
st.title("Gold Investment Calculator")
# Input section
col1, col2 = st.columns(2)
with col1:
gold_tola = st.number_input("Amount of Gold (in tolas)", min_value=0.0, step=0.1)
buy_date = st.date_input("Date when bought")
buy_price = st.number_input("Price per tola when bought (in your currency)", min_value=0.0)
with col2:
current_price = st.number_input("Current price per tola (in your currency)", min_value=0.0)
current_date = st.date_input("Current date")
if st.button("Calculate Returns"):
if buy_date >= current_date:
st.error("Buy date must be before current date!")
else:
# Calculate total investment and current value
total_investment = gold_tola * buy_price
current_value = gold_tola * current_price
# Calculate total profit
total_profit = current_value - total_investment
# Calculate time difference
date_diff = (current_date - buy_date)
total_days = date_diff.days
total_months = total_days / 30.44 # average days in a month
# Calculate average earnings
avg_monthly_earning = total_profit / total_months if total_months > 0 else 0
avg_daily_earning = total_profit / total_days if total_days > 0 else 0
# Display results
st.markdown("### Results")
results_df = pd.DataFrame({
'Metric': ['Total Investment', 'Current Value', 'Total Profit',
'Average Monthly Earning', 'Average Daily Earning'],
'Value': [
f"{total_investment:,.2f}",
f"{current_value:,.2f}",
f"{total_profit:,.2f}",
f"{avg_monthly_earning:,.2f}",
f"{avg_daily_earning:,.2f}"
]
})
st.table(results_df)
# Additional insights
profit_percentage = (total_profit / total_investment) * 100
st.markdown(f"### Investment Insights")
st.write(f"Total time period: {total_days} days ({total_months:.1f} months)")
st.write(f"Return on Investment: {profit_percentage:.2f}%")
if profit_percentage > 0:
st.success("Your investment has generated positive returns! 📈")
elif profit_percentage < 0:
st.error("Your investment is currently at a loss. 📉")
else:
st.info("Your investment value remains unchanged. 📊")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?