Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ from sklearn.preprocessing import StandardScaler
|
|
7 |
from sklearn.model_selection import train_test_split
|
8 |
from datetime import datetime, timedelta
|
9 |
import numpy as np
|
10 |
-
import matplotlib.pyplot as plt
|
11 |
|
12 |
# Function to compute RSI
|
13 |
def compute_rsi(data, window):
|
@@ -21,27 +20,30 @@ def compute_rsi(data, window):
|
|
21 |
return rsi
|
22 |
|
23 |
# Set up the Streamlit app
|
24 |
-
st.title("
|
25 |
-
st.write("This app uses historical data to predict future
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
30 |
|
31 |
# Display the historical data
|
32 |
-
st.write("Historical
|
33 |
-
st.dataframe(
|
34 |
|
35 |
# Feature engineering
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
# Prepare features and target variable
|
43 |
-
X =
|
44 |
-
y =
|
45 |
|
46 |
# Split the data
|
47 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
@@ -60,7 +62,7 @@ rf_model = RandomForestRegressor(n_estimators=100, random_state=42)
|
|
60 |
rf_model.fit(X_train_scaled, y_train)
|
61 |
|
62 |
# Predict future prices using ensemble method
|
63 |
-
future_dates = [
|
64 |
future_df = pd.DataFrame(index=future_dates, columns=X.columns)
|
65 |
future_df = future_df.fillna(method='ffill')
|
66 |
|
@@ -74,7 +76,7 @@ combined_predictions = (lr_predictions + rf_predictions) / 2
|
|
74 |
# Display predictions
|
75 |
predictions_df = pd.DataFrame({'Date': future_dates, 'Predicted Close': combined_predictions})
|
76 |
predictions_df.set_index('Date', inplace=True)
|
77 |
-
st.write("Future
|
78 |
st.dataframe(predictions_df)
|
79 |
|
80 |
# Plot the predictions
|
|
|
7 |
from sklearn.model_selection import train_test_split
|
8 |
from datetime import datetime, timedelta
|
9 |
import numpy as np
|
|
|
10 |
|
11 |
# Function to compute RSI
|
12 |
def compute_rsi(data, window):
|
|
|
20 |
return rsi
|
21 |
|
22 |
# Set up the Streamlit app
|
23 |
+
st.title("Stock Price Prediction")
|
24 |
+
st.write("This app uses historical data to predict future stock prices.")
|
25 |
|
26 |
+
# User input for stock ticker symbol
|
27 |
+
ticker = st.text_input("Enter the stock ticker symbol:", value='AAPL')
|
28 |
+
|
29 |
+
# Fetch stock historical data using yfinance
|
30 |
+
stock_data = yf.download(ticker, start='2020-01-01', end=datetime.today().strftime('%Y-%m-%d'))
|
31 |
+
stock_data.reset_index(inplace=True)
|
32 |
|
33 |
# Display the historical data
|
34 |
+
st.write(f"Historical Data for {ticker}")
|
35 |
+
st.dataframe(stock_data.tail())
|
36 |
|
37 |
# Feature engineering
|
38 |
+
stock_data['MA_10'] = stock_data['Close'].rolling(window=10).mean()
|
39 |
+
stock_data['MA_50'] = stock_data['Close'].rolling(window=50).mean()
|
40 |
+
stock_data['RSI'] = compute_rsi(stock_data['Close'], window=14)
|
41 |
+
stock_data['Return'] = stock_data['Close'].pct_change()
|
42 |
+
stock_data.dropna(inplace=True)
|
43 |
|
44 |
# Prepare features and target variable
|
45 |
+
X = stock_data[['Open', 'High', 'Low', 'Volume', 'MA_10', 'MA_50', 'RSI', 'Return']]
|
46 |
+
y = stock_data['Close']
|
47 |
|
48 |
# Split the data
|
49 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
|
|
62 |
rf_model.fit(X_train_scaled, y_train)
|
63 |
|
64 |
# Predict future prices using ensemble method
|
65 |
+
future_dates = [stock_data['Date'].iloc[-1] + timedelta(days=x) for x in range(1, 15)]
|
66 |
future_df = pd.DataFrame(index=future_dates, columns=X.columns)
|
67 |
future_df = future_df.fillna(method='ffill')
|
68 |
|
|
|
76 |
# Display predictions
|
77 |
predictions_df = pd.DataFrame({'Date': future_dates, 'Predicted Close': combined_predictions})
|
78 |
predictions_df.set_index('Date', inplace=True)
|
79 |
+
st.write(f"Future Price Predictions for {ticker}")
|
80 |
st.dataframe(predictions_df)
|
81 |
|
82 |
# Plot the predictions
|