public

calendar heat of system entry

  • Download Gist
calendar heat of system entry.r
Rebol
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.