Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import numpy as np | |
from pandas_datareader import data | |
from datetime import date, datetime | |
from sklearn.model_selection import train_test_split | |
from sklearn.metrics import r2_score | |
def dateArrange(data): | |
dates = data['Date'].to_list() | |
epochDates = [] | |
for i in dates: | |
date_time = np.datetime64(i).astype(datetime) | |
splitDate = date_time.strftime("%Y-%#m-%d").split('-') | |
epochDate = datetime(int(splitDate[0]),int(splitDate[1]),int(splitDate[2]),0,0).timestamp() | |
epochDates.append(epochDate) | |
data['Date'] = epochDates | |
def dataScraper(ticker): | |
startdate = '2012-01-01' | |
today = date.today().strftime("%Y-%m-%d") | |
enddate = today | |
try: | |
panel_data = data.DataReader(ticker, 'yahoo', startdate, enddate).reset_index() | |
price_close = panel_data['Adj Close'] | |
price_20dma = price_close.rolling(window=20).mean().to_list()[100:] # 20 Day Moving Average | |
price_50dma = price_close.rolling(window=50).mean().to_list()[100:] # 50 day | |
price_100dma = price_close.rolling(window=100).mean().to_list()[100:] # 100 day | |
panel_data = panel_data.iloc[100: , :] | |
panel_data['20dma'] = price_20dma | |
panel_data['50dma'] = price_50dma | |
panel_data['100dma'] = price_100dma | |
dateArrange(panel_data) | |
return panel_data | |
except: | |
print("Error while scraping data") | |
return | |
def predictor(stock_ticker): | |
prediction_list = [] | |
stock_data = dataScraper(stock_ticker) | |
try: | |
stock_data.iat[0,0] | |
except: | |
print('Error with stock data') | |
return | |
from sklearn.linear_model import Lasso | |
alpha = 1.0 | |
tol = 0.0008 | |
max_iter = 10000 | |
lasso = Lasso(alpha=alpha, max_iter=max_iter, tol=tol) | |
test_size = 0.1 | |
print('\nModel = ' + str(lasso)) | |
print('\n~ ' + stock_ticker.upper() + ' Next Day Price Predictions ~\n') | |
X = stock_data.iloc[:-1 , :] | |
y = { | |
'High' : stock_data.iloc[1: , :]['High'], | |
'Low' : stock_data.iloc[1: , :]['Low'], | |
'Close (Adjusted)': stock_data.iloc[1: , :]['Close'] | |
} | |
sample = stock_data.iloc[-1:, :] | |
for i in y: | |
X_train, X_test, y_train, y_test = train_test_split(X, y[i], test_size=test_size) | |
y_pred_lasso = lasso.fit(np.array(X_train), np.array(y_train)) | |
r2_score_lasso = r2_score(np.array(y_test), y_pred_lasso.predict(np.array(X_test))) | |
prediction = y_pred_lasso.predict(np.array(sample)) | |
prediction_list.append(prediction) | |
output = "<h5 style='text-align: center; color: #0E7600;'>" + i + ' - ' + str(prediction) + '\n' + "</h1>" | |
st.markdown(output, unsafe_allow_html=True) | |
prediction_list.append(lasso) | |
prediction_list.append(r2_score_lasso) | |
r2score = 'R2 = ' + str(r2_score_lasso) | |
print(r2score) | |
st.markdown("<h1 style='text-align: center; color: #2BD314;'>Stock Price Predictor v2.0</h1>", unsafe_allow_html=True) | |
st.markdown("<h6 style='text-align: center; color: #094B00;'>by Bryan Mildort</h1>", unsafe_allow_html=True) | |
ticker = st.text_input('Enter Ticker to Scrape:', placeholder='SPY') | |
col1, col2, col3, col4, col5 = st.columns(5) | |
with col1: | |
st.write(' ') | |
with col2: | |
st.write(' ') | |
with col3: | |
if st.button('Scrape!'): | |
predictor(ticker) | |
with col4: | |
st.write(' ') | |
with col5: | |
st.write(' ') | |