Skip to content

Instantly share code, notes, and snippets.

@helgasoft
Last active September 26, 2019 19:18
Show Gist options
  • Save helgasoft/a766acbd3c0af07b9faf6e62a94f9246 to your computer and use it in GitHub Desktop.
Save helgasoft/a766acbd3c0af07b9faf6e62a94f9246 to your computer and use it in GitHub Desktop.
add Volume inside quantmod candlechart
# https://gist.github.com/helgasoft/a766acbd3c0af07b9faf6e62a94f9246
library(shiny)
library(quantmod)
ui <- fluidPage(
titlePanel("Volume inside chart_Series"),
mainPanel(
fluidRow(
column( 4, textInput('tick', label='symbol', value='IBM', width='60px')),
column( 4, checkboxInput('useValue', 'from returned value', value=FALSE))
),
plotOutput('testPlot', height='600px', width='900px')
)
)
server <- function(input, output) {
volumeInChart <- function(tick=NULL, h=0.25, p=F) {
# add Volume inside candlechart
# tick: OHCLV xts object
# h: height of volume as fraction of chart height
# p: plot it or get data only
library(quantmod)
stopifnot(!is.null(tick), is.xts(tick), is.OHLCV(tick)) # input validation
voDmin <- min(Hi(tick), Lo(tick))
voGmax <- (max(Hi(tick)) - voDmin) * h
voDmax <- max(Vo(tick))
voG <- Vo(tick) / voDmax * voGmax + voDmin
names(voG) <- 'vchart'
if (!p) { return(voG) } # data only
vta <- paste0("add_TA(voG$vchart,on=-1,type='h',col='black',lwd=3);")
plot(chart_Series(tick, name=unlist(strsplit(colnames(tick)[1], '.', T))[1], TA=vta))
return('plot done')
}
output$testPlot <- renderPlot({
x <- getSymbols(input$tick, from="2019-05-01", auto.assign=F);
if (input$useValue) {
vo <- volumeInChart(x, p=F) # data only
vta <-"add_TA(abs(vo$vchart),on=-1,type='h',col='blue',lwd=3)"
t <- chart_theme(); t$col$grid<-NA;
chart_Series(x, theme=t, TA=vta)
} else {
volumeInChart(x, p=T)
}
})
} #server
# Run the application
shinyApp(ui = ui, server = server)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment