Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,80 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
try:
|
| 7 |
-
data = yf.download(symbol, start=start, end=end)
|
| 8 |
-
return data
|
| 9 |
-
except Exception as e:
|
| 10 |
-
st.error(f"Error fetching data: {e}")
|
| 11 |
-
return None
|
| 12 |
|
|
|
|
| 13 |
def main():
|
| 14 |
-
st.title("
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
end_date = "2024-02-04"
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
st.write(data.head())
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
st.
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
main()
|
|
|
|
| 1 |
+
# Import necessary libraries
|
| 2 |
+
import pandas as pd
|
| 3 |
import yfinance as yf
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 6 |
+
from sklearn.metrics import mean_squared_error
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from datetime import datetime, timedelta, timezone
|
| 9 |
+
|
| 10 |
+
# Function to fetch historical stock data
|
| 11 |
+
def fetch_stock_data(symbol, start_date, end_date):
|
| 12 |
+
data = yf.download(symbol, start=start_date, end=end_date)
|
| 13 |
+
return data
|
| 14 |
+
|
| 15 |
+
# Function to create features for the model
|
| 16 |
+
def create_features(data):
|
| 17 |
+
data['Date'] = data.index
|
| 18 |
+
data['Year'] = data['Date'].dt.year
|
| 19 |
+
data['Month'] = data['Date'].dt.month
|
| 20 |
+
data['Day'] = data['Date'].dt.day
|
| 21 |
+
data['Hour'] = data['Date'].dt.hour
|
| 22 |
+
data['Minute'] = data['Date'].dt.minute
|
| 23 |
+
|
| 24 |
+
return data
|
| 25 |
+
|
| 26 |
+
# Function to train a machine learning model
|
| 27 |
+
def train_model(data):
|
| 28 |
+
features = ['Year', 'Month', 'Day', 'Hour', 'Minute']
|
| 29 |
+
target = 'Close'
|
| 30 |
+
|
| 31 |
+
X = data[features]
|
| 32 |
+
y = data[target]
|
| 33 |
+
|
| 34 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 35 |
+
|
| 36 |
+
model = RandomForestRegressor()
|
| 37 |
+
model.fit(X_train, y_train)
|
| 38 |
+
|
| 39 |
+
# Evaluate the model
|
| 40 |
+
predictions = model.predict(X_test)
|
| 41 |
+
mse = mean_squared_error(y_test, predictions)
|
| 42 |
+
st.write(f"Mean Squared Error: {mse}")
|
| 43 |
|
| 44 |
+
return model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Streamlit UI
|
| 47 |
def main():
|
| 48 |
+
st.title("Stock Price Prediction Tool")
|
| 49 |
|
| 50 |
+
symbol = st.text_input("Enter Stock Symbol (e.g., AAPL):")
|
| 51 |
+
start_date = st.date_input("Select Start Date:")
|
| 52 |
+
end_date = st.date_input("Select End Date:")
|
|
|
|
| 53 |
|
| 54 |
+
if st.button("Predict Stock Price"):
|
| 55 |
+
# Fetch stock data
|
| 56 |
+
stock_data = fetch_stock_data(symbol, start_date, end_date)
|
| 57 |
|
| 58 |
+
# Create features
|
| 59 |
+
stock_data = create_features(stock_data)
|
|
|
|
| 60 |
|
| 61 |
+
# Train the model
|
| 62 |
+
model = train_model(stock_data)
|
| 63 |
|
| 64 |
+
# Predict the stock price for a specific date (e.g., the last date in the dataset)
|
| 65 |
+
prediction_date = stock_data['Date'].iloc[-1]
|
| 66 |
+
prediction_features = [[
|
| 67 |
+
prediction_date.year,
|
| 68 |
+
prediction_date.month,
|
| 69 |
+
prediction_date.day,
|
| 70 |
+
prediction_date.hour,
|
| 71 |
+
prediction_date.minute
|
| 72 |
+
]]
|
| 73 |
+
predicted_price = model.predict(prediction_features)[0]
|
| 74 |
|
| 75 |
+
st.subheader(f"Predicted Stock Price on {prediction_date} (UTC):")
|
| 76 |
+
st.write(f"${predicted_price:.2f}")
|
| 77 |
|
| 78 |
+
# Run the Streamlit app
|
| 79 |
+
if __name__ == '__main__':
|
| 80 |
main()
|