Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import os
- import matplotlib.pyplot as plt
- def clear_screen():
- os.system('cls' if os.name == 'nt' else 'clear')
- def calculate_sma(prices, period):
- return sum(prices[-period:]) / period
- def simulate_price_change(stock_price):
- return stock_price + random.uniform(-10, 10)
- def display_menu():
- print("** MS-DOS Stock Market Simulator **")
- print("---------------------------------")
- print("1. Buy Stocks")
- print("2. Sell Stocks")
- print("3. Check Portfolio")
- print("4. View Historical Data")
- print("5. Calculate Technical Indicators")
- print("6. Quit")
- def main():
- stocks = {
- "AAPL": 150,
- "GOOGL": 2500,
- "AMZN": 3000,
- }
- stock_history = {
- "AAPL": [],
- "GOOGL": [],
- "AMZN": [],
- }
- player_portfolio = {}
- player_cash = 10000
- while True:
- clear_screen()
- display_menu()
- choice = input("Enter your choice: ")
- if choice == "1":
- stock_name = input("Which stock do you want to buy? ")
- shares = int(input("How many shares do you want to buy? "))
- if stock_name in stocks and player_cash >= stocks[stock_name] * shares:
- player_portfolio[stock_name] = player_portfolio.get(stock_name, 0) + shares
- player_cash -= stocks[stock_name] * shares
- print("Purchase successful!")
- else:
- print("Insufficient funds or stock not available.")
- elif choice == "2":
- stock_name = input("Which stock do you want to sell? ")
- shares = int(input("How many shares do you want to sell? "))
- if stock_name in player_portfolio and player_portfolio[stock_name] >= shares:
- player_portfolio[stock_name] -= shares
- player_cash += stocks[stock_name] * shares
- print("Sale successful!")
- else:
- print("You don't have enough shares of that stock.")
- elif choice == "3":
- print("Your Portfolio:")
- for stock, shares in player_portfolio.items():
- print(f"{stock}: {shares} shares")
- print("Your Cash:")
- print(player_cash)
- elif choice == "4":
- stock_name = input("Enter the stock name: ")
- if stock_name in stock_history:
- plt.plot(stock_history[stock_name])
- plt.title(f"Historical Prices for {stock_name}")
- plt.xlabel("Days")
- plt.ylabel("Price")
- plt.show()
- else:
- print("No historical data available for that stock.")
- elif choice == "5":
- stock_name = input("Enter the stock name: ")
- if stock_name in stock_history:
- sma_20 = calculate_sma(stock_history[stock_name], 20)
- print(f"20-day SMA for {stock_name}: {sma_20}")
- # Calculate other technical indicators as needed
- else:
- print("No historical data available for that stock.")
- elif choice == "6":
- break
- else:
- print("Invalid choice. Please try again.")
- # Update stock prices and history
- for stock in stocks:
- stocks[stock] = simulate_price_change(stocks[stock])
- stock_history[stock].append(stocks[stock])
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment