Close

Analysis and Visualization of Stock Performance

Performed Using Python And Webscraping


Overview

Scenario:
For this project, you will assume the role of a Data Scientist working for an investment firm that helps customers invest their money in stocks. Your job is to extract financial data like historical share price and quarterly revenue reportings from various sources using Python libraries and webscraping on popular stocks. After collecting this data you will visualize it in a dashboard to identify patterns or trends. The stocks we will work with are Tesla and GameStop.


Understanding Stock Shares and yfinance:
An investor can buy a stock and sell it later. If the stock price increases, the investor profits, If it decreases, the investor will incur a loss. Determining the stock price is complex; it depends on the number of outstanding shares, the size of the company's future profits, and much more. People trade stocks throughout the day. The stock ticker is a report of the price of a certain stock, updated continuously throughout the trading session by the various stock market exchanges. Use the y-finance API to obtain the stock ticker and extract information about the stock.


Steps:

Step 1: Import the necessary libraries

Libraries:
    - yfinance
    - pandas
    - requests
    - BeautifulSoup
    - plotly

Step 2: Use yfinance to Extract the Stock Data of Tesla

  1. Use yf.Ticker('TSLA') to extract Tesla stock data and save it into a variable called 'tesla'.
  2. Use the ticker object and the function history to extract stock information and save it in a dataframe named tesla_data. Set the period parameter to "max" so we get information for the maximum amount of time.
            tesla_data = tesla.history(period='max')

Step 3: Use Webscraping to Extract Tesla Revenue Data

  1. Save the URL below to a requests object
            url = https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue
  2. Save the text of the response as a variable named html_data.
  3. Parse the html data using beautiful_soup using the parser html.parser.
  4. Extract the table on the webpage and store it into a dataframe named tesla_revenue. The column names should be "Date" and "Revenue".
  5. Run the following line to remove the comma and dollar sign from the Revenue column.
            tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"")

Step 4: Use yfinance to Extract the Stock Data of GameStop

  1. Use yf.Ticker('GME') to extract GameStop stock data and save it into a variable called 'gamestop'.
  2. Use the ticker object and the function history to extract stock information and save it in a dataframe named gamestop_data. Set the period parameter to "max" so we get information for the maximum amount of time.
            gamestop_data = gamestop.history(period='max')

Step 5: Use Webscraping to Extract GameStop Revenue Data

  1. Save the URL below to a requests object
            url = https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue
  2. Save the text of the response as a variable named html_data.
  3. Parse the html data using beautiful_soup using the parser html.parser.
  4. Extract the table on the webpage and store it into a dataframe named gamestop_revenue. The column names should be "Date" and "Revenue".
  5. Run the following line to remove the comma and dollar sign from the Revenue column.
            gamestop["Revenue"] = gamestop['Revenue'].str.replace(',|\$',"")

Step 6: Create Graphs

Create a function called make_graph that takes 3 parameters: stock_data, revenue_data, and company_name.

The Tesla graph:

Tesla Stock Graph

Insights:

  • Since 2018, Tesla’s share price has seen a significant increase. This is a positive sign for potential investors.
  • The company's revenue shows a steady upward trend, indicating strong increasing sales.

Final Verdict:

The share price and revenue both increase over time, which suggests that Tesla’s stock is good to invest in.



The GameStop graph:

GameStop Stock Graph

Insights:

  • The price was stable for years but saw a sharp spike in 2021. Upon further research, this was due to the meme stock phenomenon rather than business growth.
  • GameStop’s revenue is cyclical and does not show consistent long-term growth, indicating potential business struggles. Furthermore, the spikes in revenue only occur towards the end of the year, during the holiday season. This suggests that the company generates its main income from holiday sales.

Final Verdict:

GameStop is a speculative play with high risk and uncertain future performance. Due to many years of inconsistency, GameStop’s stock is NOT good to invest in.