import yfinance as yf import datetime while True: # Prompt the user for the share symbol share_symbol = input("Enter the share symbol (e.g., LLOY.L): ") # Retrieve historical price data for the last six months end_date = datetime.date.today() start_date = end_date - datetime.timedelta(days=6 * 30) # Fetch the historical price data using yfinance try: data = yf.download(share_symbol, start=start_date, end=end_date) except ValueError: print("Error: Share symbol not recognized. Please try again.") continue # Extract the adjusted close prices close_prices = data['Close'] # Check if we have valid data for the last six months if close_prices.empty: print("Error: No data available for the last six months. Please try again later.") else: # Calculate weekly high and low prices weekly_highs = close_prices.resample('W').max() weekly_lows = close_prices.resample('W').min() # Get the current week's high and low prices current_week_high = weekly_highs.iloc[-1] current_week_low = weekly_lows.iloc[-1] # Get the start and end dates of the current week current_week_start = weekly_highs.index[-1].strftime("%Y-%m-%d") current_week_end = weekly_lows.index[-1].strftime("%Y-%m-%d") # Print the workings and figures for the last six months print("Last Six Months:") print("{:<12s}{:<12s}{:<12s}".format("Week", "Highest", "Lowest")) for i in range(len(weekly_highs)): week_start = weekly_highs.index[i].strftime("%Y-%m-%d") week_end = weekly_lows.index[i].strftime("%Y-%m-%d") print("{:<12s}{:<12.2f} {:<12.2f}".format(week_start + " - " + week_end, weekly_highs[i], weekly_lows[i])) # Calculate the average percentage difference between high and low prices for each week percentage_diffs = ((weekly_highs - weekly_lows) / weekly_lows) * 100 avg_percentage_diff = percentage_diffs.mean() # Retrieve the current share price current_price = close_prices.iloc[-1] # Calculate the other required values current_week_highest = round(current_week_high, 2) current_price_increased = round(current_price * (1 + avg_percentage_diff / 100), 2) current_price_decreased = round(current_price * (1 - avg_percentage_diff / 100), 2) current_week_lowest = round(current_week_low, 2) # Display the final results print("\nFinal Results:") print("Current Week:") print("Week: {} - {}".format(current_week_start, current_week_end)) print("Current Week's Highest Price: {:.2f}".format(current_week_highest)) print("Current Price Increased by Average Percentage Increase: {:.2f}".format(current_price_increased)) print("Current Share Price: {:.2f}".format(current_price)) print("Current Price Decreased by Average Percentage Decrease: {:.2f}".format(current_price_decreased)) print("Current Week's Lowest Price: {:.2f}".format(current_week_lowest)) # Ask the user if they want to enter another share choice = input("Do you want to enter another share symbol? (y/n): ") if choice.lower() != "y": break