The craze for cryptocurrencies and stock marketing is growing among today’s youth. The same craze motivated me to analyze the stock market data and develop some sort of tools like financial indicators and graph. In the search of the best tool for plotting data I found p5.js to be the best alternative to all the existing libraries , however I initially started with matplotlib. In that journey , I learnt a lot about the indicators and how a good strategy could be build using indicators and time-independent graphs. However we are not going to dive deep into this article. We will just learn how to download and analyze some sort of data from Bombay Stock Exchange (BSE) website and plot corresponding graphs.
Downloading data from BSE official website
To download data for analysis
Step1 : Go to the official website of BSE Historical data https://www.bseindia.com/markets/equity/EQReports/StockPrcHistori.aspx
Step2: Enter the name of the company (for e.g. ACC Ltd ) , enter the dates and press submit.
Step3: Now you will see a table showing symbol , closing price , opening price volume and a lot more columns. At the bottom of the page you will be able to see a download button ( as shown in figure ) press on the download button and your data will be downloaded as csv.
Requirements for plotting and analyzing data
If you are doing on a local machine you need to install following softwares:
- Anaconda or python
- Pandas & Numpy ( comes already installed with anaconda )
- Jupyter Notebook ( comes already installed with anaconda ) or VS Code
However there is an easier way to get started and that is Google Colab.
So what is Google Colab ?
well, Google Colab is a Co-Laboratory for data scientist where you can run , execute and share python code online. It provides you cloud computation and so your system speed is not going to determine speed of the execution of code.
Getting started in Google Colab
Few basic things you need to know before writing the actual code . Code in a colab notebook is written in cells and these cells run one by one sequentially. To create a cell below the current cell use ‘ b ’ key and for a cell above current cell press ‘ a ’ . However you can also use the graphical buttons and menus. You can learn more about google colab from their official getting started notebook. https://colab.research.google.com/notebooks/basic_features_overview.ipynb
Let’s write some code
Step1: Import all the required libraries for plotting the graph.
- Pandas for reading csv data as dataframe
- Matplotlib for plotting data as graphs
Step2: If you are using google Colab you can upload the downloaded file from BSE website using the side bar menu ( as shown in the picture below).
Read the data from the file using pd.read_csv() function after reading the file since we don’t want to mess up with a lot of data and this is only for learning purpose so plotting 100 days data would be sufficient . use df.head() function to select 100 rows from top.
Step3: Now we have data and we have to plot the data using matplotlib before doing this we need to assign colors to our candle. Let ‘s give our candle an initial color red and let’s loop through the data and change it to green wherever required. Next thing to do is to calculate the lower value where the lower value for drawing the rectangle will be the minimum of open and close price for the day. We also need to calculate the height for the rectangles. ( For more info read the next section )
Step4: Create a subplot with the 18x8 plot size. create two list one for plotting lines and other for rectangles ( A candle stick is made up of lines and rectangle, more theory in next section). add the lines and rectangle to plot with corresponding fill colors.
Step5: Set the Y label and X label for the data since there are 100 days data so labelling every day on x-axis will be a bad idea so we will be plotting every 10th date. Finally show the plot.
What are Candlestick Graphs ? How to plot ?
CandleStick graphs are the most common graph used in the finance world. It consists of a line showing ‘ High Price ’ and ‘ Low Price ’ and a rectangle showing the ‘ Open Price ’ and ‘ Close Price ’ for the day. The height of the rectangle is the difference between ‘ Open Price ’ and ‘ Low price ’ for the day.
Hope you like this blog . We are building open source library for indicators and graphs for stocks and also developing a software for algorithmic trading , stock screening and back testing bundled together with graphs and indicator. If you would like to contribute visit our GitHub repo https://github.com/kUNWAR-DIVYANSHU/Financial-Graph-Lib or join us on discord https://discord.gg/4KnV9qZ9 .
Thanks for reading.