Hi Cristi,

1) You are using column='signal' in add.signal(), but there is no column 
called 'signal' in your mktdata object. You should add a label to your 
indicator, eg. label='signal' to make that work.

However, while testing your code, I found a problem with the way 
quantstrat deals with ROC-indicator; as a result the indicator is named 
'<instrument>.Close.1', and your label would be ignored. While we are 
working to provide a fix, I suggest that you use 'Close.1' as the signal 
name in add.signal() (hence column="Close.1" in the parameter list). 
Will let you know when a real fix is available.

2) You are using 'prefer' in the wrong way; it is meant to refer to the 
preferred column for the price in the mktdata object, and defaults to 
'Close'. As this is an optional parameter, I suggest that you leave it 
out for now, until you have a working program. Currently your program 
will fail because there is no column named 'stock.str'.

A couple of other points of attention:

=> remove the install.packages() from your code; you only need to do 
this once for your system

=> library(quanstrat) is sufficient, it will pull in all the other 
necessary libraries automatically, as it depends on them

=> you are using T/F instead of TRUE/FALSE. This is bad practice, T and 
F are variables, consider if someone defines T=FALSE and F=TRUE, this 
would break you program

=> you are hiding all output from functions or assign them to "dummy" 
variables. It is better to have everything dumped to the screen, at 
least until your program works correctly, so you get information about 
what is going on

HTH,

Jan Humme.


On 12-10-12 12:22, Cristi Popescu wrote:
> Hello,
>
> I am resending my code. I have also attached the warnings presented in the code (with the reluctance that the problem comes from those because I have applied the same code with a different indicator and it worked without a problem).
>
> Strategy code:
>
>
> rm(list=ls(all=TRUE))
> install.packages("blotter",
>              repos="http://R-Forge.R-project.org")
> install.packages("FinancialInstrument",
>              repos="http://R-Forge.R-project.org")
> install.packages("quantstrat",
>              repos="http://R-Forge.R-project.org")
> library('quantmod')
> library('blotter')
> library('PerformanceAnalytics')
> library('parallel')
> library('quantstrat')
> library('foreach')
> library('lattice')
> library('TTR')
> library('quantstrat')
> library('blotter')
> library('xts')
>
> .instrument <- new.env()
> .blotter <- new.env()
> .strategy <- new.env()
> try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE)
> try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE)
>
> currency("USD")
> stock.str <- c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI",
>              "XLB", "XLK", "XLU")
> for (symbol in stock.str){
>    stock(symbol, currency ="USD", multiplier=1)
> }
> start.data <- as.Date("2008-1-1")
> end.data <- as.Date (Sys.time())
> initDate <- start.data-1
>
> for (symbol in stock.str){
>    getSymbols(symbol, from=start.data, to=end.data, adjust=T)
> }
>
> strat.name <- "ROC"
> initEq = 10000
> dummy <- initPortf(name=strat.name,
>              symbols=stock.str,initDate=initDate)
> dummy <-
>              initAcct(name=strat.name,portfolios=strat.name,initDate=initDate,
>              initEq=initEq)
> initOrders(portfolio=strat.name,initDate=initDate)
> strat <- strategy(strat.name)
>
> #rocPortfolio <- getPortfolio(strat.name)
> nper=3
> #indicators
> strat <- add.indicator(strategy = strat, name="ROC",
>    arguments = list(x = quote(getPrice(mktdata)), n=nper))
>
> #signals
> strat <- add.signal(strategy=strat,
>              name="sigThreshold",arguments = list
>    (column='signal', relationship="gte",
>              threshold=-0.05,cross=T),label="signal.gt.zero")
> strat <- add.signal(strategy =
>              strat,name="sigThreshold",arguments = list
>   
>              (column="signal",relationship="lte",threshold=-0.01,cross=TRUE),
>              label="signal.lt.zero")
>
> #rules
> strat <- add.rule(strategy = strat,name='ruleSignal',
>              arguments = list
>    (sigcol="signal.gt.zero",sigval=TRUE, orderqty=100,
>              ordertype='market',
>     orderside='long'),type='enter')
> strat <- add.rule(strategy = strat,name='ruleSignal',
>              arguments = list
>    (sigcol="signal.lt.zero",sigval=TRUE, orderqty='all',
>              ordertype='market',
>     orderside='long'),type='exit')
>
> # apply strategy
> out<-try(applyStrategy(strategy=strat,
>              portfolios=strat.name,
>    parameters=list(n=nper),prefer= 'stock.str',verbose=F))
> dummy <-
> updatePortf(Portfolio=strat.name,Dates=paste('::',as.Date(Sys.time()),sep=''))
>
> The warnings messages are these:
>
>
>> warnings() Warning messages:
> 1: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 60612 != reported length 200
> 2: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 375 != reported length 200
> 3: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 581 != reported length 200
> 4: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59280 != reported length 200
> 5: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 6: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 7: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 60260 != reported length 200
> 8: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 9: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 10: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59343 != reported length 200
> 11: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 12: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 13: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59318 != reported length 200
> 14: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 15: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 16: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59957 != reported length 200
> 17: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 18: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 19: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59806 != reported length 200
> 20: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 21: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 22: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59555 != reported length 200
> 23: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 355 != reported length 200
> 24: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 553 != reported length 200
> 25: In download.file(paste(yahoo.URL, "s=", Symbols.name,  ... : downloaded length 59292 != reported length 200
> 26: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 375 != reported length 200
> 27: In download.file(paste(yahoo.URL, Symbol.name, "&a=",  ... : downloaded length 581 != reported length 200 Best Regards, Cristian Popescu
>
> ----- Forwarded Message -----
> From: OpenTrades <jan@opentrades.nl>
> To: r-sig-finance@r-project.org
> Sent: Thursday, October 11, 2012 4:11 PM
> Subject: Re: [R-SIG-Finance] Error with applyStrategy
>   
> Can you please repost your code Cristi? It is hard to read because of
> strange characters and line breaks (see below). Perhaps you can use
> pastie.org to paste it or so.
>
> Anyway, I do see a message after getSymbols():
>
> There were 27 warnings (use
> warnings() to see them)
>
>
> I suggest that you track down these warnings to see if they might be
> causing your problem.
>
> Best regards,
>
> Jan Humme.
>
>
>
> On 11-10-12 08:45, Cristi Popescu wrote:
>> Hello,
>>
>> I am writing as I have been struggling to get Quantstrat going without any success (please find beneath my code). I believe there is an error with the ROC indicator or the way I have applied it, as the strategy works with other indicators. I have seen similar posts on the site, with the same error, and have tried the proposed solutions without success.
>>
>> Secondly, I would like to ask you how do you suggest implementing the proposed trades from Quantstrat for live trading. Do you know of any tutorial, book, article, anything that could help.
>>
>> Kind regards,
>>
>> Cristian
>>
>> Ā
>> Ā
>>> install.packages("blotter",
>> repos="http://R-Forge.R-project.org")
>> Installing package(s) into
>> āEUR~C:/Users/Cristian/Documents/R/win-library/2.15āEUR^(TM)
>> (as āEUR~libāEUR^(TM) is unspecified)
>> Warning in install.packages :
>> Ā  package āEUR~blotterāEUR^(TM) is in use and will not be installed
>>> install.packages("FinancialInstrument",
>> repos="http://R-Forge.R-project.org")
>> Installing package(s) into
>> āEUR~C:/Users/Cristian/Documents/R/win-library/2.15āEUR^(TM)
>> (as āEUR~libāEUR^(TM) is unspecified)
>> Warning in install.packages :
>> Ā  package āEUR~FinancialInstrumentāEUR^(TM) is in use and will not be installed
>>> install.packages("quantstrat",
>> repos="http://R-Forge.R-project.org")
>> Installing package(s) into
>> āEUR~C:/Users/Cristian/Documents/R/win-library/2.15āEUR^(TM)
>> (as āEUR~libāEUR^(TM) is unspecified)
>> Warning in install.packages :
>> Ā  package āEUR~quantstratāEUR^(TM) is in use and will not be installed
>>> library('quantmod')
>>> library('blotter')
>>> library('PerformanceAnalytics')
>>> library('parallel')
>>> library('quantstrat')
>>> library('foreach')
>>> library('lattice')
>>> library('TTR')
>>> library('quantstrat')
>>> library('blotter')
>>> library('xts')
>>>
>>> .instrument <- new.env()
>>> .blotter <- new.env()
>>> .strategy <- new.env()
>>> try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE)
>>> try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE)
>>>
>>> currency("USD")
>> [1] "USD"
>>> stock.str <-
>> c("XLF", "XLP", "XLE", "XLY",
>> "XLV", "XLI", "XLB", "XLK",
>> "XLU")
>>> for (symbol in stock.str){
>> + Ā Ā stock(symbol, currency ="USD",
>> multiplier=1)
>> + }
>>> start.data <-
>> as.Date("2008-1-1")
>>> end.data <- as.Date
>> (Sys.time())
>>> initDate <- start.data-1
>>>
>>> for (symbol in stock.str){
>> + Ā Ā getSymbols(symbol, from=start.data,
>> to=end.data, adjust=T)
>> + }
>> There were 27 warnings (use
>> warnings() to see them)
>>> strat.name <-
>> "ROC"
>>> initEq = 10000
>>> dummy <-
>> initPortf(name=strat.name, symbols=stock.str,initDate=initDate)
>>> dummy <-
>> initAcct(name=strat.name,portfolios=strat.name,initDate=initDate,
>> initEq=initEq)
>>> initOrders(portfolio=strat.name,initDate=initDate)
>>> strat <-
>> strategy(strat.name)
>>> #rocPortfolio <-
>> getPortfolio(strat.name)
>>> nper=3
>>> #indicators
>>> strat <- add.indicator(strategy
>> = strat, name="ROC",
>> + Ā Ā arguments = list(x =
>> quote(getPrice(mktdata)), n=nper))
>>> #signals
>>> strat <-
>> add.signal(strategy=strat, name="sigThreshold",arguments = list
>> + Ā Ā (column='signal',
>> relationship="gte", threshold=-0.05,cross=T),label="signal.gt.zero")
>>> strat <-
>> add.signal(strategy = strat,name="sigThreshold",arguments = list
>> + Ā Ā (column="signal",relationship="lte",threshold=-0.01,cross=TRUE),
>> label="signal.lt.zero")
>>> #rules
>>> strat <- add.rule(strategy
>> = strat,name='ruleSignal', arguments = list
>> + Ā Ā (sigcol="signal.gt.zero",sigval=TRUE,
>> orderqty=100, ordertype='market',
>> + Ā Ā Ā orderside='long'),type='enter')
>>> strat <- add.rule(strategy
>> = strat,name='ruleSignal', arguments = list
>> + Ā Ā (sigcol="signal.lt.zero",sigval=TRUE,
>> orderqty='all', ordertype='market',
>> + Ā Ā Ā orderside='long'),type='exit')
>>> # apply strategy
>>> out<-try(applyStrategy(strategy=strat,
>> portfolios=strat.name,
>> + Ā Ā parameters=list(n=nper),prefer=
>> 'stock.str',verbose=F))
>> Error in .xts(e, .index(e1),
>> .indexCLASS = indexClass(e1), .indexFORMAT = indexFormat(e1),Ā  :
>> Ā  index length must match number of observations
>> In addition: Warning messages:
>> 1: In match.names(column,
>> colnames(data)) :
>> Ā  all columns not located in signal for XLF.Open XLF.High XLF.Low
>> XLF.Close XLF.Volume XLF.Adjusted XLF.Close.1
>> 2: In min(j, na.rm = TRUE) :
>> Ā  no non-missing arguments to min; returning Inf
>> 3: In max(j, na.rm = TRUE) :
>> Ā  no non-missing arguments to max; returning -Inf
>>> dummy <- updatePortf(Portfolio=strat.name,Dates=paste('::',as.Date(Sys.time()),sep=''))
>>      [[alternative HTML version deleted]]
>>
>>
>>
>> _______________________________________________
>> R-SIG-Finance@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>> -- Subscriber-posting only. If you want to post, subscribe first.
>> -- Also note that this is not the r-help list where general R questions should go.
>
>
>
> _______________________________________________
> R-SIG-Finance@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.


-- 
Jan Humme - OpenTrades

WWW:     http://www.opentrades.nl
Email:   jan@opentrades.nl
Twitter: @opentrades


	[[alternative HTML version deleted]]

