7

Candlestick plots on the financial sites, at least the ones I have seen, fill the candlestick green if the close is higher then the open, and red if the close is lower then the open.

If I set the fill style all candlesticks are filled with the fill color, is there a way in gnuplot 4.6 to use the color scheme as I described above?

A sample script is:

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set xrange ["2014-01-01":"2014-01-02"]
set yrange [*:*]
set datafile separator ","
plot '201401_EURUSD_Hourly.csv' using 1:2:4:3:5 notitle with candlesticks

with some data points

2014-01-01 17:00:00,1.376150,1.376550,1.374020,1.375990
2014-01-01 18:00:00,1.376100,1.377340,1.375980,1.376520 
2014-01-01 19:00:00,1.376440,1.376870,1.375780,1.375860 
2014-01-01 20:00:00,1.375850,1.376470,1.375000,1.376280 
2014-01-01 21:00:00,1.376270,1.376720,1.375970,1.376530 
2014-01-01 22:00:00,1.376550,1.377440,1.376270,1.376530 
2014-01-01 23:00:00,1.376540,1.376540,1.374390,1.374520 
2014-01-02 00:00:00,1.374500,1.375790,1.374380,1.375660 
2014-01-02 01:00:00,1.375630,1.375740,1.374610,1.375000 
2014-01-02 02:00:00,1.374980,1.375270,1.372480,1.373100
Bodger
  • 1,342
  • 4
  • 16
  • 23
  • 1
    Yes, you can use `linecolor variable` or similar. Please include a minimal, working example of what you have now, including a few data points. – Christoph Apr 24 '14 at 13:55
  • `set xdata time set timefmt"%Y-%m-%d %H:%M:%S" set xrange ["2014-04-01":"2014-04-11"] set yrange [*:*] set datafile separator "," plot '201404_EURUSD_Hourly.csv' using 1:2:4:3:5 notitle with candlesticks` – Bodger Apr 24 '14 at 14:50
  • I cannot seem to get the code lined up correctly, the help is useless in this case. Let me get some data points – Bodger Apr 24 '14 at 14:56
  • `code`2014-01-01 17:00:00,1.376150,1.376550,1.374020,1.375990 2014-01-01 18:00:00,1.376100,1.377340,1.375980,1.376520 2014-01-01 19:00:00,1.376440,1.376870,1.375780,1.375860 2014-01-01 20:00:00,1.375850,1.376470,1.375000,1.376280 2014-01-01 21:00:00,1.376270,1.376720,1.375970,1.376530 2014-01-01 22:00:00,1.376550,1.377440,1.376270,1.376530 2014-01-01 23:00:00,1.376540,1.376540,1.374390,1.374520 2014-01-02 00:00:00,1.374500,1.375790,1.374380,1.375660 2014-01-02 01:00:00,1.375630,1.375740,1.374610,1.375000 2014-01-02 02:00:00,1.374980,1.375270,1.372480,1.373100 `code` – Bodger Apr 24 '14 at 14:57
  • Ok, I already added the script to the question. You can format the code properly only inside the question (adding four spaces before each line, or marking and hitting Ctrl+K). – Christoph Apr 24 '14 at 14:57
  • thank you, i looked at linecolor variable, I need to precalculate the color and add it as a column in the data, is that correct? – Bodger Apr 24 '14 at 15:00
  • I further editted, I was showing one plot from a different data file I was showing you. – Bodger Apr 24 '14 at 15:01
  • Just a second, I'm preparing an answer :) – Christoph Apr 24 '14 at 15:04

1 Answers1

8

Here is an example which uses a predefined color palette to select the colors (keyword palette):

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set datafile separator ","

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

set style fill solid noborder

plot '201404_EURUSD_Hourly.csv' using 1:2:4:3:5:($5 < $2 ? -1 : 1)  notitle with candlesticks palette

That uses an additional column (the last one) to select between values -1 (red, close lower than open) and +1 (green, close higher than open).

The output with 4.6.4 is:

enter image description here

The second option is to use linecolor rgb variable, in which case the last column must be in integer representation of an rgb-tuple:

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set datafile separator ","

set style fill solid noborder

# Place colored points in 3D at the x,y,z coordinates corresponding to
# their red, green, and blue components
rgb(r,g,b) = 65536 * int(r) + 256 * int(g) + int(b)

plot '201404_EURUSD_Hourly.csv' using 1:2:4:3:5:($5 < $2 ? rgb(255,0,0) : rgb(0,255,0))  linecolor rgb variable notitle with candlesticks

And, last but not least, use linecolor variable to select between two linetypes (the last column is the linetype index):

set xdata time
set timefmt"%Y-%m-%d %H:%M:%S"
set datafile separator ","

set style fill solid noborder

set linetype 1 lc rgb 'red'
set linetype 2 lc rgb 'green'

plot '201404_EURUSD_Hourly.csv' using 1:2:4:3:5:($5 < $2 ? 1 : 2)  linecolor variable notitle with candlesticks
Christoph
  • 47,569
  • 8
  • 87
  • 187
  • That worked, I am going to close this and hope it credits you with it, if not comment again. – Bodger Apr 24 '14 at 15:33