Spaces:
Configuration error
Configuration error
shubham5027
commited on
Upload 5 files
Browse files- Procfile +1 -0
- README.md +92 -12
- app.py +153 -0
- requirements.txt +11 -0
- setup.sh +10 -0
Procfile
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
web: sh setup.sh && streamlit run app.py
|
README.md
CHANGED
@@ -1,13 +1,93 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Stock Price Prediction App
|
2 |
+
|
3 |
+
Welcome to the Stock Price Prediction App! This app allows you to visualize stock price data, explore technical indicators, and make short-term price predictions using machine learning models.
|
4 |
+
|
5 |
+
Created and designed by [Vikas Sharma](https://www.linkedin.com/in/vikas-sharma005/).
|
6 |
+
|
7 |
+
## Table of Contents
|
8 |
+
|
9 |
+
- [Description](#description)
|
10 |
+
- [Features](#features)
|
11 |
+
- [Setup](#setup)
|
12 |
+
- [Usage](#usage)
|
13 |
+
- [Technologies](#technologies)
|
14 |
+
- [License](#license)
|
15 |
+
|
16 |
+
## Description
|
17 |
+
|
18 |
+
The Stock Price Prediction App is a Streamlit-based web application that provides users with tools to analyze historical stock price data, visualize technical indicators, and make short-term price predictions using different machine learning models.
|
19 |
+
|
20 |
+
## Features
|
21 |
+
|
22 |
+
- **Visualize Technical Indicators**: Explore various technical indicators such as Bollinger Bands, MACD, RSI, SMA, and EMA to gain insights into stock price trends.
|
23 |
+
|
24 |
+
- **Recent Data Display**: View the most recent data of the selected stock, including the last 10 data points.
|
25 |
+
|
26 |
+
- **Price Prediction**: Predict future stock prices using machine learning models including Linear Regression, Random Forest Regressor, Extra Trees Regressor, KNeighbors Regressor, and XGBoost Regressor.
|
27 |
+
|
28 |
+
## Setup
|
29 |
+
|
30 |
+
1. Clone the repository:
|
31 |
+
```sh
|
32 |
+
git clone https://github.com/vikasharma005/Stock-Price-Prediction.git
|
33 |
+
```
|
34 |
+
|
35 |
+
2. Navigate to the project directory:
|
36 |
+
```sh
|
37 |
+
cd stock-price-prediction-app
|
38 |
+
```
|
39 |
|
40 |
+
3. Install the required Python packages using pip:
|
41 |
+
```sh
|
42 |
+
pip install -r requirements.txt
|
43 |
+
```
|
44 |
+
|
45 |
+
## Usage
|
46 |
+
|
47 |
+
1. Run the Streamlit app:
|
48 |
+
```sh
|
49 |
+
streamlit run app.py
|
50 |
+
```
|
51 |
+
|
52 |
+
2. The app will open in your default web browser. Use the sidebar to choose options for visualization, recent data display, or making price predictions.
|
53 |
+
|
54 |
+
3. Follow the on-screen instructions to input the stock symbol, select a date range, and choose technical indicators or prediction models.
|
55 |
+
|
56 |
+
## Technologies
|
57 |
+
|
58 |
+
- Python
|
59 |
+
- Streamlit
|
60 |
+
- pandas
|
61 |
+
- yfinance
|
62 |
+
- ta (Technical Analysis Library)
|
63 |
+
- scikit-learn
|
64 |
+
- XGBoost
|
65 |
+
|
66 |
+
## Author
|
67 |
+
|
68 |
+
<div id="header" align="center">
|
69 |
+
<img src="https://media.giphy.com/media/M9gbBd9nbDrOTu1Mqx/giphy.gif" width="100"/>
|
70 |
+
</div>
|
71 |
+
|
72 |
+
<h3 align="center">Hi there 👋, I'm Vikas</h3>
|
73 |
+
<h4 align="center">Just learning New Skills😀</h4>
|
74 |
+
|
75 |
+
<div id="socials" align="center">
|
76 |
+
<a href="https://www.linkedin.com/in/vikas-sharma005">
|
77 |
+
<img src="https://user-images.githubusercontent.com/76098066/186728913-a66ef85f-4644-4e3a-b847-98309c8cff42.svg">
|
78 |
+
</a>
|
79 |
+
<a href="https://www.instagram.com/_thisisvikas">
|
80 |
+
<img src="https://user-images.githubusercontent.com/76098066/186728908-f1a9919a-f4b2-4262-9515-683e77f8aabf.svg">
|
81 |
+
</a>
|
82 |
+
<a href="https://twitter.com/hitechvikas05">
|
83 |
+
<img src="https://user-images.githubusercontent.com/76098066/186728901-a4d90f01-2cdf-45c1-a1b3-73467c3d2698.svg">
|
84 |
+
</a>
|
85 |
+
</div>
|
86 |
+
|
87 |
+
You can find more about me and my projects on my [GitHub profile](https://github.com/vikasharma005).
|
88 |
+
|
89 |
+
## License
|
90 |
+
|
91 |
+
This project is licensed under the [MIT License](LICENSE).
|
92 |
+
|
93 |
+
---
|
app.py
ADDED
@@ -0,0 +1,153 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import pandas as pd
|
4 |
+
import yfinance as yf
|
5 |
+
from ta.volatility import BollingerBands
|
6 |
+
from ta.trend import MACD, EMAIndicator, SMAIndicator
|
7 |
+
from ta.momentum import RSIIndicator
|
8 |
+
import datetime
|
9 |
+
from datetime import date
|
10 |
+
from sklearn.preprocessing import StandardScaler
|
11 |
+
from sklearn.model_selection import train_test_split
|
12 |
+
from sklearn.linear_model import LinearRegression
|
13 |
+
from sklearn.neighbors import KNeighborsRegressor
|
14 |
+
from xgboost import XGBRegressor
|
15 |
+
from sklearn.ensemble import RandomForestRegressor
|
16 |
+
from sklearn.ensemble import ExtraTreesRegressor
|
17 |
+
from sklearn.metrics import r2_score, mean_absolute_error,accuracy_score
|
18 |
+
|
19 |
+
|
20 |
+
st.title('Stock Price Analysis')
|
21 |
+
st.sidebar.info('Welcome to the Stock Price Prediction App. Choose your options below')
|
22 |
+
|
23 |
+
|
24 |
+
def main():
|
25 |
+
option = st.sidebar.selectbox('Make a choice', ['Visualize','Recent Data', 'Predict'])
|
26 |
+
if option == 'Visualize':
|
27 |
+
tech_indicators()
|
28 |
+
elif option == 'Recent Data':
|
29 |
+
dataframe()
|
30 |
+
else:
|
31 |
+
predict()
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
@st.cache_resource
|
36 |
+
def download_data(op, start_date, end_date):
|
37 |
+
df = yf.download(op, start=start_date, end=end_date, progress=False)
|
38 |
+
return df
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
option = st.sidebar.text_input('Enter a Stock Symbol', value='SPY')
|
43 |
+
option = option.upper()
|
44 |
+
today = datetime.date.today()
|
45 |
+
duration = st.sidebar.number_input('Enter the duration', value=3000)
|
46 |
+
before = today - datetime.timedelta(days=duration)
|
47 |
+
start_date = st.sidebar.date_input('Start Date', value=before)
|
48 |
+
end_date = st.sidebar.date_input('End date', today)
|
49 |
+
if st.sidebar.button('Send'):
|
50 |
+
if start_date < end_date:
|
51 |
+
st.sidebar.success('Start date: `%s`\n\nEnd date: `%s`' %(start_date, end_date))
|
52 |
+
download_data(option, start_date, end_date)
|
53 |
+
else:
|
54 |
+
st.sidebar.error('Error: End date must fall after start date')
|
55 |
+
|
56 |
+
|
57 |
+
|
58 |
+
|
59 |
+
data = download_data(option, start_date, end_date)
|
60 |
+
scaler = StandardScaler()
|
61 |
+
|
62 |
+
def tech_indicators():
|
63 |
+
st.header('Technical Indicators')
|
64 |
+
option = st.radio('Choose a Technical Indicator to Visualize', ['Close', 'BB', 'MACD', 'RSI', 'SMA', 'EMA'])
|
65 |
+
|
66 |
+
bb_indicator = BollingerBands(data.Close)
|
67 |
+
bb = data
|
68 |
+
bb['bb_h'] = bb_indicator.bollinger_hband()
|
69 |
+
bb['bb_l'] = bb_indicator.bollinger_lband()
|
70 |
+
bb = bb[['Close', 'bb_h', 'bb_l']]
|
71 |
+
macd = MACD(data.Close).macd()
|
72 |
+
rsi = RSIIndicator(data.Close).rsi()
|
73 |
+
sma = SMAIndicator(data.Close, window=14).sma_indicator()
|
74 |
+
ema = EMAIndicator(data.Close).ema_indicator()
|
75 |
+
|
76 |
+
if option == 'Close':
|
77 |
+
st.write('Close Price')
|
78 |
+
st.line_chart(data.Close)
|
79 |
+
elif option == 'BB':
|
80 |
+
st.write('BollingerBands')
|
81 |
+
st.line_chart(bb)
|
82 |
+
elif option == 'MACD':
|
83 |
+
st.write('Moving Average Convergence Divergence')
|
84 |
+
st.line_chart(macd)
|
85 |
+
elif option == 'RSI':
|
86 |
+
st.write('Relative Strength Indicator')
|
87 |
+
st.line_chart(rsi)
|
88 |
+
elif option == 'SMA':
|
89 |
+
st.write('Simple Moving Average')
|
90 |
+
st.line_chart(sma)
|
91 |
+
else:
|
92 |
+
st.write('Expoenetial Moving Average')
|
93 |
+
st.line_chart(ema)
|
94 |
+
|
95 |
+
|
96 |
+
def dataframe():
|
97 |
+
st.header('Recent Data')
|
98 |
+
st.dataframe(data.tail(10))
|
99 |
+
|
100 |
+
|
101 |
+
|
102 |
+
def predict():
|
103 |
+
model = st.radio('Choose a model', ['LinearRegression', 'RandomForestRegressor', 'ExtraTreesRegressor', 'KNeighborsRegressor', 'XGBoostRegressor'])
|
104 |
+
num = st.number_input('How many days forecast?', value=5)
|
105 |
+
num = int(num)
|
106 |
+
if st.button('Predict'):
|
107 |
+
if model == 'LinearRegression':
|
108 |
+
engine = LinearRegression()
|
109 |
+
model_engine(engine, num)
|
110 |
+
elif model == 'RandomForestRegressor':
|
111 |
+
engine = RandomForestRegressor()
|
112 |
+
model_engine(engine, num)
|
113 |
+
elif model == 'ExtraTreesRegressor':
|
114 |
+
engine = ExtraTreesRegressor()
|
115 |
+
model_engine(engine, num)
|
116 |
+
elif model == 'KNeighborsRegressor':
|
117 |
+
engine = KNeighborsRegressor()
|
118 |
+
model_engine(engine, num)
|
119 |
+
else:
|
120 |
+
engine = XGBRegressor()
|
121 |
+
model_engine(engine, num)
|
122 |
+
|
123 |
+
|
124 |
+
def model_engine(model, num):
|
125 |
+
df = data[['Close']]
|
126 |
+
df['preds'] = data.Close.shift(-num)
|
127 |
+
x = df.drop(['preds'], axis=1).values
|
128 |
+
x = scaler.fit_transform(x)
|
129 |
+
x_forecast = x[-num:]
|
130 |
+
|
131 |
+
x = x[:-num]
|
132 |
+
|
133 |
+
y = df.preds.values
|
134 |
+
|
135 |
+
y = y[:-num]
|
136 |
+
|
137 |
+
|
138 |
+
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.2, random_state=7)
|
139 |
+
model.fit(x_train, y_train)
|
140 |
+
preds = model.predict(x_test)
|
141 |
+
st.text(f'R2_SCORE: {r2_score(y_test, preds)} \
|
142 |
+
\nMAE: {mean_absolute_error(y_test, preds)}'
|
143 |
+
)
|
144 |
+
|
145 |
+
|
146 |
+
forecast_pred = model.predict(x_forecast)
|
147 |
+
day = 1
|
148 |
+
for i in forecast_pred:
|
149 |
+
st.text(f'Day {day}: {i}')
|
150 |
+
day += 1
|
151 |
+
|
152 |
+
if __name__ == '__main__':
|
153 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
scikit-learn
|
3 |
+
yfinance
|
4 |
+
pandas
|
5 |
+
ta
|
6 |
+
xgboost
|
7 |
+
numpy
|
8 |
+
Pillow
|
9 |
+
tensorflow
|
10 |
+
opencv-python-headless
|
11 |
+
|
setup.sh
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
mkdir -p ~/.streamlit/
|
2 |
+
|
3 |
+
|
4 |
+
echo "\
|
5 |
+
[server]\n\
|
6 |
+
port = $PORT\n\
|
7 |
+
enableCORS = false\n\
|
8 |
+
headless = true\n\
|
9 |
+
\n\
|
10 |
+
" > ~/.streamlit/config.toml
|