Projects

Analyzing Historical Stock/Revenue Data and Building a Dashboard

DaTALK 2021. 4. 7.

- 직접 주식 data web scraping해서 history 변화 graph나타내는 project 하기! (toy project) 모든 주식 -

selenium 배우기

 

* Define Graphing Function

 

def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data.Date, infer_datetime_format=True), y=stock_data.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data.Date, infer_datetime_format=True), y=revenue_data.Revenue.astype("float"), name="Revenue"), 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_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()

 

* Use yfinance to extract Stock Data

 

tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
tesla_data.head()

 

we will use 'Date' & 'Close' column

 

* Use Webscraping to Extract Tesla Revenue Data

 

url = 'https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue'
html_data = requests.get(url).text

soup = BeautifulSoup(html_data,'html5lib')

tables = soup.find_all('table')
for index,table in enumerate(tables):
    if ("Tesla Quarterly Revenue" in str(table)):
        table_index = index
tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])

for row in tables[table_index].tbody.find_all("tr"):
    col = row.find_all("td")
    if (col != []):
        date = col[0].text
        revenue = col[1].text.replace("$", "").replace(",", "")
        tesla_revenue = tesla_revenue.append({"Date":date, "Revenue":revenue}, ignore_index=True)

# Remove the rows in the dataframe that are empty strings or are NaN in the Revenue column
tesla_revenue.dropna(inplace=True)
tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]

tesla_revenue

 

 

tesla_revenue.tail()

 

 

(+ extracting GME Stock Data and Revenue Data follows the same procedures as we've done in Tesla data)

 

* Plot Tesla & GameStop Stock Graph

 

make_graph(tesla_data, tesla_revenue, 'Tesla')

 

 

make_graph(gme_data, gme_revenue, 'GameStop')

 

* Conclusion

 

- As we see the results of the two TESLA graphs above(stock graph & revenue graph), it show similar up & down among those two graphs. As Revenue from TESLA starts to rise since 2013, the stock price begins to rise accordingly. But when we see the results of the two GameStop graphs above, we can guess the correlation between the stock price and the revenue has not always been the same as the TESLA's. Even though the Share price of GameStop has soared in 2020, the revenue has been fluctuated since 2005 for now. The GameStop's revenue has arised in the end while there has been many upside-down syndrome until 2009. After 2009, it dramatically fluctuated but in the end it has fallen to about $1000M. 

 

댓글