Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,67 +52,66 @@ class Investment_Simulator:
|
|
| 52 |
|
| 53 |
def update_portfolio_history(self):
|
| 54 |
now = datetime.now()
|
| 55 |
-
total_value = compute_portfolio_value()
|
| 56 |
self.portfolio_history.append((now, total_value))
|
| 57 |
|
| 58 |
def plot_portfolio_value(self):
|
| 59 |
if not self.portfolio_history:
|
| 60 |
return None
|
| 61 |
df = pd.DataFrame(self.portfolio_history, columns=["Time", "Value"])
|
| 62 |
-
fig = px.line(df, x="Time", y="Value", title="
|
| 63 |
fig.update_layout(xaxis_rangeslider_visible=True)
|
| 64 |
return fig
|
| 65 |
|
| 66 |
def process_input(self, user_input):
|
| 67 |
-
#global self.portfolio, self.history
|
| 68 |
user_input = user_input.lower()
|
| 69 |
|
| 70 |
try:
|
| 71 |
if "buy" in user_input:
|
| 72 |
amount = int(user_input.split("£")[1].split()[0])
|
| 73 |
ticker = user_input.split("of")[1].strip().upper()
|
| 74 |
-
price = get_price(ticker)
|
| 75 |
if not price:
|
| 76 |
-
return f"
|
| 77 |
qty = round(amount / price, 4)
|
| 78 |
if amount > self.portfolio["cash"]:
|
| 79 |
-
return f"
|
| 80 |
self.portfolio["cash"] -= amount
|
| 81 |
self.portfolio["stocks"][ticker] = self.portfolio["stocks"].get(ticker, 0) + qty
|
| 82 |
-
self.history.append(f"
|
| 83 |
-
update_portfolio_history()
|
| 84 |
-
return f"
|
| 85 |
|
| 86 |
elif "sell" in user_input:
|
| 87 |
amount = int(user_input.split("£")[1].split()[0])
|
| 88 |
ticker = user_input.split("of")[1].strip().upper()
|
| 89 |
-
price = get_price(ticker)
|
| 90 |
if not price:
|
| 91 |
-
return f"
|
| 92 |
qty_to_sell = round(amount / price, 4)
|
| 93 |
current_qty = self.portfolio["stocks"].get(ticker, 0)
|
| 94 |
if qty_to_sell > current_qty:
|
| 95 |
-
return f"
|
| 96 |
self.portfolio["stocks"][ticker] -= qty_to_sell
|
| 97 |
self.portfolio["cash"] += amount
|
| 98 |
-
self.history.append(f"
|
| 99 |
-
update_portfolio_history()
|
| 100 |
-
return f"
|
| 101 |
|
| 102 |
elif "portfolio" in user_input:
|
| 103 |
cash = self.portfolio['cash']
|
| 104 |
total_stock_value = 0.0
|
| 105 |
-
summary = [f"
|
| 106 |
for ticker, qty in self.portfolio["stocks"].items():
|
| 107 |
-
live_price = get_price(ticker)
|
| 108 |
if live_price:
|
| 109 |
value = qty * live_price
|
| 110 |
total_stock_value += value
|
| 111 |
-
summary.append(f"
|
| 112 |
else:
|
| 113 |
-
summary.append(f"
|
| 114 |
total_value = cash + total_stock_value
|
| 115 |
-
summary.append(f"\
|
| 116 |
return "\n".join(summary)
|
| 117 |
|
| 118 |
elif "history" in user_input:
|
|
@@ -123,21 +122,21 @@ class Investment_Simulator:
|
|
| 123 |
self.portfolio["stocks"].clear()
|
| 124 |
self.history.clear()
|
| 125 |
self.portfolio_history.clear()
|
| 126 |
-
update_portfolio_history()
|
| 127 |
-
return "
|
| 128 |
|
| 129 |
else:
|
| 130 |
-
return "
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
-
return f"
|
| 134 |
|
| 135 |
def run_all(self, text):
|
| 136 |
-
response = process_input()
|
| 137 |
-
fig = plot_portfolio_value()
|
| 138 |
return response, fig
|
| 139 |
|
| 140 |
-
update_portfolio_history()
|
| 141 |
|
| 142 |
|
| 143 |
class News:
|
|
|
|
| 52 |
|
| 53 |
def update_portfolio_history(self):
|
| 54 |
now = datetime.now()
|
| 55 |
+
total_value = self.compute_portfolio_value()
|
| 56 |
self.portfolio_history.append((now, total_value))
|
| 57 |
|
| 58 |
def plot_portfolio_value(self):
|
| 59 |
if not self.portfolio_history:
|
| 60 |
return None
|
| 61 |
df = pd.DataFrame(self.portfolio_history, columns=["Time", "Value"])
|
| 62 |
+
fig = px.line(df, x="Time", y="Value", title="Portfolio Value Over Time", labels={"Time": "Time", "Value": "Value (£)"})
|
| 63 |
fig.update_layout(xaxis_rangeslider_visible=True)
|
| 64 |
return fig
|
| 65 |
|
| 66 |
def process_input(self, user_input):
|
|
|
|
| 67 |
user_input = user_input.lower()
|
| 68 |
|
| 69 |
try:
|
| 70 |
if "buy" in user_input:
|
| 71 |
amount = int(user_input.split("£")[1].split()[0])
|
| 72 |
ticker = user_input.split("of")[1].strip().upper()
|
| 73 |
+
price = self.get_price(ticker)
|
| 74 |
if not price:
|
| 75 |
+
return f"Couldn't fetch price for {ticker}."
|
| 76 |
qty = round(amount / price, 4)
|
| 77 |
if amount > self.portfolio["cash"]:
|
| 78 |
+
return f"Insufficient funds (£{self.portfolio['cash']:.2f})."
|
| 79 |
self.portfolio["cash"] -= amount
|
| 80 |
self.portfolio["stocks"][ticker] = self.portfolio["stocks"].get(ticker, 0) + qty
|
| 81 |
+
self.history.append(f"Bought £{amount} of {ticker} ({qty} shares at £{price:.2f})")
|
| 82 |
+
self.update_portfolio_history()
|
| 83 |
+
return f"Bought {qty} shares of {ticker} at £{price:.2f}"
|
| 84 |
|
| 85 |
elif "sell" in user_input:
|
| 86 |
amount = int(user_input.split("£")[1].split()[0])
|
| 87 |
ticker = user_input.split("of")[1].strip().upper()
|
| 88 |
+
price = self.get_price(ticker)
|
| 89 |
if not price:
|
| 90 |
+
return f"Couldn't fetch price for {ticker}."
|
| 91 |
qty_to_sell = round(amount / price, 4)
|
| 92 |
current_qty = self.portfolio["stocks"].get(ticker, 0)
|
| 93 |
if qty_to_sell > current_qty:
|
| 94 |
+
return f"You don't own enough of {ticker}."
|
| 95 |
self.portfolio["stocks"][ticker] -= qty_to_sell
|
| 96 |
self.portfolio["cash"] += amount
|
| 97 |
+
self.history.append(f"Sold £{amount} of {ticker} ({qty_to_sell} shares at £{price:.2f})")
|
| 98 |
+
self.update_portfolio_history()
|
| 99 |
+
return f"Sold {qty_to_sell} shares of {ticker} at £{price:.2f}"
|
| 100 |
|
| 101 |
elif "portfolio" in user_input:
|
| 102 |
cash = self.portfolio['cash']
|
| 103 |
total_stock_value = 0.0
|
| 104 |
+
summary = [f"Cash: £{cash:.2f}"]
|
| 105 |
for ticker, qty in self.portfolio["stocks"].items():
|
| 106 |
+
live_price = self.get_price(ticker)
|
| 107 |
if live_price:
|
| 108 |
value = qty * live_price
|
| 109 |
total_stock_value += value
|
| 110 |
+
summary.append(f"{ticker}: {qty:.4f} shares (£{value:.2f})")
|
| 111 |
else:
|
| 112 |
+
summary.append(f"{ticker}: {qty:.4f} shares (price unavailable)")
|
| 113 |
total_value = cash + total_stock_value
|
| 114 |
+
summary.append(f"\nTotal Portfolio Value: £{total_value:.2f}")
|
| 115 |
return "\n".join(summary)
|
| 116 |
|
| 117 |
elif "history" in user_input:
|
|
|
|
| 122 |
self.portfolio["stocks"].clear()
|
| 123 |
self.history.clear()
|
| 124 |
self.portfolio_history.clear()
|
| 125 |
+
self.update_portfolio_history()
|
| 126 |
+
return "Portfolio reset."
|
| 127 |
|
| 128 |
else:
|
| 129 |
+
return "Try commands like:\n• Buy £100 of AAPL\n• Sell £50 of TSLA\n• Show portfolio"
|
| 130 |
|
| 131 |
except Exception as e:
|
| 132 |
+
return f"Error: {e}"
|
| 133 |
|
| 134 |
def run_all(self, text):
|
| 135 |
+
response = self.process_input()
|
| 136 |
+
fig = self.plot_portfolio_value()
|
| 137 |
return response, fig
|
| 138 |
|
| 139 |
+
self.update_portfolio_history()
|
| 140 |
|
| 141 |
|
| 142 |
class News:
|