Error in Web App

Can anyone help me with this. I’m a newbie on streamlit. The thing is that I’m creating a Stock Prediction web app using Machine Learning and Streamlit. I created a Sidebar Menu where I gave to options to the users whether they can choose the “Stock Exploration and Feature Extraction” or “Train Model”.

1. So when I choose “Stock Exploration and Feature Extraction” it’s working well but sometimes it gives an error for the “Show Moving Average” button. If anyone checked this one then it shows Moving Average but when it is unchecked it also showing the “Moving Average”.

2. There is another section called “Select number of days Moving Average” its also gives an error. For this field the default value is 5, if I didn’t enter anything it shows “ValueError”. But after entering some values it’s totally gone but after refreshing it comes again.
How to resolve this??

3. Error 3 : There is two options in the sidebar menu as I told before. When I go click on the “Train Model” option and after training when I came back to “Stock Exploration and Feature Extraction” option it automatically loads the the previously Company data which I don’t want. I want to start the process again for the “Stock Exploration and Feature Extraction” by entering the Company name again not the by the previous one but with the new one or how can I can I refreshed it??


See there is No Ticker symbol on the fieldEnter Ticker Symbol” but it loads the prevoius data(see Below “Select option to Explore Stocks only Comes when I select the Ticker Symbol”) after the returning from the “Train Model” option.

Here is the Source Code-----------

from pandas.core.algorithms import mode
import streamlit as st
import math
import time
from tensorflow import keras
import datetime as dt
from datetime import date
import yfinance as yf
import pandas as pd
from plotly import graph_objs as go
import as px
import math
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt

START = "2014-01-01"
TODAY ="%Y-%m-%d")

st.title("Stock Prediction App")

stocks = ["Select the Stock", "AAPL", "GOOG", "MSFT", "AMZN", "TSLA", "GME", "NVDA", "AMD"]

# ------------------- Loading Data ---------------------

def load_data(ticker):
    data =, START, TODAY)
    return data

# ------------------ Showing Stock Financials ----------------

def stock_financials(stock):

    df_ticker = yf.Ticker(stock)
    sector =['sector']
    prevClose =['previousClose']
    marketCap =['marketCap']
    twoHunDayAvg =['twoHundredDayAverage']
    fiftyTwoWeekHigh =['fiftyTwoWeekHigh']
    fiftyTwoWeekLow =['fiftyTwoWeekLow']
    Name =['longName']
    averageVolume =['averageVolume']
    ftWeekChange =['52WeekChange']
    website =['website']

    st.write('Company Name -', Name)
    st.write('Sector -', sector)
    st.write('Company Website -', website)
    st.write('Average Volume -', averageVolume)
    st.write('Market Cap -', marketCap)
    st.write('Previous Close -', prevClose)
    st.write('52 Week Change -', ftWeekChange)
    st.write('52 Week High -', fiftyTwoWeekHigh)
    st.write('52 Week Low -', fiftyTwoWeekLow)
    st.write('200 Day Average -', twoHunDayAvg)

# -------------------- Plotting Raw Data ---------------------

def plot_raw_data(stock, data_1):
    df_ticker = yf.Ticker(stock)
    Name =['longName']
    numeric_df = data_1.select_dtypes(['float', 'int'])
    numeric_cols = numeric_df.columns.tolist()
    st.markdown('**_Features_** you want to **_Plot_**')
    features_selected = st.multiselect("", numeric_cols)
    if st.button("Generate Plot"):
        cust_data = data_1[features_selected]
        plotly_figure = px.line(data_frame=cust_data, x=data_1['Date'], y=features_selected, title= Name + ' ' + '<i>timeline</i>')
        plotly_figure.update_layout(title = {'y':0.9,'x':0.5, 'xanchor': 'center', 'yanchor': 'top'})
        plotly_figure.update_layout(font=dict(size=16, family="Monospace"), legend=dict(yanchor="top", y=0.98, 
                                    xanchor="left", x=0.02, title="Price"), width=800, height=550)

# ------------------ Creating train and test data ------------

def create_train_test_data(df1):

    data = df1.sort_index(ascending=True, axis=0)
    new_data = pd.DataFrame(index=range(0, len(df1)), columns=['Date', 'High', 'Low', 'Open', 'Volume', 'Close'])

    for i in range(0, len(data)):
        new_data['Date'][i] = data['Date'][i]
        new_data['High'][i] = data['High'][i]
        new_data['Low'][i] = data['Low'][i]
        new_data['Open'][i] = data['Open'][i]
        new_data['Volume'][i] = data['Volume'][i]
        new_data['Close'][i] = data['Close'][i]

    # Removing the hour, minute and second

    new_data['Date'] = pd.to_datetime(new_data['Date'])

    train_data_len = math.ceil(len(new_data) * .8)

    train_data = new_data[:train_data_len]
    test_data = new_data[train_data_len:]

    return train_data, test_data

# ------------------ Finding Linear Regression -------------

def Linear_Regression_model(train_data, test_data):

    x_train = train_data.drop(columns=['Date', 'Close'], axis=1)
    x_test = test_data.drop(columns=['Date', 'Close'], axis=1)
    y_train = train_data['Close']
    y_test = test_data['Close']

    from sklearn.linear_model import LinearRegression

    model = LinearRegression(), y_train)

    #Making the Predictions
    prediction = model.predict(x_test)

    return prediction

# ------------------ Finding Moving Average -------------

def find_moving_avg(ma_button, df):

    days = ma_button
    data1 = df.sort_index(ascending=True, axis = 0)
    new_data = pd.DataFrame(index = range(0, len(data1)), columns = ['Date', 'Close'])

    for i in range(0, len(data1)):
        new_data['Date'][i] = data1['Date'][i]
        new_data['Close'][i] = data1['Close'][i]

    # creating new column for Simple Moving Average Values
    new_data['SMA_'+str(days)] = new_data['Close'].rolling(min_periods=1, window=days).mean()



    fig = go.Figure()
    fig.add_trace(go.Scatter(x=new_data['Date'], y=new_data['Close'], mode='lines', name='Close'))
    fig.add_trace(go.Scatter(x=new_data['Date'], y=new_data['SMA_'+str(days)], mode='lines', name='SMA_'+str(days)))
    fig.update_layout(title_text='Simple Moving Average', title_x=0.5, font=dict(size=16, family="Monospace"), legend=dict(yanchor="top", y=0.98, xanchor="left", x=0.02), height=550, width=800, autosize=False)


# ------------------ Sidebar Menu -----------------------

menu=["Stock Exploration and Feature Extraction", "Train Model"]
st.sidebar.subheader("Timeseries Settings")
choices = st.sidebar.selectbox("Select the Activity", menu,index=0)

if choices == 'Stock Exploration and Feature Extraction':
    st.subheader("Extract Data")
    #user_input = ''
    st.markdown('Enter **_Ticker_ Symbol** for the **Stock**')
    #user_input=st.selectbox("", stocks)
    user_input = st.text_input("", '')

    if not user_input:
        data = load_data(user_input)
        st.markdown('Select from the options below to Explore Stocks')

        selected_explore = st.selectbox("", options=['Select your Option', 'Stock Financials Exploration',
                                                     'Extract Features for Stock Price Forecasting'], index=0)
        if selected_explore == 'Stock Financials Exploration':
            st.markdown('**_Stock_ _Financial_** Information ------')
            plot_raw_data(user_input, data)
            shw_SMA = st.checkbox('Show Moving Average')

            if shw_SMA:
                st.write('Stock Data based on Moving Average')
                st.write('A Moving Average(MA) is a stock indicator that is commonly used in technical analysis')
                    'The reason for calculating moving average of a stock is to help smooth out the price of data over '
                    'a specified period of time by creating a constanly updated average price')
                    'A Simple Moving Average (SMA) is a calculation that takes the arithmatic mean of a given set of '
                    'prices over the specified number of days in the past, for example: over the previous 15, 30, 50, '
                    '100, or 200 days.')

                ma_button = st.number_input("Select Number of Days Moving Average", 5, 200)
                if ma_button:
                    st.write('You entered the Moving Average for ', ma_button, 'days')
                    find_moving_avg(ma_button, data)

        elif selected_explore == 'Extract Features for Stock Price Forecasting':
            st.markdown('Select **_Start_ _Date_ _for_ _Historical_ Stock** Data & features')
            start_date = st.date_input("", date(2014, 1, 1))
            st.write('You Selected Data From - ', start_date)
            submit_button = st.button("Extract Features")

            start_row = 0
            if submit_button:
                st.write('Extracted Features Dataframe for ', user_input)
                for i in range(0, len(data)):
                    if start_date <= pd.to_datetime(data['Date'][i]):
                        start_row = i

                st.write(data.iloc[start_row:, :])

elif choices == 'Train Model':
    st.subheader("Train Machine Learning Models for Stock Prediction")
    st.markdown('**_Select_ _Stocks_ _to_ Train**')
    stock_select = st.selectbox("", stocks, index=0)
    df1 = load_data(stock_select)
    df1 = df1.reset_index()
    df1['Date'] = pd.to_datetime(df1['Date'])
    options = ['Select your option', 'Linear Regression', 'Random Forest', 'XGBoost', 'LSTM']
    st.markdown('**_Select_ _Machine_ _Learning_ _Algorithms_ to Train**')
    models = st.selectbox("", options)
    submit = st.button('Train Model')

    if models == 'LSTM':
        st.markdown("**Select the _Number_ _of_ _epochs_ and _batch_ _size_ for _training_ form the following**")
        epoch = st.slider("Epochs", 0, 300, step = 1)
        b_s = st.slider("Batch Size", 32, 1024, step = 1)
        if submit:
            st.write('**Your _final_ _dataframe_ _for_ Training**')
            create_train_test_LSTM(df1, epoch, b_s, stock_select)

    elif models == 'Linear Regression':
        if submit:
            st.write('**Your _final_ _dataframe_ _for_ Training**')
            train_data, test_data = create_train_test_data(df1)
            pred_data = Linear_Regression_model(train_data, test_data)
            #prediction_plot(pred_data, test_data, models, stock_select)