You are on page 1of 1

import random

class Stock:
def __init__(self, symbol, price):
self.symbol = symbol
self.price = price

def update_price(self):
# Simulate price changes (for demonstration purposes)
self.price += random.uniform(-5, 5)

class Portfolio:
def __init__(self, balance):
self.balance = balance
self.stocks = {}

def buy(self, stock, quantity):


cost = stock.price * quantity
if cost > self.balance:
return False
self.balance -= cost
if stock.symbol in self.stocks:
self.stocks[stock.symbol] += quantity
else:
self.stocks[stock.symbol] = quantity
return True

def sell(self, stock, quantity):


if stock.symbol in self.stocks and self.stocks[stock.symbol] >= quantity:
revenue = stock.price * quantity
self.balance += revenue
self.stocks[stock.symbol] -= quantity
if self.stocks[stock.symbol] == 0:
del self.stocks[stock.symbol]
return True
return False

def main():
initial_balance = 10000 # Initial balance for the portfolio
portfolio = Portfolio(initial_balance)
stocks = [Stock("AAPL", 150), Stock("GOOGL", 2700), Stock("TSLA", 750)]

while True:

You might also like