Still some really weird behaviour, but stock trading is working a bit better now than before.

This commit is contained in:
Isaak Buslovich 2023-11-20 00:22:52 +01:00
parent 24951b36be
commit fd647c2407
Signed by: Isaak
GPG Key ID: EEC31D6437FBCC63

View File

@ -148,6 +148,7 @@ class Market:
self.trend_factor = self.generate_market_trend() self.trend_factor = self.generate_market_trend()
self.update_economic_indicators() self.update_economic_indicators()
self.handle_market_events() self.handle_market_events()
def calculate_price_change(self, product): def calculate_price_change(self, product):
demand_factor = self.total_bought[product] - self.total_sold[product] demand_factor = self.total_bought[product] - self.total_sold[product]
# Here you can define how strongly demand affects the price, e.g., 0.05 as in your original code # Here you can define how strongly demand affects the price, e.g., 0.05 as in your original code
@ -463,13 +464,13 @@ class Company:
total_value = stock_price * amount total_value = stock_price * amount
if action == 'buy': if action == 'buy':
return self._buy_stock(company_id, amount, total_value, is_ai) return self._buy_stock(company_id, amount)
elif action == 'sell': elif action == 'sell':
return self._sell_stock(company_id, amount, total_value, is_ai) return self._sell_stock(company_id, amount, total_value, is_ai)
else: else:
return "Invalid stock action." return "Invalid stock action."
def _buy_stock(self, company_id, amount, total_value, is_ai): def _buy_stock(self, company_id, amount):
# Calculate the total number of shares currently owned # Calculate the total number of shares currently owned
total_shares_owned = sum(self._market.companies[company_id].own_stock_ownership.values()) total_shares_owned = sum(self._market.companies[company_id].own_stock_ownership.values())
available_shares = self.total_shares - total_shares_owned available_shares = self.total_shares - total_shares_owned
@ -487,7 +488,6 @@ class Company:
self._market.update_stock_ledger(company_id, self.player_id, amount) self._market.update_stock_ledger(company_id, self.player_id, amount)
if company_id != self.player_id: if company_id != self.player_id:
# Update buyer's stock_holdings for investments in other companies
self.stock_holdings[company_id] += amount self.stock_holdings[company_id] += amount
else: else:
# Update own_stock_ownership when buying own shares # Update own_stock_ownership when buying own shares