Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R/quantmod: how to specify the bollinger bands colour?

Tags:

r

quantmod

This could be more generally be How to change the theme colours? Or maybe TA colours are not controlled by theme?

This makes bollinger bands with a nice cloud effect:

chartSeries(bars, theme="white")
addBBands()

(See example of how it looks (near the bottom) )

The cloud effect is dark grey on this next example, so almost invisible.

chartSeries(bars, theme="black")
addBBands()

How do I change it to be, say, a nice bright red, with bright purple for the upper and lower lines? (Yeah, I know, -1 for the colour scheme)

I believe I'll be able to specify an 8-hex-digit colour to specify semi-transparency. But can I do anything more exotic? E.g. it would be rather cool to use a gradient and have it #ff0000 at the centre, fading to #330000 at the upper and lower lines. Is there any gradient support in quantmod charting?

like image 728
Darren Cook Avatar asked Sep 04 '25 03:09

Darren Cook


2 Answers

A look at chartTheme seems to indicate that a gradient is not possible, but the up/down colours can be specified, as can the respective border colours. Just define your own theme as per the examples. You can start with the predetermined theme and modify certain individual parameters.

like image 110
Benjamin Avatar answered Sep 06 '25 12:09

Benjamin


Fleshing out Benjamin's answer and my own learnings, here is an example:

#bars is an XTS object, OHLC data
library(quantmod)

chartSeries(bars)
addBBands(n=20,sd=2)
addBBands(n=50,sd=1)

The above draws two bollinger bands, in default colour scheme. The following will change them to be a semi-transparent red (i.e. the red is stronger where they both exist):

t=chartTheme()
t$BBands$fill="#ff666633"   #20% red (i.e. hex 33 is the transparency)
reChart(theme=t)

From my study of the source this should have worked to change the line colours:

t$BBands$col=c('red','blue','green')

But it does not. However you can change the top/bottom colours to the same colour with:

t$BBands$col='blue'
reChart(theme=t)

And here is how to do the same with the newer chart_series() function, and notice you can set the line colours individually (NB. there is no reChart function, as far as I can see):

t=chart_theme()
t$bbands$col$fill="#ff000033"
t$bbands$col$upper='red'
t$bbands$col$lower='green'
t$bbands$col$ma='blue'
chart_Series(bars,theme=t)
add_BBands(n=50,sd=1)
add_BBands(n=20,sd=2)

It is not possible, as far as I know, to use a different colour scheme for each of the two bollinger bands. Even changing the colour scheme like this fails, as after the second command it redraws both with the new colours!

obj=chart_Series(bars)
add_BBands(n=50,sd=1)
obj$Env$theme$bbands$col$fill="#00ff0033"
add_BBands(n=20,sd=2)
like image 28
Darren Cook Avatar answered Sep 06 '25 11:09

Darren Cook



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!