# [R-SIG-Finance] Quantmod - chartSeries

Andreas Johansson Andreas.Johansson at mbamfunds.com
Mon Mar 23 11:41:27 CET 2009

```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.

> 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=NULL)
# 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 ********

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))
}
}

> LoopChart("C")
[1] "xts" "zoo"
[[1]]
NULL

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

> traceback()
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)
1: LoopChart("C")

On Fri, Mar 20, 2009 at 11:08 AM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
> Andreas,
>
> Reproducible code would be of great help, as would actual output and a
> traceback() call, but without that here is my guess.
>
> The code that draws TA objects onto the chart does a bit of testing to
> 'see' where the call comes from.  Inside of the chartSeries call, the
> code gets evaluated but not drawn until later.  Outside (from the
> command line) it simply updates critical parts of the chart in memory,
> then redraws the entire thing. The nesting inside is the culprit ---
> and to change this is quite difficult.
>
> One possible work-around may be to not wrap in a pdf() call, but
> instead use dev.copy2pdf after the chart & TA has been drawn at each
> iteration of the loop.
>
>
> HTH,
> Jeff
>
> R/Finance 2009: Applied Finance with R
> April 24 and 25th, 2009 Chicago, IL USA
> http://www.RinFinance.com
>
> EARLY REGISTRATION ENDS MARCH 31st!
> On Fri, Mar 20, 2009 at 10:36 AM, Andreas Johansson
> <Andreas.Johansson at mbamfunds.com> wrote:
>> Hi,
>>
>>
>>
>> am using chartSeries to create a set of charts that I output to pdf in a
>> loop. Works fine but have encountered a problem. If I want to loop over a
>> call like
>>
>>
>>
>> for (i=1:10){
>>
>>                 Data=getSymbols(ID[i])
>>
>>
>> )
>>
>> }
>>
>>
>>
>> I cannot do it as chartSeires complains that object Data does not exist
>>
>>
>>
>> does not complain if I do
>>
>> for (i=1:10){
>>
>>                 Data=getSymbols(ID[i])
>>
>>                 chartSeries(Data)
>>
>>
>>
>> }
>>
>>
>>
>> Is there a way of making the call to chart series in one go and referencing
>> to Data in the addTA function?
>>
>>
>>
>> If I run Data=getSymbols(ID[i]) and  then
>> ) in the console it works fine.
>>
>>
>>
>> regards
>>
>>
>>
>> Andreas
>>
>>
>>
>> Andreas Johansson
>> Quantitative Analyst
>>
>>
>>
>> Marble Bar Asset Management LLP
>> 11-12 St James Square
>> London, SW1Y 4LB
>>
>> Direct +44 (0) 20 3023 8141
>> Fax +44 (0) 20 3023 8065
>> Mobile +44 (0) 7747 725992
>> Andreas.Johansson at mbamfunds.com
>>
>>
>>
>> .
>>
>> This message is intended only for the use of the person(s) to whom it is
>> addressed. It may contain information which is privileged and confidential.
>> Accordingly any unauthorised use is strictly prohibited. If you are not the
>>
>> It is not intended as an offer or solicitation for the purchase or sale of
>> any financial instrument or as an official confirmation of any transaction,
>> unless specifically agreed otherwise. All market prices, data and other
>> information are not warranted as to completeness or accuracy and are subject
>> to change without notice. Any opinions or advice contained in this Internet
>> email are subject to the terms and conditions expressed in any applicable
>> governing Marble Bar Asset Management LLP's terms and conditions of business
>> or client agreement letter. Any comments or statements made herein do not
>> necessarily reflect those of Marble Bar Asset Management LLP.
>>
>> Marble Bar Asset Management LLP is regulated and authorised by the FSA.
>>
>> _______________________________________________
>> R-SIG-Finance at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only.
>> -- If you want to post, subscribe first.
>>
>
>
>
> --
> Jeffrey Ryan
> jeffrey.ryan at insightalgo.com
>
> ia: insight algorithmics
> www.insightalgo.com
>

--
Jeffrey Ryan
jeffrey.ryan at insightalgo.com

ia: insight algorithmics
www.insightalgo.com

.

This message is intended only for the use of the person(s) to whom it is addressed. It may contain information which is privileged and confidential. Accordingly any unauthorised use is strictly prohibited. If you are not the intended recipient, please contact the sender as soon as possible.

It is not intended as an offer or solicitation for the purchase or sale of any financial instrument or as an official confirmation of any transaction, unless specifically agreed otherwise. All market prices, data and other information are not warranted as to completeness or accuracy and are subject to change without notice. Any opinions or advice contained in this Internet email are subject to the terms and conditions expressed in any applicable governing Marble Bar Asset Management LLP's  terms and conditions of business or client agreement letter. Any comments or statements made herein do not necessarily reflect those of Marble Bar Asset Management LLP.

Marble Bar Asset Management LLP is regulated and authorised by the FSA.

```