-
Notifications
You must be signed in to change notification settings - Fork 0
/
Webscrape and Lin Reg
45 lines (35 loc) · 1.54 KB
/
Webscrape and Lin Reg
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
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from sklearn.linear_model import LinearRegression
#Defining graphing function
def make_graph(stock_data, stock):
fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Time Series Regression"), vertical_spacing = .3)
stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
#Add time series regression line
X = stock_data_specific.index.values.reshape(-1, 1)
y = stock_data_specific['Close'].values.reshape(-1, 1)
reg = LinearRegression().fit(X, y)
y_pred = reg.predict(X)
fig.add_trace(go.Scatter(x=stock_data_specific['Date'], y=y_pred.flatten(), name='Time Series Regression'), row=2, col=1)
fig.update_xaxes(title_text="Date", row=1, col=1)
fig.update_xaxes(title_text="Date", row=2, col=1)
fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
fig.update_layout(showlegend=False,
height=900,
title=stock,
xaxis_rangeslider_visible=True)
fig.show()
#Use yfinance to extract stock data
SPY = yf.Ticker("SPY")
SPY_data = SPY.history(period="max")
#reset the index
SPY_data.reset_index(inplace=True)
SPY_data.head()
#Plots graph with time series regression
make_graph(SPY_data, 'S&P 500')
#end