This is an updated version of an article I originally posted in German (change language with the little flags in the upper right). After posting a lot of charts and chart snippets lately, I thought I could share some ideas (and code :)) with whoever is interested in Gnuplot. When I made the initial design/architecture of my (future) trading system framework, I added a charting component to my wishlist. Not because I needed it for the actual trading, but for debugging the signal generation part (and of course enhance my signal posts on this blog with some visuals). Almost instantly Gnuplot came to my mind, a scientific plotting utility with scripting capabilities. The best part: it is free and open source software, free as in free speech AND free beer! :) And of course, to conclude the teaser with shameless name-dropping: picking up a code snipped John Bollinger posted more than 11 years ago! So I spend a couple of evenings with Gnuplot, googling and playing around...

To show off in the beginning, the current output (like you see in the signal/ weekly overview blogposts), click to enlarge:

DAX light2

Pretty ok for a couple of hours googling and c&p'ing, isn't it? :) In the end Gnuplot is no rocket science, the links that brought me closer step by step are attached below. If anyone anytime tries something similar, here come the biggest stumbling blocks. If you think "STOP babbling, gimme the code!"- just skip this text and move straigth to the end of this article.

The csv file I use for plotting is generated by my program and contains all price and signal data (all in one line as header):

date;open;high;low;close;tenkan;kijun;chikou;senkou_A;

senkou_B;signal1_name;signal1_descr;signal1_quote;

signal2_name;signal2_descr;signal2_quote;

signal3_name;signal3_descr;signal3_quote;

signal4_name;signal4_descr;signal4_quote;

signal5_name;signal5_descr;signal5_quote

Because Ichimoku has five kinds of different signals, in theory all 5 signals could occur simultaneously, so the datafile has placeholders for that (pretty rare) case.

Below that header follows the actual data, also separated by colons. One example is the following line (your resolution might affect the number of rows that you see, in the file it is one single line of data). As you can see, in this example there are two signals on this day: K-X (which stands for Kijun Cross) and KB (which stands for Kumo breakout... we definitely need a legend, but first things first):

2016-10-24;23431.60;23620.10;23235.60;23604.10;23521.20;23521.20;;23482.80;22334.20;K-X;SL;23521.199219;KB;SL;23604.099609;;;;;;;;

Now my first try was to plot all data using the first (date) column. In this case the x-axis is formatted with the dates as legend. BUT: the data file naturally does not contain data for weekends and holidays (nor a date for these days). Everytime Gnuplot stumbles upon such a "gap" in the data, it plots a gap in the chart. Looks like the x-axis is just equally filled if data values are missing. So I looked for a workaround for this issue. If you plot the index of rows instead of the date itself (if your data doesn't contain an index, use the fictional row '0'), the chart itself looks nice without gaps. But of course you lose the dates on the x-axis (instead it is labelled with 1,2,3..,n). Using a little trick mitigates that problem: In Gnuplot one can define a function, which looks in row 1 (the dates) and uses that as labels for column 0 (the fictional index). Additionally you can skip dates and plot only the nth dates (if for example you only want every 5th date as label):

everyNth(countColumn, labelColumnNum, N) = \
  ( (int(column(countColumn)) % N == 0) ? stringcolumn(labelColumnNum) : "" )

This function can be used for the actual plot command, here an example for the candlesticks (yes, gnuplot has build- in candlestick capabilies!):

"DAX_plotdata.csv" using 0:2:3:4:5:($5 < $2 ? -1 : 1):xticlabels(everyNth(0, 1, 5)) notitle with candles palette

the comparison  $5 < $2 ? -1 : 1

creates the different colors for each candle, depending if column 5 (close) is less than column 2 (open)- in plain english: if you have a rising or falling daily candle. The colors can be chosen freely, for example:

set palette defined (-1 '#CD5C5C', 1 '#32CD32')

To mark the signals (column 13), I chose to draw transparent blueish bubbles and label (column 11) them with some abbreviation (like K-X and K-B):

plot "DAX_plotdata.csv" using 0:13 with circles lt 3 fs transparent solid 0.3 noborder notitle,
"DAX_plotdata.csv" using 0:13:11 with labels font "Verdana, 8" textcolor "black" nopoint offset char 0,-1.5 notitle,\
 

Plotting the Kumo cloud was far easier than I thought: The filledcurves Gnuplot provides fit perfectly. bereit. Columns 9 and 10 in the csv contain Senkou Span A and B and can be plotted as follows:

plot "DAX_plotdata.csv" using 0:9:10:xticlabels(everyNth(0, 1, 5)) with filledcurves above fill pattern 7 lt 2 title 'rising Kumo', \
    "DAX_plotdata.csv" using 0:9:10:xticlabels(everyNth(0, 1, 5)) with filledcurves below fill pattern 6 lt 4 title 'falling Kumo'

Above command fills the area between both lines with different colors, depending whether the kumo is rising (above) or falling (below). Btw. I had to experiment a bit in which order the lines are drawn: I ended up with plotting the Kumo first, and then the other indicators and finally the candles on top of it.

After plotting all indicators and candles, Gnuplot knows about the maximum used prices and automacically choses how to display the Y- values. The lower and upper limits can be queried using

MIN=GPVAL_Y_MIN
MAX=GPVAL_Y_MAX

The final goody of my plotting script is the fine dotted line, which connects each signal with the upper limit of the screen and displays additional information about the signal, a code for the strength:

replot "DAX_plotdata.csv" using 0:($13):(0):(MAX-$13) with vectors  lt 8 dashtype 2  nohead notitle,\
    "DAX_plotdata.csv" using 0:(MAX):12 with labels font "Verdana, 8" textcolor "black" offset char -1,char 0.8 rotate by 90 notitle

The rest of the script is pretty straigthforward and should be self-explaining. Remember: the actual script file and the csv datafile is generated by my trading software (based on the settings like colors and actual symbol the scripts are somewhat varying). I do not want to (and now don't have to) fiddle around with those scripts manually anymore. The plotting itself is done externally by calling Gnuplot, including the export to an image file.

If you have any questions, need a sample data file or whatever else reason, feel free to contact me (comment this article or drop a mail: This email address is being protected from spambots. You need JavaScript enabled to view it.).

Plans for the future: of course Gnuplot will be handy for the postprocessing of the trading system like visualizing backtest results, depot status, statistics or whatever. Enough ideas for two lifetimes ;)

To conclude this article, a different colorset for the chart script (click to enlarge):

 DAX contrast

 

In the end some ressources I found useful when searching for information how to achieve what I described in this post (btw. link nr. 2 by John Bollinger himself):

www.gnuplot.info

http://gnuplot.sourceforge.net/demo/finance.html

http://stackoverflow.com/questions/36030346/remove-weekend-gaps-in-gnuplot-for-candlestick-chart

https://groups.google.com/forum/#!msg/comp.graphics.apps.gnuplot/M_ldKLSFXwg/FFeOPZ4MCccJ

http://stackoverflow.com/questions/24092212/gnuplot-candlesticks-and-boxwidth

http://www.gnuplot.info/demo/fillbetween.html

 

And finally! the complete script itself, enjoy!

 

# (c) 2016 www.spare-time-trading.de
# feel free to modify this file to your needs
# no warranty if gnuplot eats your cat (or any other circumstances)

set title "DAX daily" textcolor "black" offset char -60,0
set label "  www.spare-time-trading.de" at graph 0.021, 0.19 center rotate by 90 textcolor "black" front

set label "SL: Strong Long Signal" at graph 0.87, 0.84 textcolor "black" front
set label "SS: Strong Short Signal" at graph 0.87, 0.825 textcolor "black" front
set label "K-X: Kijun Cross" at graph 0.87, 0.81 textcolor "black" front
set label "TK-X: Tenkan/ Kijun Cross" at graph 0.87,0.795 textcolor "black"
set label "C-X: Chikou Cross" at graph 0.87, 0.78 textcolor "black" front
set label "S-X: Senkou Cross" at graph 0.87, 0.765 textcolor "black" front
set label "KB: Kumo Breakout" at graph 0.87, 0.75 textcolor "black" front

set terminal pngcairo size 1600,1200 nocrop enhanced font "arial,12"
set output "DAX_light.png"

#set terminal wxt size 800,600 enhanced

set border linecolor rgbcolor "black"
set key textcolor rgbcolor "black"

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

set palette defined (-1 '#CD5C5C', 1 '#32CD32')
set grid
set lmargin 9
set rmargin 2

set style fill solid 0.25 border

set cbrange [-1:1]

unset colorbox

everyNth(countColumn, labelColumnNum, N) = \
   ( (int(column(countColumn)) % N == 0) ? stringcolumn(labelColumnNum) : "" )

set xtics rotate 90

set datafile separator ";"
set key autotitle columnhead

set multiplot
plot "DAX_plotdata.csv" using 0:9:10:xticlabels(everyNth(0, 1, 5)) with filledcurves above fill pattern 7 lt 2 title 'rising Kumo', \
    "DAX_plotdata.csv" using 0:9:10:xticlabels(everyNth(0, 1, 5)) with filledcurves below fill pattern 6 lt 4 title 'falling Kumo', \
    "DAX_plotdata.csv" using 0:13 with circles lt 3 fs transparent solid 0.3 noborder notitle,\
    "DAX_plotdata.csv" using 0:13:11 with labels font "Verdana, 8" textcolor "black" nopoint offset char 0,-1.5 notitle,\
    "DAX_plotdata.csv" using 0:6 with lines lt 7 lw 2,\
    "DAX_plotdata.csv" using 0:7 with lines lt 3 lw 2,\
    "DAX_plotdata.csv" using 0:8 with lines lt 2 lw 1,\
    "DAX_plotdata.csv" using 0:2:3:4:5:($5 < $2 ? -1 : 1):xticlabels(everyNth(0, 1, 5)) notitle with candles palette
MIN=GPVAL_Y_MIN
MAX=GPVAL_Y_MAX
replot "DAX_plotdata.csv" using 0:($13):(0):(MAX-$13) with vectors  lt 8 dashtype 2  nohead notitle,\
    "DAX_plotdata.csv" using 0:(MAX):12 with labels font "Verdana, 8" textcolor "black" offset char -1,char 0.8 rotate by 90 notitle
unset multiplot

Comments powered by CComment