Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +97 -0
- keras_model.h5 +3 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from nselib import capital_market
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
+
|
| 10 |
+
# Define date range for data extraction
|
| 11 |
+
from_date_str = '01-01-2016'
|
| 12 |
+
to_date_str = '01-08-2024'
|
| 13 |
+
|
| 14 |
+
# Streamlit App Title
|
| 15 |
+
st.title('Stock Trend Prediction')
|
| 16 |
+
|
| 17 |
+
# Fetching Nifty 50 data
|
| 18 |
+
input_field = st.text_input("Enter your input:")
|
| 19 |
+
index_data = capital_market.index_data(index=input_field, from_date=from_date_str, to_date=to_date_str)
|
| 20 |
+
index_data['TIMESTAMP'] = pd.to_datetime(index_data['TIMESTAMP'], format='%d-%m-%Y')
|
| 21 |
+
index_data = index_data.sort_values('TIMESTAMP').reset_index(drop=True)
|
| 22 |
+
|
| 23 |
+
st.subheader('Data from 2016 to 2024')
|
| 24 |
+
st.write(index_data)
|
| 25 |
+
|
| 26 |
+
# Visualization: Closing Price vs Time
|
| 27 |
+
st.subheader('Closing Price vs Time chart')
|
| 28 |
+
fig = plt.figure(figsize=(12, 6))
|
| 29 |
+
plt.plot(index_data['TIMESTAMP'], index_data['CLOSE_INDEX_VAL'], label='Closing Price')
|
| 30 |
+
plt.xlabel('Year')
|
| 31 |
+
plt.ylabel('Closing Price')
|
| 32 |
+
plt.title('Closing Price vs Time')
|
| 33 |
+
plt.legend()
|
| 34 |
+
st.pyplot(fig)
|
| 35 |
+
|
| 36 |
+
# Add Moving Averages (100 EMA and 200 EMA)
|
| 37 |
+
moving_avg_100 = index_data['CLOSE_INDEX_VAL'].ewm(span=100, adjust=False).mean()
|
| 38 |
+
moving_avg_200 = index_data['CLOSE_INDEX_VAL'].ewm(span=200, adjust=False).mean()
|
| 39 |
+
|
| 40 |
+
fig = plt.figure(figsize=(12, 6))
|
| 41 |
+
plt.plot(index_data['TIMESTAMP'], index_data['CLOSE_INDEX_VAL'], label='Closing Price')
|
| 42 |
+
plt.plot(index_data['TIMESTAMP'], moving_avg_100, label='100 EMA', color='orange')
|
| 43 |
+
plt.plot(index_data['TIMESTAMP'], moving_avg_200, label='200 EMA', color='red')
|
| 44 |
+
plt.xlabel('Year')
|
| 45 |
+
plt.ylabel('Closing Price')
|
| 46 |
+
plt.title('Closing Price vs Time with 100 EMA and 200 EMA')
|
| 47 |
+
plt.legend()
|
| 48 |
+
st.pyplot(fig)
|
| 49 |
+
|
| 50 |
+
# Prepare data for model
|
| 51 |
+
df = index_data
|
| 52 |
+
|
| 53 |
+
# Splitting data into training and testing sets (70-30 split)
|
| 54 |
+
data_training = pd.DataFrame(df['CLOSE_INDEX_VAL'][0:int(len(df) * 0.70)])
|
| 55 |
+
data_testing = pd.DataFrame(df['CLOSE_INDEX_VAL'][int(len(df) * 0.70):])
|
| 56 |
+
past_100_days = data_training.tail(100)
|
| 57 |
+
|
| 58 |
+
# Scaling data
|
| 59 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 60 |
+
data_training_array = scaler.fit_transform(data_training)
|
| 61 |
+
|
| 62 |
+
# Prepare test data
|
| 63 |
+
final_df = pd.concat([past_100_days, data_testing], ignore_index=True)
|
| 64 |
+
input_data = scaler.transform(final_df)
|
| 65 |
+
|
| 66 |
+
x_test = []
|
| 67 |
+
y_test = []
|
| 68 |
+
|
| 69 |
+
for i in range(100, input_data.shape[0]):
|
| 70 |
+
x_test.append(input_data[i-100:i])
|
| 71 |
+
y_test.append(input_data[i, 0])
|
| 72 |
+
|
| 73 |
+
x_test, y_test = np.array(x_test), np.array(y_test)
|
| 74 |
+
|
| 75 |
+
# Load pre-trained model
|
| 76 |
+
model = load_model('keras_model.h5')
|
| 77 |
+
|
| 78 |
+
# Predictions
|
| 79 |
+
y_predicted = model.predict(x_test)
|
| 80 |
+
|
| 81 |
+
# Inverse transform to original scale
|
| 82 |
+
y_predicted = scaler.inverse_transform(y_predicted.reshape(-1, 1)).flatten()
|
| 83 |
+
y_test = scaler.inverse_transform(y_test.reshape(-1, 1)).flatten()
|
| 84 |
+
|
| 85 |
+
# Visualization: Predictions vs Original
|
| 86 |
+
test_dates = df['TIMESTAMP'][len(data_training):].reset_index(drop=True)
|
| 87 |
+
|
| 88 |
+
st.subheader('Prediction v/s Original')
|
| 89 |
+
fig2 = plt.figure(figsize=(12, 6))
|
| 90 |
+
plt.plot(test_dates, y_test, 'b', label='Original Price')
|
| 91 |
+
plt.plot(test_dates, y_predicted, 'r', label='Predicted Price')
|
| 92 |
+
plt.xlabel('Year')
|
| 93 |
+
plt.ylabel('Closing Price')
|
| 94 |
+
plt.title('Prediction vs Original Closing Price Over Time')
|
| 95 |
+
plt.legend()
|
| 96 |
+
plt.xticks(rotation=45)
|
| 97 |
+
st.pyplot(fig2)
|
keras_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ed57540c248fb825e55399711ef8987135105f176fbfcc6f1882c7e5c119c888
|
| 3 |
+
size 2205768
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
keras
|
| 3 |
+
streamlit
|
| 4 |
+
scikit-learn
|
| 5 |
+
matplotlib
|
| 6 |
+
pandas
|
| 7 |
+
numpy
|
| 8 |
+
nselib
|
| 9 |
+
pandas_market_calendars
|