Gnuplot can create candlestick charts out of the box. These charts are often used for graphing financial data, with the middle block representing the difference between the open and close, and the high and low represented with lines protruding from the top and bottom of this box. The bars are colored differently depending on if the security’s price increased or decreased over this period of time. In gnuplot, losses are represented with filled bars.

Creating the graph is relatively easy. First you need a data file. This can be in a number of formats, but the following format is fairly common:

<date>,<open_price>,<high_price>,<low_price>,<close_price>

Once you have your data file, creating the graph is as simple as:

plot '<data_file>' using 1:2:4:3:5 with candlesticks

The 1:2:4:3:5 is the field order for date, open, low, high and close prices in the file. If your file has data in a different order, you will need to rearrange these numbers.

The following is a complete working example. We have two files; bhp.dat which contains the price data for BHP on the Australian Stock Exchange, and candlestick.gnuplot which contains the gnuplot instructions.

bhp.dat

# BHP (Australian Stock Exchange)
# Date,Open,High,Low,Close
2013-02-15,38.75,38.84,38.43,38.59
2013-02-18,38.76,38.94,38.59,38.67
2013-02-19,38.73,39.00,38.62,39.00
2013-02-20,39.00,39.34,38.43,38.65
2013-02-21,37.45,37.64,37.17,37.17
2013-02-22,36.73,37.18,36.51,36.87
2013-02-25,36.75,37.19,36.56,36.90
2013-02-26,36.27,36.69,36.12,36.35

candlestick.gnuplot

set terminal pngcairo font "arial,10" size 500,500
set output 'candlestick.png'
set xdata time
set timefmt"%Y-%m-%d"
set xrange ["2013-02-14":"2013-02-27"]
set yrange [*:*]
set datafile separator ","
plot 'bhp.dat' using 1:2:4:3:5 notitle with candlesticks

With these files, we can generate the graph on the command line with:

$ gnuplot candlestick.gnuplot

Which will generate candlestick.png which will look like this:

candlestick chart created with gnuplot

  1. Shivanshu Goswami says:

    Thanks, this was very useful.

  2. Hi there. This is pretty much only web page to explain how to plot candle chart with gnuplot. Thanks very much.

  3. Hector says:

    Hi,

    Great tutorial! I modified your example with set terminal dumb, because I need to view over ssh. I can view the chart but it is plotted with star (*) character, which makes it difficult to read. I know that ASCII won’t look pretty but… is there any workaround to make this graph readable over ssh? Perhaps by changing the star (*) character to a more “viewable” one? If the latter, how can I change the character used for plotting? Thank you in advance for any hint or orientation.