Spaces:
Sleeping
Sleeping
File size: 8,037 Bytes
b4eb7a7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
import streamlit as st
import pandas as pd
import numpy as np
from prophet import Prophet
import yfinance as yf
from sklearn.metrics import mean_absolute_error, mean_squared_error
from prophet.plot import plot_plotly, plot_components_plotly
# List of ticker symbols
ticker_symbols = ['RELIANCE', 'TCS', 'HDFCBANK', 'ICICIBANK', 'BHARTIARTL', 'SBIN', 'INFY', 'LICI', 'ITC', 'HINDUNILVR', 'LT', 'BAJFINANCE', 'HCLTECH', 'MARUTI', 'SUNPHARMA', 'ADANIENT', 'KOTAKBANK', 'TITAN', 'ONGC', 'TATAMOTORS', 'NTPC', 'AXISBANK', 'DMART', 'ADANIGREEN', 'ADANIPORTS', 'ULTRACEMCO', 'ASIANPAINT', 'COALINDIA', 'BAJAJFINSV', 'BAJAJ-AUTO', 'POWERGRID', 'NESTLEIND', 'WIPRO', 'M&M', 'IOC', 'JIOFIN', 'HAL', 'DLF', 'ADANIPOWER', 'JSWSTEEL', 'TATASTEEL', 'SIEMENS', 'IRFC', 'VBL', 'ZOMATO', 'PIDILITIND', 'GRASIM', 'SBILIFE', 'BEL', 'LTIM', 'TRENT', 'PNB', 'INDIGO', 'BANKBARODA', 'HDFCLIFE', 'ABB', 'BPCL', 'PFC', 'GODREJCP', 'TATAPOWER', 'HINDALCO', 'HINDZINC', 'TECHM', 'AMBUJACEM', 'INDUSINDBK', 'CIPLA', 'GAIL']
# Function to fetch stock data from Yahoo Finance
def fetch_stock_data(ticker_symbol, start_date, end_date):
ticker_symbol = ticker_symbol +".NS"
stock_data = yf.download(ticker_symbol, start=start_date, end=end_date)
df = stock_data[['Adj Close']].reset_index()
df = df.rename(columns={'Date': 'ds', 'Adj Close': 'y'})
# df.to_csv(f"{ticker_symbol}.csv")
return df
# Function to train the Prophet model
def train_prophet_model(df):
model = Prophet()
model.fit(df)
return model
# Function to make the forecast
def make_forecast(model, periods):
future = model.make_future_dataframe(periods=periods)
forecast = model.predict(future)
return forecast
# Function to calculate performance metrics
def calculate_performance_metrics(actual, predicted):
mae = mean_absolute_error(actual, predicted)
mse = mean_squared_error(actual, predicted)
rmse = np.sqrt(mse)
return {'MAE': mae, 'MSE': mse, 'RMSE': rmse}
# Function to determine sentiment
def determine_sentiment(actual, predicted):
if actual > predicted:
sentiment = 'Negative'
elif actual < predicted:
sentiment = 'Positive'
else:
sentiment = 'Neutral'
return sentiment
# Streamlit app
def main():
st.title('Stock Prediction on NSE Stocks')
# Set up the layout
st.sidebar.header('User Input Parameters')
ticker_symbol = st.sidebar.selectbox('Enter Ticker Symbol', options=ticker_symbols, index=0)
# Dropdown for training period selection
training_period = st.sidebar.selectbox('Select Training Period',
options=['1 week', '1 month', '1 year', '10 years'])
# Calculate start date and end date based on training period
if training_period == '1 week':
start_date = pd.to_datetime('today') - pd.DateOffset(weeks=1)
elif training_period == '1 month':
start_date = pd.to_datetime('today') - pd.DateOffset(months=1)
elif training_period == '1 year':
start_date = pd.to_datetime('today') - pd.DateOffset(years=1)
elif training_period == '10 years':
start_date = pd.to_datetime('today') - pd.DateOffset(years=10)
end_date = pd.to_datetime('today')
# Fetching the data for the selected training period
df = fetch_stock_data(ticker_symbol, start_date, end_date)
# Dropdown for forecast horizon selection
forecast_horizon = st.sidebar.selectbox('Forecast Horizon',
options=['Next day', 'Next week', 'Next month'],
format_func=lambda x: x.capitalize())
# Convert the selected horizon to days
horizon_mapping = {'Next day': 1, 'Next week': 7, 'Next month': 30}
forecast_days = horizon_mapping[forecast_horizon]
if st.sidebar.button('Forecast Stock Prices'):
with st.spinner('Training model...'):
model = train_prophet_model(df)
forecast = make_forecast(model, forecast_days)
forecast_reversed = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].iloc[-forecast_days:].iloc[::-1]
st.markdown("""
*The prediction was made using the Prophet forecasting model. The model was trained on historical stock data and used to forecast future prices based on the observed trends and patterns.*
""")
st.subheader(f'Forecast Summary for {ticker_symbol}')
latest_forecast = forecast_reversed.iloc[0]
# Last Stock Price details with sentiment indicator
actual_last_price = df["y"].iloc[-1]
predicted_last_price = latest_forecast['yhat']
sentiment = determine_sentiment(actual_last_price, predicted_last_price)
st.warning(f'The last available adjusted closing price for {ticker_symbol} on {end_date.strftime("%d %B %Y")} is **{actual_last_price:.2f}**.')
if sentiment == 'Positive':
st.success(f'Overall predication indicates positive sentiment.')
elif sentiment == 'Negative':
st.error(f'Overall predication indicates negative sentiment.')
else:
st.info(f'Overall predication indicates neutral sentiment.')
# Prediction details
st.markdown(f"""
**Prediction for {forecast_horizon.lower()}:**
- **Date:** {latest_forecast['ds'].strftime("%d %B %Y")}
- **Predicted Price:** {latest_forecast['yhat']:.2f}
- **Lower Bound:** {latest_forecast['yhat_lower']:.2f}
- **Upper Bound:** {latest_forecast['yhat_upper']:.2f}
""")
st.markdown(f"""
**Find below the prediction Data for the {forecast_horizon.lower()}:**
""")
st.write(forecast_reversed)
# Calculate performance metrics
# Function to determine if performance metrics are in a good range
def evaluate_performance_metrics(metrics):
evaluation = {}
evaluation['MAE'] = 'Good' if metrics['MAE'] < 0.05 * (df['y'].max() - df['y'].min()) else 'Not Good'
evaluation['MSE'] = 'Good' if metrics['MSE'] < 0.1 * (df['y'].max() - df['y'].min())**2 else 'Not Good'
evaluation['RMSE'] = 'Good' if metrics['RMSE'] < 0.1 * (df['y'].max() - df['y'].min()) else 'Not Good'
return evaluation
# Calculate performance metrics
actual = df['y']
predicted = forecast['yhat'][:len(df)]
metrics = calculate_performance_metrics(actual, predicted)
# Evaluate performance metrics
evaluation = evaluate_performance_metrics(metrics)
metrics = calculate_performance_metrics(actual, predicted)
MAE =metrics['MAE']
MSE = metrics['MSE']
RMSE = metrics['RMSE']
# Display evaluation
st.subheader('Performance Evaluation')
st.write('The metrics below provide a quantitative measure of the model’s accuracy:')
maecolor = "green" if evaluation["MAE"] == "Good" else "red"
msecolor = "green" if evaluation["MSE"] == "Good" else "red"
rmsecolor = "green" if evaluation["RMSE"] == "Good" else "red"
st.markdown(f'- **Mean Absolute Error (MAE):** {MAE:.2f} - :{maecolor}[{"Good" if evaluation["MAE"] == "Good" else "Not good"}] ')
st.markdown("(The average absolute difference between predicted and actual values.)")
st.markdown(f'- **Mean Squared Error (MSE):** {MSE:.2f} - :{msecolor}[{"Good" if evaluation["MSE"] == "Good" else "Not good"}] ')
st.markdown("(The average squared difference between predicted and actual values.)")
st.markdown(f'- **Root Mean Squared Error (RMSE):** {RMSE:.2f} - :{rmsecolor}[{"Good" if evaluation["RMSE"] == "Good" else "Not good"}] ')
st.markdown("(The square root of MSE, which is more interpretable in the same units as the target variable.)")
# Run the main function
if __name__ == "__main__":
main() |