gnuplotfinancialcandlestick-chart

Gnuplot Candlesticks and boxwidth


Been working with candlesticks on real financial data. It works great unless I have gaps in the data, which there are plenty with historical financial data.

I have had "set boxwidth 1 relative" and it works ok, it gives me a proper "width" of the candlestick in most cases. But if there is no data between 2 points the candlestick will become fatter, i.e. it extends right to fill that gap. The visual is horrible, the extension to the right is really bad.

I have tried playing with set boxwidth x absolute, but I cannot come to terms in how it is displaying. I have narrowed it down to set boxwidth 37500 absolute and have no idea why that number works, even though it has even worse problems then the set boxwidth 1 relative.

The first image is what it looks like using set boxwidth 37500 absolute. There the dates goes from 01/31/13, 02/01/13, 02/03/13, 02/04/13, 02/05/13. There is no 02/02/13:

enter image description here

The absolute shows a proper gap between 02/01 and 02/02 but 02/03 and 02/04 overlap for reasons I cannot explain.

The second image uses set boxwidth 1 relative. This has it mostly the way I want it. The candlesticks are adjacent and mostly right. But the gap on 02/02/13 it fattens to the right. There is also a gap on 02/09/13 where it also fattens, or possibly the 2 on either side extend to fill the gap I do not know.

enter image description here

How can I configure this so that all the candlesticks are the same width adjacent and gaps in the data are empty?

I googled this like crazy and no one talks about it. The few examples of candlesticks that I have found do not use "dates" but integers, totally worthless. Candlestick charts require dates per the manual.

Running Gnuplot 4.6 patchlevel 0 on Windows 7.

Thank you

PS: I should have added data here goes.

basic.csv:

2013-01-15 00:00:00,93.879000,93.949000,92.874000,93.078000
2013-01-16 00:00:00,93.079000,93.672000,92.458000,92.800000
2013-01-17 00:00:00,92.799000,95.011000,92.629000,94.616000
2013-01-18 00:00:00,94.617000,94.872000,94.157000,94.662000
2013-01-20 17:00:00,94.649000,94.820000,93.965000,94.155000
2013-01-21 00:00:00,94.159000,94.938000,93.726000,94.009000
2013-01-22 00:00:00,94.011000,94.284000,93.147000,93.231000
2013-01-23 00:00:00,93.229000,94.024000,92.793000,93.649000
2013-01-24 00:00:00,93.650000,94.715000,93.559000,94.489000
2013-01-25 00:00:00,94.490000,95.083000,94.472000,94.749000
2013-01-27 17:00:00,94.819000,95.007000,94.652000,94.834000
2013-01-28 00:00:00,94.835000,94.968000,94.082000,94.809000
2013-01-29 00:00:00,94.803000,95.330000,94.370000,95.248000
2013-01-30 00:00:00,95.245000,95.450000,94.255000,94.365000
2013-01-31 00:00:00,94.372000,95.799000,94.328000,95.714000
2013-02-01 00:00:00,95.715000,96.718000,95.457000,96.597000
2013-02-03 17:00:00,96.716000,96.777000,96.370000,96.572000
2013-02-04 00:00:00,96.574000,97.064000,95.968000,96.044000
2013-02-05 00:00:00,96.043000,97.426000,95.945000,97.131000
2013-02-06 00:00:00,97.133000,97.284000,96.092000,96.395000
2013-02-07 00:00:00,96.396000,97.023000,95.813000,96.145000
2013-02-08 00:00:00,96.146000,96.182000,95.124000,95.625000
2013-02-10 17:00:00,95.623000,95.744000,95.210000,95.339000
2013-02-11 00:00:00,95.336000,96.877000,95.168000,96.537000
2013-02-12 00:00:00,96.536000,96.719000,95.776000,96.214000
2013-02-13 00:00:00,96.216000,96.890000,96.114000,96.775000
2013-02-14 00:00:00,96.771000,96.964000,95.609000,95.621000
2013-02-15 00:00:00,95.622000,96.676000,95.521000,96.351000

absolute.plt:

reset

set border linecolor rgbcolor "yellow"
set key textcolor rgbcolor "white"

set obj 1 rectangle behind from screen 0,0 to screen 1,1
set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "black"

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set xrange ["2013-01-15 00:00:00":"2013-02-15 23:59:59"]

set yrange [*:*]
set datafile separator ","

set palette defined (-1 'red', 1 'green')
set cbrange [-1:1]
unset colorbox

set style fill solid noborder
set boxwidth 37500 absolute

set title "AUDJPY" textcolor rgbcolor "white"
plot 'basic.csv' using 1:2:4:3:5:($5 < $2 ? -1 : 1) with candlesticks palette

relative.plt:

reset

set border linecolor rgbcolor "yellow"
set key textcolor rgbcolor "white"

set obj 1 rectangle behind from screen 0,0 to screen 1,1
set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "black"

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set xrange ["2013-01-15 00:00:00":"2013-02-15 23:59:59"]

set yrange [*:*]
set datafile separator ","

set palette defined (-1 'red', 1 'green')
set cbrange [-1:1]
unset colorbox

set style fill solid noborder
set boxwidth 1 relative

set title "AUDJPY" textcolor rgbcolor "white"
plot 'basic.csv' using 1:2:4:3:5:($5 < $2 ? -1 : 1) with candlesticks palette

Solution

  • When using set boxwidth absolute, the width is given in units of the x-axis, which in the case of dates is seconds. So, a width of 37500 is 10 hours.

    You could also use an explicit width in the 6th column, and switch to -2 to get an automatic box width for certain columns. That however would require you to manipulate your data file by hand.

    Another point: Is it essential for you to include the hours in some data points? This is what narrows the distance of some adjacent points. You could ignore the hours, which would give you a point distance of minimum one day. To strip the hours, use strptime in the using statement:

    reset
    
    set border linecolor rgbcolor "yellow"
    set key textcolor rgbcolor "white"
    
    set obj 1 rectangle behind from screen 0,0 to screen 1,1
    set obj 1 fillstyle solid 1.0 fillcolor rgbcolor "black"
    
    set xdata time
    set timefmt"%Y-%m-%d %H:%M:%S"
    set xrange ["2013-01-15 00:00:00":"2013-02-15 23:59:59"]
    
    set yrange [*:*]
    set datafile separator ","
    
    set palette defined (-1 'red', 1 'green')
    set cbrange [-1:1]
    unset colorbox
    
    set style fill solid noborder
    set boxwidth 60000 absolute
    
    set title "AUDJPY" textcolor rgbcolor "white"
    plot 'basic.csv' using (strptime('%Y-%m-%d', strcol(1))):2:4:3:5:($5 < $2 ? -1 : 1) with candlesticks palette
    

    Result with 4.6.0:

    enter image description here