Spaces:
Running
Running
Knight-coderr
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,135 +1,136 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
from textblob import TextBlob
|
4 |
-
import joblib
|
5 |
-
import matplotlib.pyplot as plt
|
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 |
-
merged_data['
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
merged_data['
|
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 |
-
st.write(
|
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 |
-
# Prepare prediction data for display
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
'
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
st.
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
plt.
|
131 |
-
plt.
|
132 |
-
plt.
|
133 |
-
plt.
|
134 |
-
plt.
|
135 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from textblob import TextBlob
|
4 |
+
import joblib
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import datetime
|
7 |
+
|
8 |
+
# Load the data
|
9 |
+
@st.cache_data
|
10 |
+
def load_data():
|
11 |
+
stock_data = pd.read_csv('data/stock_yfinance_data.csv')
|
12 |
+
tweets_data = pd.read_csv('data/stock_tweets.csv')
|
13 |
+
|
14 |
+
# Convert the Date columns to datetime
|
15 |
+
stock_data['Date'] = pd.to_datetime(stock_data['Date'])
|
16 |
+
tweets_data['Date'] = pd.to_datetime(tweets_data['Date']).dt.date
|
17 |
+
|
18 |
+
# Perform sentiment analysis on tweets
|
19 |
+
def get_sentiment(tweet):
|
20 |
+
analysis = TextBlob(tweet)
|
21 |
+
return analysis.sentiment.polarity
|
22 |
+
|
23 |
+
tweets_data['Sentiment'] = tweets_data['Tweet'].apply(get_sentiment)
|
24 |
+
|
25 |
+
# Aggregate sentiment by date and stock
|
26 |
+
daily_sentiment = tweets_data.groupby(['Date', 'Stock Name']).mean(numeric_only=True).reset_index()
|
27 |
+
|
28 |
+
# Convert the Date column in daily_sentiment to datetime64[ns]
|
29 |
+
daily_sentiment['Date'] = pd.to_datetime(daily_sentiment['Date'])
|
30 |
+
|
31 |
+
# Merge stock data with sentiment data
|
32 |
+
merged_data = pd.merge(stock_data, daily_sentiment, how='left', left_on=['Date', 'Stock Name'], right_on=['Date', 'Stock Name'])
|
33 |
+
|
34 |
+
# Fill missing sentiment values with 0 (neutral sentiment)
|
35 |
+
merged_data['Sentiment'].fillna(0, inplace=True)
|
36 |
+
|
37 |
+
# Sort the data by date
|
38 |
+
merged_data.sort_values(by='Date', inplace=True)
|
39 |
+
|
40 |
+
# Create lagged features
|
41 |
+
merged_data['Prev_Close'] = merged_data.groupby('Stock Name')['Close'].shift(1)
|
42 |
+
merged_data['Prev_Sentiment'] = merged_data.groupby('Stock Name')['Sentiment'].shift(1)
|
43 |
+
|
44 |
+
# Create moving averages
|
45 |
+
merged_data['MA7'] = merged_data.groupby('Stock Name')['Close'].transform(lambda x: x.rolling(window=7).mean())
|
46 |
+
merged_data['MA14'] = merged_data.groupby('Stock Name')['Close'].transform(lambda x: x.rolling(window=14).mean())
|
47 |
+
|
48 |
+
# Create daily price changes
|
49 |
+
merged_data['Daily_Change'] = merged_data['Close'] - merged_data['Prev_Close']
|
50 |
+
|
51 |
+
# Create volatility
|
52 |
+
merged_data['Volatility'] = merged_data.groupby('Stock Name')['Close'].transform(lambda x: x.rolling(window=7).std())
|
53 |
+
|
54 |
+
# Drop rows with missing values
|
55 |
+
merged_data.dropna(inplace=True)
|
56 |
+
|
57 |
+
return merged_data
|
58 |
+
|
59 |
+
data = load_data()
|
60 |
+
stock_names = data['Stock Name'].unique()
|
61 |
+
|
62 |
+
# Load the best model
|
63 |
+
model_filename = 'model/best_model.pkl'
|
64 |
+
model = joblib.load(model_filename)
|
65 |
+
|
66 |
+
st.title("Stock Price Prediction Using Sentiment Analysis")
|
67 |
+
|
68 |
+
# User input for stock data
|
69 |
+
st.header("Input Stock Data")
|
70 |
+
selected_stock = st.selectbox("Select Stock Name", stock_names)
|
71 |
+
days_to_predict = st.number_input("Number of Days to Predict",
|
72 |
+
min_value=1, max_value=30, value=10)
|
73 |
+
|
74 |
+
# Get the latest data for the selected stock
|
75 |
+
latest_data = data[data['Stock Name'] == selected_stock].iloc[-1]
|
76 |
+
prev_close = latest_data['Close']
|
77 |
+
prev_sentiment = latest_data['Sentiment']
|
78 |
+
ma7 = latest_data['MA7']
|
79 |
+
ma14 = latest_data['MA14']
|
80 |
+
daily_change = latest_data['Daily_Change']
|
81 |
+
volatility = latest_data['Volatility']
|
82 |
+
|
83 |
+
# Display the latest stock data in a table
|
84 |
+
latest_data_df = pd.DataFrame({
|
85 |
+
'Metric': ['Previous Close Price', 'Previous Sentiment', '7-day Moving Average', '14-day Moving Average', 'Daily Change', 'Volatility'],
|
86 |
+
'Value': [prev_close, prev_sentiment, ma7, ma14, daily_change, volatility]
|
87 |
+
})
|
88 |
+
|
89 |
+
st.write("Latest Stock Data:")
|
90 |
+
st.write(latest_data_df)
|
91 |
+
|
92 |
+
st.write("Use the inputs above to predict the next days close prices of the stock.")
|
93 |
+
if st.button("Predict"):
|
94 |
+
predictions = []
|
95 |
+
latest_date = datetime.datetime.now()
|
96 |
+
|
97 |
+
for i in range(days_to_predict):
|
98 |
+
X_future = pd.DataFrame({
|
99 |
+
'Prev_Close': [prev_close],
|
100 |
+
'Prev_Sentiment': [prev_sentiment],
|
101 |
+
'MA7': [ma7],
|
102 |
+
'MA14': [ma14],
|
103 |
+
'Daily_Change': [daily_change],
|
104 |
+
'Volatility': [volatility]
|
105 |
+
})
|
106 |
+
|
107 |
+
next_day_prediction = model.predict(X_future)[0]
|
108 |
+
predictions.append(next_day_prediction)
|
109 |
+
|
110 |
+
# Update features for next prediction
|
111 |
+
prev_close = next_day_prediction
|
112 |
+
ma7 = (ma7 * 6 + next_day_prediction) / 7 # Simplified rolling calculation
|
113 |
+
ma14 = (ma14 * 13 + next_day_prediction) / 14 # Simplified rolling calculation
|
114 |
+
daily_change = next_day_prediction - prev_close
|
115 |
+
|
116 |
+
# st.write(f"Predicted next {days_to_predict} days close prices: {predictions}")
|
117 |
+
# Prepare prediction data for display
|
118 |
+
# Prepare prediction data for display
|
119 |
+
prediction_dates = pd.date_range(start=latest_date + pd.Timedelta(days=1), periods=days_to_predict)
|
120 |
+
prediction_df = pd.DataFrame({
|
121 |
+
'Date': prediction_dates,
|
122 |
+
'Predicted Close Price': predictions
|
123 |
+
})
|
124 |
+
|
125 |
+
st.subheader("Predicted Prices")
|
126 |
+
st.write(prediction_df)
|
127 |
+
|
128 |
+
# Plotting the results
|
129 |
+
st.subheader("Prediction Chart")
|
130 |
+
plt.figure(figsize=(10, 6))
|
131 |
+
plt.plot(prediction_df['Date'], prediction_df['Predicted Close Price'], marker='o', linestyle='--', label="Predicted Close Price")
|
132 |
+
plt.xlabel("Date")
|
133 |
+
plt.ylabel("Close Price")
|
134 |
+
plt.title(f"{selected_stock} Predicted Close Prices")
|
135 |
+
plt.legend()
|
136 |
+
st.pyplot(plt)
|