-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamlit_app.py
132 lines (106 loc) · 4.11 KB
/
streamlit_app.py
1
2
3
4
5
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
42
43
44
45
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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import streamlit as st
import pandas as pd
import numpy as np
import yfinance as yf
import plotly.express as px
import matplotlib.pyplot as plt
from keras.models import load_model
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
# App 1: Stock Market Trend Prediction Using Sentiment Analysis
st.title("Stock Market Trend Prediction Using Sentiment Analysis")
ticker = st.sidebar.text_input('Ticker')
start_date = st.sidebar.date_input('Start Date')
end_date = st.sidebar.date_input('End Date')
# Check if the ticker and start date are provided
if ticker and start_date:
# Fetch historical stock data
data = yf.download(ticker, start=start_date, end=end_date)
# Check if data is available
if not data.empty:
# Display the fetched data in a line chart
fig = px.line(data, x=data.index, y='Close', title=f'{ticker} Stock Price')
st.plotly_chart(fig)
pricing_data, predictions = st.tabs(["Pricing Data", "Predictions"])
with pricing_data:
st.header("Price Movement")
data2 = data.copy()
data2['% Change'] = data['Adj Close'] / data2['Adj Close'].shift(1)
data2.dropna(inplace=True)
st.write(data2)
annual_return = data2['% Change'].mean() * 252 * 100 - 1
st.write('Annual Return:', annual_return, '%')
stdev = np.std(data2['% Change']) * np.sqrt(252)
st.write('Standard Deviation:', stdev * 100, '%')
st.write('Risk-Adjusted Return:', annual_return / (stdev * 100))
with predictions:
st.header("Predictions")
# Display the fetched data in a line chart
st.plotly_chart(fig)
else:
st.write("No data available for the specified ticker and date range.")
else:
st.write("Please enter a valid ticker and start date.")
# App 2: Stock Trend Prediction
st.title("Stock Trend Prediction")
start = "2011-02-01"
end = "2019-12-31"
user_input = st.text_input("Enter Stock Ticker", "AAPL")
df = yf.download(user_input, start=start, end=end)
df = df.reset_index()
df = df.dropna()
# Describing data
st.subheader('Data from 2011-2019')
st.write("Description")
st.write(df.describe())
# Visualization
st.subheader("Closing Price vs Time Chart")
fig = plt.figure(figsize=(12, 6))
plt.plot(df.Close)
st.pyplot(fig)
st.subheader("Closing Price vs Time Chart with 100MA")
ma100 = df.Close.rolling(100).mean()
fig = plt.figure(figsize=(12, 6))
plt.plot(ma100)
plt.plot(df.Close)
st.pyplot(fig)
st.subheader("Closing Price vs Time Chart with 100MA and 200MA")
ma100 = df.Close.rolling(100).mean()
ma200 = df.Close.rolling(200).mean()
fig = plt.figure(figsize=(12, 6))
plt.plot(ma100, "b")
plt.plot(ma200, "g")
plt.plot(df.Close)
st.pyplot(fig)
data_training = pd.DataFrame(df["Close"][0:int(len(df) * 0.70)])
data_testing = pd.DataFrame(df["Close"][int(len(df) * 0.70):int(len(df))])
if len(data_testing) > 0:
scaler = MinMaxScaler(feature_range=(0, 1))
data_training_array = scaler.fit_transform(data_training)
model = load_model("stock_sentiment_model.pt.h5")
# Feeding model with past 100 days of data
# Testing part
past_100_days = data_training.tail(100)
final_df = past_100_days.append(data_testing, ignore_index=True)
input_data = scaler.transform(final_df)
x_test = []
y_test = []
for i in range(100, input_data.shape[0]):
x_test.append(input_data[i - 100:i])
y_test.append(input_data[i, 0])
x_test, y_test = np.array(x_test), np.array(y_test)
y_pred = model.predict(x_test)
scale_factor = 1 / 0.13513514
y_pred = y_pred * scale_factor
y_test = y_test * scale_factor
# Final graph
st.subheader("Prediction vs Original")
fig2 = plt.figure(figsize=(12, 6))
plt.plot(y_pred, 'r', label='Predicted Price')
plt.plot(y_test, 'b', label='Original Price')
plt.xlabel('Time')
plt.ylabel('Price')
plt.legend()
st.pyplot(fig2)
else:
st.write("Insufficient data for testing.")