Introduction to PloTA library in the Systematic Investor Toolbox
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
PloTA ( plot + ta ) library in the Systematic Investor Toolbox is a simple plot interface to charting Time Series and Technical Analysis plots. I created it as an alternative to charting functionality in quantmod package. It is designed to mimic default plot interface and works with xts objects. PloTA implements following methods:
- plota – main plot method
- plota2Y – add second Y axis to existing plot
- plota.lines – plot lines
- plota.candle – plot Candle
- plota.ohlc – plot Open/High/Low/Close
- plota.hl – plot High/Low
- plota.volume – plot Volume
- plota.scale.volume – scale Volume
- plota.grid – add grid
- plota.legend – plot legend
- plota.layout – specify plot layout
- plota.theme.blue.red – color theme
- plota.theme.green.orange – color theme
- plota.theme.gray.orange – color theme
To start using PloTA, let’s first load Systematic Investor Toolbox:
# load Systematic Investor Toolbox setInternet2(TRUE) source(gzcon(url('https://github.com/systematicinvestor/SIT/raw/master/sit.gz', 'rb')))
Next let’s load quantmod package and download price history for SPY and IBM:
load.packages('quantmod') # download sample data from Yahoo data.spy = getSymbols('SPY', from = '1980-01-01', auto.assign = FALSE) data.ibm = getSymbols('IBM', from = '1980-01-01', auto.assign = FALSE)
Next I will show a few simple uses of PloTA library.
To create a simple chart of SPY:
y = data.spy['2011:01:01::2011:02:01'] highlight = which(Cl(y) < 127) png(filename = 'plot1.png', width = 500, height = 500, units = 'px', pointsize = 12, bg = 'white') layout(c(1,1,2)) plota(y, type = 'candle', main = 'SPY', plotX = F, x.highlight = highlight) y = plota.scale.volume(y) plota(y, type = 'volume', x.highlight = highlight) dev.off()
To create a simple chart of SPY with RSI and Legend:
y = data.spy['2010:01:01::2011:02:01'] layout(c(1,1,2,3)) plota(y, type = 'candle', plotX = F) plota.legend('SPY', 'blue', y) y = plota.scale.volume(y) plota(y, type = 'volume', plotX = F) plota.legend('Volume', 'blue', Vo(y)) rsi = RSI(Cl(y),2) plota(rsi, type = 'l', y.highlight = c(c(Inf,80),c(20,-Inf))) abline(h = 20, col = 'red') abline(h = 80, col = 'red') plota.legend('RSI(2)', 'black', rsi)
To create a chart with second Y axis:
y = data.spy['2010:01:01::2011:02:01'] # to plot second Y axis, free some space on left side # e.g. set LeftMargin=3 plota(y, type = 'ohlc', LeftMargin=3) y0 = y; y = data.ibm['2010:10:15::2011:02:01'] plota2Y(y, ylim = range(OHLC(y)),las=1, col='red', col.axis = 'red') plota.ohlc(y, col = 'red') plota.legend('SPY(rhs),IBM(lhs)', 'blue,red', list(y0,y))
To plot daily and monthly on the same plot:
y = data.spy['2010:01:01::2011:02:01'] plota(y, type = 'candle') y1 = to.monthly(y) index(y1) = as.Date(index(y1)) plota.ohlc(y1, col = 'pink') plota.candle(y) plota.legend('Daily,Monthly', 'red,pink')
To plot daily, weekly and monthly:
y = data.spy['2010:01:01::2011'] layout(c(1,2,3)) plota(y, type = 'candle', plotX = F) plota.legend('Daily', 'blue', y) plota(y, ylim = range(OHLC(y)), plotX = F) y1 = to.weekly(y) index(y1) = as.Date(index(y1)) plota.candle(y1) plota.legend('Weekly', 'blue', y1) plota(y, ylim = range(OHLC(y))) y1 = to.monthly(y) index(y1) = as.Date(index(y1)) plota.candle(y1) plota.legend('Monthly', 'blue', y1)
I will show more examples of PloTA in the future posts.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.