import streamlit as st import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression import matplotlib.pyplot as plt import random import string # Remove the Streamlit default layout st.set_page_config(layout="wide") # Load the stock data from the CSV files data1 = pd.read_csv('Google_test_data.csv') data2 = pd.read_csv('tesla.csv') # Combine the datasets into a dictionary datasets = {'Google': data1, 'Tesla': data2} # Generate a random string for unique key generation def random_string(length): letters = string.ascii_lowercase return ''.join(random.choice(letters) for i in range(length)) # Get the user's dataset selection key = f'dataset_selection_{random_string(5)}' selected_dataset = st.selectbox('Select Stock', list(datasets.keys()), key=key) # Retrieve the selected dataset selected_data = datasets[selected_dataset] # Prepare the data for prediction X = np.arange(len(selected_data)).reshape(-1, 1) y = selected_data['Close'] # Train the linear regression model model = LinearRegression() model.fit(X, y) # Predict the stock prices predictions = model.predict(X) # Create subplots fig, axes = plt.subplots(3, 1, figsize=(10, 15)) # Plot 1: Actual and Predicted Stock Prices axes[0].plot(selected_data['Date'], y, label='Actual') axes[0].plot(selected_data['Date'], predictions, label='Predicted') axes[0].set_xlabel('Date') axes[0].set_ylabel('Stock Price') axes[0].set_title(f'{selected_dataset} Stock Price Prediction') axes[0].legend() # Plot 2: Volume of Trades axes[1].plot(selected_data['Date'], selected_data['Volume']) axes[1].set_xlabel('Date') axes[1].set_ylabel('Volume') axes[1].set_title(f'{selected_dataset} Volume of Trades') # Plot 3: Daily Percentage Change in Stock Prices daily_returns = selected_data['Close'].pct_change() * 100 axes[2].plot(selected_data['Date'], daily_returns) axes[2].set_xlabel('Date') axes[2].set_ylabel('Percentage Change') axes[2].set_title(f'{selected_dataset} Daily Percentage Change in Stock Prices') # Add a submit button if st.button('Submit'): # Get the updated dataset selection key = f'dataset_selection_{random_string(5)}' selected_dataset = st.selectbox('Select Stock', list(datasets.keys()), key=key) # Retrieve the updated dataset selected_data = datasets[selected_dataset] # Prepare the data for prediction X = np.arange(len(selected_data)).reshape(-1, 1) y = selected_data['Close'] # Train the linear regression model model.fit(X, y) # Predict the stock prices predictions = model.predict(X) # Clear the existing plots for ax in axes: ax.clear() # Update the plots with the new dataset axes[0].plot(selected_data['Date'], y, label='Actual') axes[0].plot(selected_data['Date'], predictions, label='Predicted') axes[0].set_xlabel('Date') axes[0].set_ylabel('Stock Price') axes[0].set_title(f'{selected_dataset} Stock Price Prediction') axes[0].legend() axes[1].plot(selected_data['Date'], selected_data['Volume']) axes[1].set_xlabel('Date') axes[1].set_ylabel('Volume') axes[1].set_title(f'{selected_dataset} Volume of Trades') daily_returns = selected_data['Close'].pct_change() * 100 axes[2].plot(selected_data['Date'], daily_returns) axes[2].set_xlabel('Date') axes[2].set_ylabel('Percentage Change') axes[2].set_title(f'{selected_dataset} Daily Percentage Change in Stock Prices') # Display the graphs in Streamlit st.pyplot(fig)