- General file format
- Getting from daily stock details to a timeseries (not literally)
- The charting part
As part of my efforts at learning R, I stumbled upon the idea of charting Indian stocks listed on NSE. I already had some piece of code to get the stock information - written as part of pricing my portfolio account within GNUCash. GNUCash has the facility to write our own Perl modules to access different financial data.
In the case of National Stock Exchange, the historical market data (Open/Close/H/L/Volume etc.) are available freely on their website at www.nseindia.com. These market figures are called ‘bhav’ (value in Hindi) and the files themselves were called bhavcopy.
NOTE: If you are interested in following long, I have a few sample csv files and a R-Markdown file with the code on github.com
General file format
Each file for a given date is available as a zipped csv file. The zip file contain csv files named like cm23OCT2015bhav.csv
As an example, the contents are like below:
Getting from daily stock details to a timeseries (not literally)
To keep things simple, I wrote a small function to load ‘all’ csv files in a given directory and merging the contents into one huge dataframe. The function is shown below:
I keep this and similar utility functions in ~/.Rprofile
While checking the TIMESTAMP column in the frame, it is clear that it was loaded as plain text and needs to be converted to a Date. You can check the class of each column using
The charting part
Since this is a huge table with all the stock information, I have to filter out the ones corresponding to a given symbol and the plot it. In my case, I chose to use INFY stock (Infosys Technologies) and entire 2015 data.
Plotting a line chart shows a surprise drop in share price around mid-year. Turns out that was during a 1:1 bonus stock issue. Atleast, it is not due to a bug in my code!
To make things a bit more glamorous, I tried to plot the candle-chart. This turns out to be a bit more harder. We need to know the net-trend (up/down) to decide the color. In addition, we need the decide the rectangle for the candle body and then the length of the wicks. Much of the code is to figure out those numbers.
Let me know what you think. I am sure it is easier to get this view / info by visiting some popular charting sites. But then, I wouldnt be learning R :)comments powered by Disqus