Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Created April 9, 2012 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timelyportfolio/2346527 to your computer and use it in GitHub Desktop.
Save timelyportfolio/2346527 to your computer and use it in GitHub Desktop.
calendar heat of system entry
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment