Stock Market Analysis means analyzing the current and historical trends in the stock market to make future buying and selling decisions. Stock market analysis is one of the best use cases of Data Science in finance. So, if you want to learn to analyze the stock market, this article is for you. In this article, I will take you through the task of Stock Market Analysis using Python.
Stock Market Analysis using Python
To analyze the stock market, I will collect the stock price data of Google. At the end of this article, you will learn to analyze the stock market interactively using the Python programming language. Let’s start by collecting the stock price data of Google. I will use the yfinance API of Yahoo Finance for collecting the stock price data. You can learn more about this API here.
Now here’s how to collect Google’s stock price data:
import pandas as pd import yfinance as yf import datetime from datetime import date, timedelta import plotly.graph_objects as go import plotly.express as px today = date.today() d1 = today.strftime("%Y-%m-%d") end_date = d1 d2 = date.today() - timedelta(days=365) d2 = d2.strftime("%Y-%m-%d") start_date = d2 data = yf.download('GOOG', start=start_date, end=end_date, progress=False) data["Date"] = data.index data = data[["Date", "Open", "High", "Low", "Close", "Adj Close", "Volume"]] data.reset_index(drop=True, inplace=True) print(data.head())
Date Open High Low Close Adj Close \ 0 2021-07-12 2596.669922 2615.399902 2592.000000 2611.280029 2611.280029 1 2021-07-13 2617.629883 2640.840088 2612.739990 2619.889893 2619.889893 2 2021-07-14 2638.030029 2659.919922 2637.959961 2641.649902 2641.649902 3 2021-07-15 2650.000000 2651.899902 2611.959961 2625.330078 2625.330078 4 2021-07-16 2632.820068 2643.659912 2616.429932 2636.909912 2636.909912 Volume 0 847200 1 830900 2 895600 3 829300 4 742800
Whenever you analyze the stock market, always start with a candlestick chart. A candlestick chart is a handy tool to analyze the price movements of stock prices. Here’s how you can visualize a candlestick chart of Google’s stock prices:
figure = go.Figure(data=[go.Candlestick(x=data["Date"], open=data["Open"], high=data["High"], low=data["Low"], close=data["Close"])]) figure.update_layout(title = "Google Stock Price Analysis", xaxis_rangeslider_visible=False) figure.show()
A bar plot is also a handy visualization to analyze the stock market, specifically in the long term. Here’s how to visualize the close prices of Google’s stock using a bar plot:
figure = px.bar(data, x = "Date", y= "Close") figure.show()
One of the valuable tools to analyze the stock market is a range slider. It helps you analyze the stock market between two specific points by interactively selecting the time period. Here’s how you can add a range-slider to analyze the stock market:
figure = px.line(data, x='Date', y='Close', title='Stock Market Analysis with Rangeslider') figure.update_xaxes(rangeslider_visible=True) figure.show()
Another interactive feature you can add for stock market analysis is time period selectors. Time period selectors are like buttons that show you the graph of a specific time period. For example, a year, three months, six months, etc. Here is how you can add buttons for selecting the time period for stock market analysis:
figure = px.line(data, x='Date', y='Close', title='Stock Market Analysis with Time Period Selectors') figure.update_xaxes( rangeselector=dict( buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=3, label="3m", step="month", stepmode="backward"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ]) ) ) figure.show()
The weekend or holiday season always affects the stock market. So if you want to remove all the records of the weekend trends from your stock market visualization, below is how you can do it:
figure = px.scatter(data, x='Date', y='Close', range_x=['2021-07-12', '2022-07-11'], title="Stock Market Analysis by Hiding Weekend Gaps") figure.update_xaxes( rangebreaks=[ dict(bounds=["sat", "sun"]) ] ) figure.show()
So that’s how you can analyze the stock market using Python. If you want to learn how to predict the stock market, you can learn here.
So this is how you can use the Python programming language to analyze the stock market interactively. Stock Market Analysis means analyzing the current and historical trends in the stock market to make future buying and selling decisions. I hope you liked this article on Stock Market Analysis using Python. Feel free to ask valuable questions in the comments section below.