Add polygon function for shading

Issue #5 closed
Former user created an issue

This can be a nice effect, e.g., between Bollenger bands.

rtsplot.polygon <- function (y, col = graphics::par("col"), ...) { y = y[, 1:2] temp.x = rtsplot:::rtsplot.map.xaxis(y)

n <- NROW(y) xx <- .index(y)[c(1, 1:n, n:1)] yu <- as.vector(coredata(y[, 1])) yl <- as.vector(coredata(y[, 2])) graphics::polygon(x = xx, y = c(yl[1], yu, rev(yl)), border = NA, col = col, ...) }

Comments (3)

  1. rtsvizteam repo owner

    Thank you, great idea. Done, please see commit cdfb093

    Please install development version

    remotes::install_bitbucket("rtsvizteam/rtsplot")
    

    Please use the following code to test new functionality

    library(rtsplot)
    
    y = rtsplot.fake.stock.data(1000, ohlc=TRUE) 
    symbol = 'SPY'
    
    # moving average
    bbands = TTR::BBands(quantmod::HLC(y), n=200, sd=1)[,c('up','dn')]  
    
    # plot
    layout(1)
    rtsplot(y, type = 'l', col='black')
    col = grDevices::adjustcolor('green', 50/255)
    rtsplot.polygon(bbands, col = col )
    rtsplot.legend(c(symbol, 'BBands'), c('black', col), list(y,bbands))
    

  2. Log in to comment