Search This Blog

  • Stock Data Visualization Using Python

              

    Stock Data Visualization Using Python


    India is one of the fastest-growing economies in the world and the Indian stock market is an important component of the country's financial system. With the rise of data science and visualization tools, it has become easier to analyze and interpret stock market data. In this article, we will explore how to use Python to visualize Indian stock data.




    Getting the Data


    Before we can start visualizing the Indian stock data, we need to first obtain the data. There are several sources from which we can obtain this data. One popular source is the National Stock Exchange of India (NSE) website. The NSE provides historical stock data for all the companies listed on the exchange.


    To get started, we can use the nsepy package in Python. This package allows us to download historical data for Indian stocks from the NSE website.


    !pip install nsepy


    from datetime import date

    from nsepy import get_history


    start_date = date(2019, 1, 1)

    end_date = date(2022, 1, 1)


    stock_data = get_history(symbol='RELIANCE', start=start_date, end=end_date, index= False)


    In this code, we are using the get_history() function from the nsepy package to download the stock data for Reliance Industries Limited (RELIANCE) from January 1st, 2019 to January 1st, 2022.




    Cleaning the Data


    Once we have downloaded the data, we need to clean it before we can start visualizing it. This involves removing any missing or erroneous data and converting the data into a format that is suitable for visualization.


    import pandas as pd


    # Remove unnecessary columns

    stock_data = stock_data.drop(columns=['Series', 'Prev Close', 'Last', 'VWAP', 'Turnover', 'Trades'])


    # Convert Date column to datetime format

    stock_data['Date'] = pd.to_datetime(stock_data['Date'])


    In this code, we are removing the unnecessary columns from the stock data and converting the date column to a datetime format. We can now start visualizing the data.




    Line Chart


    The simplest and most commonly used visualization technique for stock data is a line chart. A line chart displays the trend of the stock prices over time.



    import matplotlib.pyplot as plt


    plt.plot(stock_data['Date'], stock_data['Close'])

    plt.xlabel('Date')

    plt.ylabel('Closing Price')

    plt.title('Reliance Industries Limited (RELIANCE) Closing Price from Jan 2019 to Jan 2022')

    plt.show()


    In this code, we are using the matplotlib library to create a line chart of the closing price of RELIANCE stock from January 2019 to January 2022.




    Candlestick Chart


    A candlestick chart is another popular visualization technique for stock data. A candlestick chart displays the opening, closing, high, and low prices of the stock for each day.


    pip install mplfinance


    from mplfinance.original_flavor import candlestick_ohlc

    import matplotlib.dates as mpl_dates


    # Convert Date column to mdates format

    stock_data['Date'] = stock_data['Date'].apply(mpl_dates.date2num)


    # Create subplots

    fig, ax = plt.subplots()


    # Format x-axis as dates

    fig.autofmt_xdate()


    # Create candlestick chart

    candlestick_ohlc(ax, stock_data[['Date', 'Open', 'High', 'Low', 'Close']].values, width=0.6, colorup='green', colordown='red')


    # Set labels and title

    ax.set_xlabel('Date')

    ax.set_ylabel('Price')

    ax.set_title('RELIANCE Chart from Jan 2019 to Jan 2022')


    # Show plot

    plt.show()


    In this code, we are using the mpl_finance library to create a candlestick chart of the opening, closing, high, and low prices of RELIANCE stock from January 2019 to January 2022. We are also using the matplotlib.dates library to format the x-axis as dates.




    Heatmap


    A heatmap is a graphical representation of data in which values are represented by colors. In the context of stock data, a heatmap can be used to visualize the correlation between different stocks.



    import seaborn as sns


    # Download stock data for HDFC Bank and Tata Steel

    hdfc_data = get_history(symbol='HDFCBANK', start=start_date, end=end_date, index=False)

    tata_data = get_history(symbol='TATASTEEL', start=start_date, end=end_date, index=  False)


    # Merge the stock data

    merged_data = pd.merge(stock_data, hdfc_data, on='Date')

    merged_data = pd.merge(merged_data, tata_data, on='Date')


    # Calculate the correlation matrix

    corr_matrix = merged_data.corr()


    # Create heatmap

    sns.heatmap(corr_matrix, annot=True, cmap='coolwarm')

    plt.title('Correlation Matrix for RELIANCE, HDFC Bank, and Tata Steel')

    plt.show()


    In this code, we are using the seaborn library to create a heatmap of the correlation between RELIANCE, HDFC Bank, and Tata Steel stocks. We first download the stock data for HDFC Bank and Tata Steel and merge it with the RELIANCE stock data. We then calculate the correlation matrix between the three stocks and create a heatmap of the matrix.




    Conclusion


    In this article, we explored how to use Python to visualize Indian stock data. We started by obtaining the data from the National Stock Exchange of India website using the nsepy package. We then cleaned the data and used various visualization techniques to analyze the data, including line charts, candlestick charts, and heatmaps.


    These visualization techniques can be used to gain insights into the stock market and make informed investment decisions. With the availability of data and powerful tools like Python, anyone can start exploring the stock market and making data-driven decisions.




  • 0 comments:

    Post a Comment

    Please do not enter any spam link in the comment box.

    DO YOU WANT MENTORSHIP?

    ADDRESS

    Delhi, India

    EMAIL

    admin@guptaharsh.in