Skip to content

Commit

Permalink
added updating prediction feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Incharajayaram committed Jul 23, 2024
1 parent 46a1555 commit ec67c24
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions web_stock_price_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,54 @@
import numpy as np
from keras.models import load_model # type: ignore
import matplotlib.pyplot as plt
from datetime import datetime, timedelta
import yfinance as yf
import time

st.title("Stock Price Predictor App")

stock = st.text_input("Enter the Stock ID", "GOOG")
st.markdown("<h2 style='text-align: left; color: white;'>Enter the Stock ID</h2>", unsafe_allow_html=True)
stock = st.text_input("", "GOOG")

from datetime import datetime
end = datetime.now()
start = datetime(end.year-20,end.month,end.day)

google_data = yf.download(stock, start, end)

model = load_model(r"C:\Users\incha\stock_price_prediction_project\Latest_stcok_price_model.keras")
st.subheader("Stock Data")
st.write(google_data)

model = load_model(r"C:\Users\incha\stock_price_prediction_project\Latest_stcok_price_model.keras")


def online_learning(model, new_data, scaler):
scaled_new_data = scaler.transform(new_data)
x_new = scaled_new_data[:-1].reshape(1, -1, 1)
y_new = scaled_new_data[-1].reshape(1, 1)

prediction = model.predict(x_new)

model.fit(x_new, y_new, epochs=1, verbose=0)

return scaler.inverse_transform(prediction)[0][0]

def update_data_and_model(stock, model, scaler, last_100_days):
end = datetime.now()
start = end - timedelta(days=1)
new_data = yf.download(stock, start, end)

if not new_data.empty:
new_close = new_data['Adj Close'].values[-1]
last_100_days = np.append(last_100_days[1:], new_close)

predicted_price = online_learning(model, last_100_days.reshape(-1, 1), scaler)

st.write(f"New data point: {new_close}")
st.write(f"Predicted next price: {predicted_price}")

return last_100_days



splitting_len = int(len(google_data)*0.7)
x_test = pd.DataFrame(google_data.Close[splitting_len:])

Expand Down Expand Up @@ -103,4 +135,10 @@ def predict_future_stock(no_of_days, prev_100):
plt.xlabel('Future days')
plt.ylabel('Close Price')
plt.title("Future Close price of stock")
st.pyplot(fig)
st.pyplot(fig)


if st.button("Perform Online Learning"):
last_100_days = google_data['Adj Close'].tail(100).values
last_100_days = update_data_and_model(stock, model, scaler, last_100_days)
st.write("Model updated with the latest data point.")

0 comments on commit ec67c24

Please sign in to comment.