Categories
Coding

Plotting Yahoo! Stock Charts with Gnuplot

Why can’t Gnuplot handle CSV file properly?

If you have ever gone to Yahoo! Finance and downloaded the historical share prices as a CSV file, a logical next step is to plot the data contained within the file. I recently used gnuplot to do some plotting of these types of files, but ran into a couple of issues. Firstly, gnuplot can’t handle CSV files correctly (unless I’m missing something obvious), at least, not CSV files with date information in them. A typical CSV download looks like this:

Date,Open,High,Low,Close,Volume,Adj. Close*
7-Oct-05,2025.00,2070.00,2070.00,2070.00,237,2070.00
6-Oct-05,2025.00,2070.00,1980.00,2070.00,1073,2070.00

In order to get gnuplot to read this, I had to do a couple of things. Firstly, I had to explicitly tell gnuplot that I was using a date-type variable for the x-axis (found here):

set xdata time
set timefmt “%d-%b-%y”
set format x “%b %d”

The next thing I needed to do was use the inbuilt capability of gnuplot to pass a file through an external filter (in this case sed), in order to strip out the commas and replace them with tabs, which gnuplot can handle more comfortably (after setting up some cosmetic-related features):

gnuplot>set grid
gnuplot>unset key
gnuplot>unset title
gnuplot> plot “< sed 's/,/\t/g' /cygdrive/c/Temp/table.csv" every ::2 using 1:2:4:3:5 with candlesticks

Note that I am telling gnuplot to ignore the header line in the CSV file (the every directive), and passing it five different pieces of information (numbered columns $1 through to $5) that it needs to generate the candlestick diagram.

Here’s the result:

Stock Price Diagram

Leave a Reply