[R-SIG-Finance] checkBlotterUpdate fails on quantstrat

Erol Biceroglu erol.biceroglu at alumni.utoronto.ca
Tue Jul 21 02:53:05 CEST 2015


Hello,

I've been playing around with quanstrat and was looking forward to running
*apply.paramset()* to optimize the strategy's parameters, only to find an
empty set of results.

(Please note, my actual code follows after my message)

After investigating, I found that *checkBlotterUpdate* fails with the
message:

> checkBlotterUpdate(tradeStrategy,tradeStrategy)
[1] "portfolio P&L doesn't match sum of symbols P&L"
[1] "portfolio P&L doesn't match account P&L"
[1] FALSE

Upon further investigation, I'm unable to run the following:
> tradeStats(tradeStrategy)
NULL
Warning message:
In tradeStats(tradeStrategy) : TotalNetProfit NA forSPY

When I run this line (after executing everything), I find that the
"strategy date" (the first date before the beginning of the time series) is
duplicated, and in the 2nd instance, there are NAs in:
-Pos.Value
-Period.Unrealized.PL
-Gross.Trading.PL
-Net.Trading.PL

I've tried to play around with it and unfortunately I can't figure out what
would cause the duplicate, generate the NA.  Any thoughts or feedback would
be greatly appreciated.  Thanks for your help.

Here's the code:
---------
rm(list=ls())
library(quantstrat)
library(timeDate)
library(stringr)
library(IKTrading)


Sys.setenv(TZ="UTC")
startDate<-as.Date("1993-02-02", format="%Y-%m-%d")
endDate<-as.Date("2015-07-17", format="%Y-%m-%d")

getSymbols(Symbols = c("SPY", "^VIX")
           ,src="yahoo"
           , verbose=TRUE
           , warnings=TRUE
           , auto.assign=TRUE
           , return.class = "xts"
           , index.class = "Date"
           ,from = startDate
           , to = endDate
)

#set Financial instrument objects
currency("USD")
stock(primary_id = c("SPY", "VIX"),currency = "USD")


#name
tradeStrategy <-"SPYVIXStrategy"

#Date, one day before prices
strategyDate <- min(index(SPY)) - 1


#rm.strat(tradeStrategy)
#rm(mktdata)

NumSh<-3
VIXThreshold <- 20
PctThreshold <- 0.75

#init portfolio and account
initPortf(name = tradeStrategy
          , symbols = "SPY" #as defined in Financial instrument
          , initDate = strategyDate)

initAcct(name = tradeStrategy
         ,portfolios = tradeStrategy
         ,initDate = strategyDate
         ,initEq = 10e6 #as.vector(first(SPY$SPY.Close))*NumSh
)



#order book, and strategy
initOrders(portfolio = tradeStrategy
           , initDate = strategyDate
)

#position limits
addPosLimit(tradeStrategy, symbol = "SPY", strategyDate, maxpos = NumSh,
longlevels = NumSh)

strategy( tradeStrategy, store = TRUE)



#define indicator function
PctStrat <- function(x){
data<-merge(
  runPercentRank(x=ifelse(diff(x)<0,0,diff(x)),cumulative = FALSE
  )
  , ifelse(diff(x)<0,0,diff(x))
  , x
)
names(data) <- c("Percentile", "PositiveDiffs","VIX.Close")

data<-xts(x = sapply(data
                     ,function(x){ifelse(is.na(x),0,x)}
                    ), order.by = index(data)
          )

return(data)
}


#add indicator
add.indicator(strategy = tradeStrategy
  , name = "PctStrat"
, arguments = list( x = quote(VIX$VIX.Close))
  , label = "VIXPct"
)


# >=75th percentile move
add.signal(strategy = tradeStrategy
           , name = "sigThreshold"
           , arguments = list(column = c("Percentile.VIXPct")
                              , threshold = quote(PctThreshold) #0.75
                              , relationship = "gte"
                              , cross = FALSE
           )
           , label = "Pct.gte.3Qt"
)

#<75th percentile move
add.signal(strategy = tradeStrategy
           , name = "sigThreshold"
           , arguments = list(column = c("Percentile.VIXPct")
                              , threshold = quote(PctThreshold) #0.75
                              , relationship = "lt"
                              , cross = FALSE
           )
           , label = "Pct.lt.3Qt"
)

#>VIX 20
add.signal(strategy = tradeStrategy
           , name = "sigThreshold"
           , arguments = list(column = c("VIX.Close.VIXPct")
                              , threshold = quote(VIXThreshold) #20
                              , relationship = "gt"
                              , cross = FALSE
           )
           , label = "HighVolatility"
)


#<=VIX 20
add.signal(strategy = tradeStrategy
           , name = "sigThreshold"
           , arguments = list(column = c("VIX.Close.VIXPct")
                              , threshold = quote(VIXThreshold) #20
                              , relationship = "lte"
                              , cross = FALSE
           )
           , label = "LowVolatility"
)

#intersect signals
add.signal(strategy = tradeStrategy
           , name = "sigAND"
           , arguments = list(columns = c("Pct.lt.3Qt", "LowVolatility"),
cross = TRUE)
           , label = "Pct.lt.3qt.LowVol"
)

add.signal(strategy = tradeStrategy
           , name = "sigAND"
           , arguments = list(columns = c("Pct.lt.3Qt", "HighVolatility"),
cross = FALSE)
           , label = "Pct.lt.3qt.HighVol"
)

add.signal(strategy = tradeStrategy
           , name = "sigAND"
           , arguments = list(columns = c("Pct.gte.3Qt", "HighVolatility"),
cross = FALSE)
           , label = "Pct.gte.3qt.HighVol"
)

add.signal(strategy = tradeStrategy
           , name = "sigAND"
           , arguments = list(columns = c("Pct.gte.3Qt", "LowVolatility"),
cross = TRUE)
           , label = "Pct.gte.3qt.LowVol"
)

#rules
add.rule(strategy = tradeStrategy
         , name = "ruleSignal"
         , arguments = list(sigcol="Pct.lt.3qt.LowVol"
                            , sigval=TRUE
                            , orderqty=NumSh
                            , ordertype="market"
                            , orderside=NULL#"long"
                            , osFUN = "osMaxPos"
         )
         , type = "enter"
)

add.rule(strategy = tradeStrategy
         , name = "ruleSignal"
         , arguments = list(sigcol= "HighVolatility" #"Pct.lt.3qt.HighVol"
                            , sigval=TRUE
                            , orderqty="all"
                            , ordertype="market"
                            , orderside=NULL#"long"
                            , osFUN = "osMaxPos"
         )
         , type = "exit"
)

add.rule(strategy = tradeStrategy
         , name = "ruleSignal"
         , arguments = list(sigcol="Pct.gte.3qt.LowVol"
                            , sigval=TRUE
                            , orderqty=1
                            , ordertype="market"
                            , orderside=NULL#"long"
                            , osFUN = "osMaxPos"
         )
         , type = "exit"
)


applyStrategy(strategy = tradeStrategy
              , portfolios = tradeStrategy
)

updatePortf(tradeStrategy)
updateAcct(tradeStrategy)
updateEndEq(tradeStrategy)


###From Guy Yollin's Slides
checkBlotterUpdate <- function(port.st,account.st,verbose=TRUE)
{
  ok <- TRUE
  p <- getPortfolio(port.st)
  a <- getAccount(account.st)
  syms <- names(p$symbols)
  port.tot <- sum(sapply(syms,FUN = function(x) eval(parse(
    text=paste("sum(p$symbols",x,"posPL.USD$Net.Trading.PL)",sep="$")))))
  port.sum.tot <- sum(p$summary$Net.Trading.PL)
  if( !isTRUE(all.equal(port.tot,port.sum.tot)) ) {
    ok <- FALSE
    if( verbose )
      print("portfolio P&L doesn't match sum of symbols P&L")
  }
  initEq <- as.numeric(first(a$summary$End.Eq))
  endEq <- as.numeric(last(a$summary$End.Eq))
  if( !isTRUE(all.equal(port.tot,endEq-initEq)) ) {
    ok <- FALSE
    if( verbose )
      print("portfolio P&L doesn't match account P&L")
  }
  if( sum(duplicated(index(p$summary))) ) {
    ok <- FALSE
    if( verbose )
      print("duplicate timestamps in portfolio summary")
  }
  if( sum(duplicated(index(a$summary))) ) {
    ok <- FALSE
    if( verbose )
      print("duplicate timestamps in account summary")
  }
  return(ok)
}
###End Guy Yollin's code

#This fails
checkBlotterUpdate(tradeStrategy,tradeStrategy)

chart.Posn(tradeStrategy
         , Symbol = "SPY"
         #, Dates = "1994::"
         #, Dates = "2012::"
         ,TA = "add_TA(VIX$VIX.Close)"

)

#Here's an error
tradeStats(tradeStrategy)

#Here's the source of the error, an NA on the 2nd line
getPortfolio(tradeStrategy)$symbols$SPY$posPL[1:10]

-------
End code


Erol Biceroglu


*erol.biceroglu at alumni.utoronto.ca <erol.biceroglu at alumni.utoronto.ca>*

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list