3 # Though gnuplot is primarily a scientific plotting program, it can do a great
4 # job of plotting finance charts as well. The primary challenge is the irregular
5 # nature of financial time series. Stocks don't trade every day, so when you set
6 # the x-axis to time gaps appear for non-trading days. Investors and traders
7 # generally prefer that these gaps be omitted. Another challenge is that finance
8 # charts are best presented in semi-log form (log y-axis, linear x-axis),
9 # but gnuplot wants to span decades in its log scaling, something that stocks
10 # rarely do. These and other challenges are met in finance.dem, a short
11 # demonstration script that proves that gnuplot can really shine in this area.
13 # gnuplot plays a central role in our work. Almost all the graphs in "Bollinger
14 # on Bollinger Bands" were plotted by gnuplot, many gnuplot visuals have
15 # appeared on CNBC, our in-house analytics use gnuplot for visual display and
16 # The Capital Growth Letter relies heavily on gnuplot for its charts.
18 # Finally, gnuplot is yet another successful demonstration of a powerful idea,
19 # open source programming. Thanks to all who made gnuplot possible from the
20 # earliest days to the present and to all those who will contribute in the
21 # future. (Special thanks to Hans-Bernhard Broeker whose patience helped me to
22 # climb the grade and to Ethan Merritt whose recent contributions have been
23 # invaluable to our work.)
26 # www.BollingerBands.com
28 # a demonstration of gnuplot finance plot styles
29 # by John Bollinger, CFA, CMT
30 # www.BollingerBands.com
31 # BBands@BollingerBands.com
33 # data and indicators in finance.dat
35 # date, open, high, low, close, volume,
36 # 50-day moving average volume, Intraday Intensity,
37 # 20-day moving average close,
38 # upper Bollinger Band, lower Bollinger Band
40 # last update: 8 May 2005
42 pause -1 "Click OK to start the Finance Demo"
45 # set label 1 "Demo of plotting financial data" at screen 0.5, screen 0.95 center
46 set title "Demo of plotting financial data"
48 set ytics (105, 100, 95, 90, 85, 80)
52 plot 'finance.dat' using 0:5 notitle with lines
54 pause -1 "Turn on the grid"
57 set title "Turn on grid"
59 set ytics (105, 100, 95, 90, 85, 80)
64 plot 'finance.dat' using 0:5 notitle with lines
66 pause -1 "Semi-log scaling"
69 set title "Semi-log scaling"
71 set ytics (105, 100, 95, 90, 85, 80)
77 plot 'finance.dat' using 0:5 notitle with lines
79 pause -1 "Finance bars"
82 set title "Finance bars"
84 set ytics (105, 100, 95, 90, 85, 80)
90 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8
92 pause -1 "Bollinger Bands"
95 set title "Bollinger Bands"
97 set ytics (105, 100, 95, 90, 85, 80)
103 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
104 'finance.dat' using 0:9 notitle with lines lt 3, \
105 'finance.dat' using 0:10 notitle with lines lt 1, \
106 'finance.dat' using 0:11 notitle with lines lt 2
109 pause -1 "Overlay an indicator on a seperate scale"
112 set title "Overlay an indicator on a separate scale"
114 set ytics (105, 100, 95, 90, 85, 80)
120 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
121 'finance.dat' using 0:9 notitle with lines lt 3, \
122 'finance.dat' using 0:10 notitle with lines lt 1, \
123 'finance.dat' using 0:11 notitle with lines lt 2, \
124 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
126 pause -1 "Add some boiler plate"
129 set title "Add some boiler plate"
130 set label "Courtesy of Bollinger Capital" at graph 0.01, 0.07
131 set label " www.BollingerBands.com" at graph 0.01, 0.03
133 set ytics (105, 100, 95, 90, 85, 80)
139 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
140 'finance.dat' using 0:9 notitle with lines lt 3, \
141 'finance.dat' using 0:10 notitle with lines lt 1, \
142 'finance.dat' using 0:11 notitle with lines lt 2, \
143 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
145 pause -1 "Add volume in a seperate clip"
148 set label "Courtesy of Bollinger Capital" at graph 0.01, 0.07
149 set label " www.BollingerBands.com" at graph 0.01, 0.03
152 set ytics (105, 100, 95, 90, 85, 80)
158 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
163 set title "Add volume in a separate clip"
164 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
165 'finance.dat' using 0:9 notitle with lines lt 3, \
166 'finance.dat' using 0:10 notitle with lines lt 1, \
167 'finance.dat' using 0:11 notitle with lines lt 2, \
168 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
181 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3
184 pause -1 "Add average volume"
187 set label "Courtesy of Bollinger Capital" at graph 0.01, 0.07
188 set label " www.BollingerBands.com" at graph 0.01, 0.03
191 set ytics (105, 100, 95, 90, 85, 80)
197 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
199 set title "Add average volume"
203 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
204 'finance.dat' using 0:9 notitle with lines lt 3, \
205 'finance.dat' using 0:10 notitle with lines lt 1, \
206 'finance.dat' using 0:11 notitle with lines lt 2, \
207 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
220 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
221 'finance.dat' using 0:($7/10000) notitle with lines lt 1
224 pause -1 "Add date labels to the x axis"
227 set label "Courtesy of Bollinger Capital" at graph 0.01, 0.07
228 set label " www.BollingerBands.com" at graph 0.01, 0.03
231 set ytics (105, 100, 95, 90, 85, 80)
237 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
239 set title "Add date labels to the x axis"
243 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
244 'finance.dat' using 0:9 notitle with lines lt 3, \
245 'finance.dat' using 0:10 notitle with lines lt 1, \
246 'finance.dat' using 0:11 notitle with lines lt 2, \
247 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
260 set xtics ("6/03" 66, "7/03" 87, "8/03" 109, "9/03" 130, "10/03" 151, "11/03" 174, "12/03" 193, "1/04" 215, "2/04" 235)
261 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
262 'finance.dat' using 0:($7/10000) notitle with lines lt 1
265 pause -1 "Add labels for each clip"
268 set label "Courtesy of Bollinger Capital" at graph 0.01, 0.07
269 set label " www.BollingerBands.com" at graph 0.01, 0.03
272 set ytics (105, 100, 95, 90, 85, 80)
278 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
280 set title "Add labels for each clip"
284 set ylabel "price" offset 1
285 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
286 'finance.dat' using 0:9 notitle with lines lt 3, \
287 'finance.dat' using 0:10 notitle with lines lt 1, \
288 'finance.dat' using 0:11 notitle with lines lt 2, \
289 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
302 set xtics ("6/03" 66, "7/03" 87, "8/03" 109, "9/03" 130, "10/03" 151, "11/03" 174, "12/03" 193, "1/04" 215, "2/04" 235)
303 set ylabel "volume (0000)" offset 1
304 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
305 'finance.dat' using 0:($7/10000) notitle with lines lt 1
308 pause -1 "Add a title"
311 set label 1 "Acme Widgets" at graph 0.5, graph 0.9 center front
312 set label 2 "Courtesy of Bollinger Capital" at graph 0.01, 0.07
313 set label 3 " www.BollingerBands.com" at graph 0.01, 0.03
316 set ytics (105, 100, 95, 90, 85, 80)
322 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
324 set title "Add a title"
328 set ylabel "price" offset 1
329 plot 'finance.dat' using 0:2:3:4:5 notitle with financebars lt 8, \
330 'finance.dat' using 0:9 notitle with lines lt 3, \
331 'finance.dat' using 0:10 notitle with lines lt 1, \
332 'finance.dat' using 0:11 notitle with lines lt 2, \
333 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
347 set xtics ("6/03" 66, "7/03" 87, "8/03" 109, "9/03" 130, "10/03" 151, "11/03" 174, "12/03" 193, "1/04" 215, "2/04" 235)
348 set ylabel "volume (0000)" offset 1
349 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
350 'finance.dat' using 0:($7/10000) notitle with lines lt 1
353 pause -1 " Change to Candlesticks"
356 set label 1 "Acme Widgets" at graph 0.5, graph 0.9 center front
357 set label 2 "Courtesy of Bollinger Capital" at graph 0.01, 0.07
358 set label 3 " www.BollingerBands.com" at graph 0.01, 0.03
361 set ytics (105, 100, 95, 90, 85, 80)
367 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
369 set title "Change to candlesticks"
373 set ylabel "price" offset 1
374 plot 'finance.dat' using 0:2:3:4:5 notitle with candlesticks lt 8, \
375 'finance.dat' using 0:9 notitle with lines lt 3, \
376 'finance.dat' using 0:10 notitle with lines lt 1, \
377 'finance.dat' using 0:11 notitle with lines lt 2, \
378 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
392 set xtics ("6/03" 66, "7/03" 87, "8/03" 109, "9/03" 130, "10/03" 151, "11/03" 174, "12/03" 193, "1/04" 215, "2/04" 235)
393 set ylabel "volume (0000)" offset 1
394 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
395 'finance.dat' using 0:($7/10000) notitle with lines lt 1
398 pause -1 "Change to Bollinger Boxes"
401 set label 1 "Acme Widgets" at graph 0.5, graph 0.9 center front
402 set label 2 "Courtesy of Bollinger Capital" at graph 0.01, 0.07
403 set label 3 " www.BollingerBands.com" at graph 0.01, 0.03
406 set ytics (105, 100, 95, 90, 85, 80)
412 set xtics (66, 87, 109, 130, 151, 174, 193, 215, 235)
414 set title "Change to Bollinger Boxes"
418 set ylabel "price" offset 1
419 plot 'finance.dat' using 0:3:3:($2>$5?$2:$5):($2>$5?$2:$5) notitle with candlesticks lt 3, \
420 'finance.dat' using 0:($2<$5?$5:1/0):($2<$5?$5:1/0):($2<$5?$2:1/0):($2<$5?$2:1/0) notitle with candlesticks lt 2, \
421 'finance.dat' using 0:($2>$5?$2:1/0):($2>$5?$2:1/0):($2>$5?$5:1/0):($2>$5?$5:1/0) notitle with candlesticks lt 1, \
422 'finance.dat' using 0:($2<$5?$2:$5):($2<$5?$2:$5):4:4 notitle with candlesticks lt 3, \
423 'finance.dat' using 0:9 notitle with lines lt 3, \
424 'finance.dat' using 0:10 notitle with lines lt 1, \
425 'finance.dat' using 0:11 notitle with lines lt 2, \
426 'finance.dat' using 0:8 axes x1y2 notitle with lines lt 4
440 set xtics ("6/03" 66, "7/03" 87, "8/03" 109, "9/03" 130, "10/03" 151, "11/03" 174, "12/03" 193, "1/04" 215, "2/04" 235)
441 set ylabel "volume (0000)" offset 1
442 plot 'finance.dat' using 0:($6/10000) notitle with impulses lt 3, \
443 'finance.dat' using 0:($7/10000) notitle with lines lt 1