[R-SIG-Finance] Problems with quantstrat and my own code

Aleksandr Rudnev alex.rudnev at gmail.com
Tue Dec 14 14:43:32 CET 2010


Brian,

I've seen those warnings typically in 2 places.
One during applyStrategy, it happens in getOrders(..., "open", ...)
when there are no open orders as part of the following invocation
chain doTryCatch -> applyStrategy -> getOrders -> [ -> [.xts.

applyStrategy
...
rem.orders <- getOrders(portfolio=portfolio, symbol=symbol, status="open")
...

getOrders
...
ordersubset<-orderbook[[portfolio]][[symbol]][indices,]
...
("indices" is empty, as there are no "open" orders where there are no
non-path-dependent rules or they don't produce any orders)

Another one in visualization code, when there were no trades:
...
  2: chart.Posn(Portfolio = my.name, Symbol = my.ticker, theme = my.theme)
  3: Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades > 0)]
  4: `[.xts`(Portfolio$symbols[[Symbol]]$txn$Txn.Price, which(Trades > 0))
  5: .signalSimpleWarning("no non-missing arguments to max; returning
-Inf", quote(max(i)))
...

Neither of those warnings seem to be critical, unless you turn
warnings into errors via option(warn = 2) or something like that :)
Couple of extra checks can eliminate those warnings to reduce
confusion and doubts.

Regards,
Alex

On Tue, Dec 14, 2010 at 5:57 AM, Brian G. Peterson <brian at braverock.com> wrote:
> OK, first, to your question:
>
> There is no book that will teach you modeling in R.  Strategy modeling is
> a very complex and often proprietary task.  There are many books that
> purport to teach strategy modeling.  All the ones I own are pretty bad.
> None of them use R.  Some of them are useful as a compilation of common
> widely known strategy parameters, indicators, signals, and rules.
>
> There's a reason that successful quantitative strategists are well
> compensated: what we do is hard, and involves art, science, math, and a
> little bit of luck.  'quantstrat', and the rest of the toolchain, represent
> tools that you can use to make you more productive as you create
> strategies.
>
> Second, to your problems.
>
> 1> quantstrat
>
> That's a *Warning*, not an *Error*, and is coming out of xts somewhere,
> probably in subsetting, since we do so much of that.  Because it is a
> Warning, and not an Error, I haven't taken the time to figure out where it
> is and either code around it or suppress it.
>
> 2> your code
>
> I'm not going to debug your ad-hoc strategy code for you, sorry.  Every
> time I set out to write an ad-hoc backtest of a strategy, I screw
> *something* up. We wrote a framework to concentrate the bugs in one place,
> so that we could eliminate them one at a time, and spend more time actually
> modeling strategies. Another way of putting that would be that we we wrote
> the framework so that we could have a different kind of discussion with the
> user of our open source code.  That discussion would look something like
> this:
>
> "I ran the quantstrat demo('faber') code on instrument 'TYC' and
> instrument 'XLY', pdf's of chart.Posn() attached.
> I believe the demo code for market orders is creating incorrect results
> here <insert detailed description> by <insert your hypothesis of what is
> wrong>. Could someone please check this and let me know?  I'd be happy to
> help patch the code.  Thanks."
>
> Since I think that's probably more or less what you meant, I'll take a
> look at the faber demo on TYC and XLY when I get to the office, and look
> for any obvious problems with the trades and P&L.  If you've found
> something specific that might raise concerns (all the data is available
> either  in the output of applyStrategy or in the portfolio to double-check
> the indicators, signals, and rules), please share.
>
> Regards,
>
>  - Brian
>
>
> On Tue, 14 Dec 2010 00:53:33 -0600, Aaditya Nanduri
> <aaditya.nanduri at gmail.com> wrote:
>> Hello All,
>>
>> I have 2 problems and 1 question today.
>>
>> QUESTION : Are there any book that teach modelling in R? Hopefully an
>> introductory book. I would like to learn how to build/backtest
> strategies,
>> so if there are any introductory books, please share.
>>
>> PROBLEMS:
>> 1) Quantstrat
>>
>> I tried to use the Faber demo but changed all the symbols to just "TYC"
>> and I get the following errors:
>>    .
>>    .
>>    .
>>  # Process the indicators and generate trades
>>> start_t<-Sys.time()
>>> out<-try(applyStrategy(strategy=stratFaber , portfolios='faber'))
>> *Warning message:*
>> *In max(i) : no non-missing arguments to max; returning -Inf*
>>> end_t<-Sys.time()
>>    .
>>    .
>>    .
>> *Warning messages:*
>> *1: In max(i) : no non-missing arguments to max; returning -Inf*
>> *2: In max(i) : no non-missing arguments to max; returning -Inf*
>>
>> I have no idea what they mean...
>>
>> 2) My code:
>>
>>
>> I know this code is wrong but I dont know where the error is. To me, it
>> seems right. But when I ran "XLY" through the faber demo and the code I
>> wrote, the end results couldnt be further from each other.
>> I also know that my code is highly inefficient so if there are any
> helpful
>> hints that you would like to throw my way, I am open to everything.
>>
>>
> ------------------------------------------------CODE-----------------------------------
>> library(quantmod)
>>
>> # Get Data
>> getSymbols("TYC", from = "1998-01-01", to = Sys.Date())
>>
>> # Convert from daily to monthly
>> x <- get(TYC)
>> x <- to.monthly(XLY, indexAt = 'lastof', drop.time= TRUE)
>> indexFormat(x) <- '%Y-%m-%d'
>> colnames(x) <- gsub("x", "XLY", colnames(x))
>> assign("XLY", x)
>>
>> price <- as.numeric(Cl(XLY))
>> # Build indicators
>> sma10 <- SMA(price, n=10)
>>
>> for(i in 1:length(price)){ buy[i] <- NA; sell[i] <- NA }
>> # Buy signal
>> buy[which(price > sma10)] <- 1
>> buy[which(is.na(buy))] <- 0
>>
>> # Sell signal
>> sell[which(price < sma10)] <- -1
>> sell[which(is.na(sell))] <- 0
>>
>> signal <- buy + sell
>> signal[which(is.na(signal))] <- 0
>>
>> x <- NA
>>
>> for(i in 1:length(signal)){
>>  # Start with buying not selling
>> if(signal[i-1] == 0 && signal[i] == -1) {
>> signal[i] = 0
>> }
>> }
>>
>> for(i in 1:length(signal)){
>>
>> if(signal[i-1] == 0 && signal[i] == 1) {
>> x[i] <- 1
>> }
>> if(signal[i-1] == -1 && signal[i] == 1) {
>> x[i] <- 1
>> }
>> if(signal[i-1] == 1 && signal[i] == -1) {
>> x[i] <- -1
>> }
>> }
>>
>> x[which(is.na(x))] <- 0
>>
>> money <- 100000 -> initEq
>> stock <- 0
>> numinP <- 0
>>
>> for(i in 1:length(x)){
>>  if(x[i] == 1) {
>> p <- price[i]
>> b <- floor(money/p)
>> numinP <- numinP + b
>> money <- money - p*b
>> stock <- p*b
>>  }
>> if(x[i] == -1) {
>> p <- price[i]
>> money <- money + p*numinP
>> stock <- 0
>> numinP <- 0
>> }
>> }
>>
>> total <- money + stock
>> print(paste("The initial equity:",initEq))
>> print(paste("After 10 years of trading using TAA:",total))
>>
>>
>> I appreciate all your help
>>
>> --
>> Aaditya Nanduri
>> aaditya.nanduri at gmail.com
>>
>>       [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-SIG-Finance at 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.
>
> --
> Brian G. Peterson
> http://braverock.com/brian/
> Ph: 773-459-4973
> IM: bgpbraverock
>
> _______________________________________________
> R-SIG-Finance at 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.
>



More information about the R-SIG-Finance mailing list