How to run multiple chartSeries efficiently

I'm receiving the below error:
"Error in try.xts(x, error = "chartSeries requires an xtsible object") :
chartSeries requires an xtsible object"
The issue seems to be with chartSeries(as.name(x), if I replace this with MCR.AX, it works as expected. I'm trying to avoid creating repetitive code here.

library(tidyquant)
library(purrr)

# Gather ticker symbols
nickel_stocks <-  c("MCR.AX", "CZI.AX", "NIC.AX", "PAN.AX", "POS.AX", "S32.AX",
                    "SGQ.AX", "WSA.AX")

# Gather xts objects
getSymbols(nickel_stocks)

fun_gen_plts = function(x) {
  print(x)
  print(as.name(x))
  chartSeries(as.name(x),
              TA='addVo();
              addOBV();
              addRSI()',
              subset='2019',
              theme="white")
  
}

# Leverage purrr map function to call and pass each stock
# to the chartSEries function
map(nickel_stocks, ~ fun_gen_plts(.x))

Hi @chalg. You can change the fun_gen_plts as follow.

fun_gen_plts = function(x) {
 print(x)
 print(as.name(x))
 chartSeries(get(x),
             TA='addVo();
             addOBV();
             addRSI()',
             subset='2019',
             theme="white")
 
}

Thanks. This works to some extent, however a huge amount of 'slot' data is printed to the console and once all plots are printed, it appears to go into a loop adding more to a plot until I receive the error: "Error in plot.new() : figure margins too large".
Is there a purrr function that plays better with chartSeries, as I have no issue with ggplot calls? Or perhaps I should be using the return statement to return something specific?

@chalg. The error is due to the plot too large to display. You can export the image to view or view it with full screen mode and minimise all panel and maximise the plot panel in rstuido. Because I don't know what plot you want to view. But after I test with the chartSeries, the chartSeries or chobTA class have a behaviour that if you plot the chobTA class in series, it will stack in the same plot. If run with map equal to call the list of resulting chobTA classes will stack all plots and make the plot very large. So if you want individual plots, you have to use for loop as follow. Hope the above can help.

for(i in nickel_stocks) {
  fun_gen_plts(i)
}

Thanks. Yes this works, I added name = x to the function also so I don't get "get x" as the name for every plot. End result:

library(tidyquant)

# Gather ticker symbols
nickel_stocks <-  c("MCR.AX", "CZI.AX", "NIC.AX", "PAN.AX", "POS.AX", "S32.AX",
                    "SGQ.AX", "WSA.AX")

# Gather xts objects
getSymbols(nickel_stocks)

fun_gen_plts = function(x) {
  
  chartSeries(get(x),
              name = x,
              TA='addVo();
             addOBV();
             addRSI()',
              subset='2019',
              theme="white")
  
}

# Use for loop to create each plot.
for(i in nickel_stocks) {
  fun_gen_plts(i)
  
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.