I was inspired by the Revolution Analytics blog post http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html on the d3.js style calendar heat map that Paul Bleicher from Humedica developed in R. In an effort to publicize such fine work, I wanted to put a slightly different spin on it to visualize a system’s time in the market. The system is ridiculously basic (not investment advice and will lose money), but the visualization is very helpful.
![]() |
From TimelyPortfolio |
To continue with the theme, I would like to continue to highlight some fine R work from http://systematicinvestor.wordpress.com. Although his work is far more sophisticated than this, I thought I would use his plota function to plot the German Dax (used in this example) with RSI below.
![]() |
From TimelyPortfolio |
Thanks so much to the amazing R coders who provided the code and functions for this post.
#quick example of d3.js style calendar heat map | |
#thanks so much Paul Bleicher, MD PhD who is Chief Medical Officer | |
# at Humedica, a next-generation clinical informatics company | |
# that provides novel business intelligence solutions | |
# to the health care and life science industries | |
#discussed in Revolution Analytics blog post | |
#http://blog.revolutionanalytics.com/2009/11/charting-time-series-as-calendar-heat-maps-in-r.html | |
con=url("http://blog.revolution-computing.com/downloads/calendarHeat.R") | |
source(con) | |
close(con) | |
require(quantmod) | |
#just as an extremely simple example | |
#enter when 20 day RSI > 50 and exit when 20 day RSI < 50 | |
#weight will be 0 or 1 | |
getSymbols("^GDAXI",from="1900-01-01",to=Sys.Date()) | |
#get 20 day RSI and lag one day | |
signal <- lag(RSI(GDAXI[,4],n=20),k=1) | |
weights <- ifelse(signal>50,1,0) | |
calendarHeat(as.Date(index(weights["2009::",])),coredata(weights["2009::"]),varname="RSI System Weight",ncolors=2) | |
####################################################### | |
#continue to highlight the very fine work of | |
#http://systematicinvestor.wordpress.com/ | |
#adapted some of his code to provide | |
#a not-so-novel additional example for | |
#those that might be interested | |
####################################################### | |
# Load Systematic Investor Toolbox (SIT) | |
con = gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb')) | |
source(con) | |
close(con) | |
#set up two regions for graphs | |
#candlestick price data on top 2 of 3 | |
#and rsi on the bottom 1 of 3 | |
layout(c(1,1,2)) | |
#plot candestick of the daily gdaxi data | |
plota(GDAXI, type = 'candle', plotX = F) | |
#add description of the data in top right and last value | |
plota.legend('German Dax', 'grey70', GDAXI) | |
#plot the rsi as line | |
plota(rsi<-RSI(GDAXI[,4],n=20), type = 'l') | |
#get some transparency for the highlighted regions | |
addalpha <- function(cols,alpha=180) { | |
rgbcomp <- col2rgb(cols) | |
rgbcomp[4] <- alpha | |
return(rgb(rgbcomp[1],rgbcomp[2],rgbcomp[3],rgbcomp[4],maxColorValue=255)) | |
} | |
#since our entry is RSI>50 we will highlight the region > 50 in green | |
plota.y.highlight(col=addalpha("green",80),highlight=c(50,100)) | |
#since our exit is RSI<50 we will highlight the region < 50 in red | |
plota.y.highlight(col=addalpha("red",80),highlight=c(0,50)) | |
#plot a line our at special 50 | |
abline(h = 50, col = 'gray20') | |
#give description of data in bottom (RSI) and color code to in (black) or out(red) | |
plota.legend('RSI(20)', text.col = ifelse(last(rsi)[]>50,'black','red'), fill = ifelse(last(rsi)[]>50,'black','red'), rsi) |