Skip to content
Prev 3814 / 15274 Next

Quantmod - chartSeries

Hi Jeff,

that is nice and clean solution. Found a quick fix as well. By temporarily assigning all objects I needed in the chartSeries call to .GlobalEnv everything works. A bit messy since you need to clean up .GlobalEnv. 

thanks

Andreas  

-----Original Message-----
From: Jeff Ryan [mailto:jeff.a.ryan at gmail.com] 
Sent: 20 March 2009 17:56
To: Andreas Johansson
Cc: r-sig-finance at stat.math.ethz.ch
Subject: Re: [R-SIG-Finance] Quantmod - chartSeries

Hi Andreas, <cc'ing back the list>

Thanks for the minimal code.  The lookup is tough.  Environments when
embedded in this whole process are very tough to handle correctly.

One possible way to fix this is by using *newTA* to create a function
call that is just like the base functions in quantmod.

Another (easier) way is to break it apart in the loop.
function(ID){
 for (i in 1:length(ID)){
   Data=getSymbols(ID[i],auto.assign=F)
   print(class(Data))
   print(dimnames(Data))
   chartSeries(Data,TA=NULL)
   plot(addTA(Cl(Data)<3,border=NA,col='#888888',on=-1))  #need plot()
   # dev.copy2pdf(file=ID[i])
 }
}

A second (cooler!) approach is with newTA:

# define the logic
myfun <- function(x) { Cl(x) < 3 }
# create a 'new' TA function
myTA <- newTA(myfun,border=NA,col="#888888",on=-1)

LoopChart <-
function(ID){
 for (i in 1:length(ID)){
   Data=getSymbols(ID[i],auto.assign=F)
   print(class(Data))
   print(dimnames(Data))
   chartSeries(Data,TA='myTA()')
   # dev.copy2pdf(file=ID[i])
 }
}

LoopChart("C")

HTH,
Jeff

******* ORIGINAL MSG ********
Thanks for your quick response,

Will try to rework as you suggested. Appreciate your comment reg
runable code in my example. Cannot fully replicate my function call
because I am using a version of getSymbols that we have linked to our
internal database. Created a similar example that should catch the
same error.

Did a bit more digging and it looks like chartSeries looks in the
global environment and not the frame of the function call when it
searches for the "Data" object. Everything works if I put a object
called Data in the global environment before i.e. run
Data=getSymbols(ID[i],auto.assign=F) in the console before making the
LoopChart("C") function call. Function is below with function call and
traceback. Thanks again /Andreas

LoopChart("C")

LoopChart<-function(ID){
 for (i in 1:length(ID)){
   Data=getSymbols(ID[i],auto.assign=F)
   print(class(Data))
   print(dimnames(Data))
   chartSeries(Data,TA="addTA(Cl(Data)<3,border=NA,col='#888888',on=-1)" )
 }
}
[1] "xts" "zoo"
[[1]]
NULL

[[2]]
[1] "C.Open"     "C.High"     "C.Low"      "C.Close"    "C.Volume"
"C.Adjusted"

Error in inherits(x, "data.frame") : object "Data" not found
11: inherits(x, "data.frame")
10: is.data.frame(x)
9: colnames(x)
8: grep("Close", colnames(x))
7: has.Cl(x)
6: Cl(Data)
5: addTA(Cl(Data) < 3, border = NA, col = "#888888", on = -1)
4: eval(expr, envir, enclos)
3: eval(parse(text = TA[[ta]]), env = thisEnv)
2: chartSeries(Data, TA = "addTA(Cl(Data)<3,border=NA,col='#888888',on=-1)")
1: LoopChart("C")
On Fri, Mar 20, 2009 at 11:08 AM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote: