# Testing quantstrat with very simple strategy: # - go short 1 lot VXX on "2009-02-10" close # - cover short on open one day after return to cash signal appeared # (go to cash == (signal =0) appeared on "2009-02-11" so we should # cover short on "2009-02-12" open) # # I was playing with prefer and delay option but do not work the way I # would like them to. Is this by design and my goal can be accomplished # in some other way? # ############################################################################### require(quantstrat) signals<-function(x, date) { signal <- xts(rep(0, NROW(x)), order.by=index(x)) signal[date] <- -1 colnames(signal) <- c("signal") return(signal) } getSymbols("VXX") VXX <- adjustOHLC(VXX) # Idea from G See: https://stat.ethz.ch/pipermail/r-sig-finance/2012q2/010227.html VXX$PreviousC <- lag.xts(Cl(VXX)) VXX$NextO <- lag.xts(Op(VXX), k=-1) # Test if signals work VXX.signals <- signals(VXX, "2009-02-10") subset="2009-02" dev.new() chart_Series(VXX, subset=subset) add_TA(VXX.signals < 0, border=NA, col="lightsalmon", on=-1) add_TA(VXX.signals) VXX <- VXX[subset] .qty <- 1 .th <- 0.0005 .TxnFees=-1.9 #.txn <- 0 trade.percent <- 1.00 initEq <- 100000 tradeSize <- initEq*trade.percent initDate <- "2009-01-30" .from <- "2002-10-21" .to <- "2008-07-04" #.to <- "2003-12-31" s <- "qs.test" p <- "qs.test" a <- "qs.test" #' Remove objects associated with a strategy #' #' Remove the order_book, account, and portfolio #' @param name name of the portfolio. rm.strat.test <- function(name='default') { if (is.strategy(name)) name <- name[['name']] try(rm(list=paste("order_book",name,sep="."), pos=.strategy)) try(rm(list=paste(c("account", "portfolio"), name, sep="."), pos=.blotter)) } rm.strat.test(p) options(width=240) #Sys.setenv(TZ="GMT") currency("USD") stock("VXX", "USD") initPortf(p, symbols=c("VXX"), initDate=initDate, currency="USD") initAcct(a, portfolios=p, initDate=initDate, currency="USD") initOrders(p, initDate=initDate) ### strategy ###################################################################### strategy(s, store=TRUE) ### indicators s <- add.indicator(s, 'signals', arguments=list( x=quote(VXX), date="2009-02-10" ), label='signal') s <- add.signal(s, 'sigThreshold', arguments = list(column="signal", relationship="eq", threshold=0, cross=TRUE), label='cash') s <- add.signal(s, 'sigThreshold', arguments = list(column="signal", relationship="gt", threshold=0, cross=TRUE), label='long') s <- add.signal(s, 'sigThreshold', arguments = list(column="signal", relationship="lt", threshold=0, cross=TRUE), label='short') s <- add.rule(s, 'ruleSignal', arguments=list(sigcol='cash' , sigval=TRUE, replace=FALSE, TxnFees=.TxnFees, orderside='short', orderqty='all', ordertype='market', prefer='NextO',#'Open', delay=1 ), type='exit', label='ExitShort2CASH') s <- add.rule(s, 'ruleSignal', arguments=list(sigcol='short' , sigval=TRUE, replace=FALSE, TxnFees=.TxnFees, orderside='short', orderqty=-.qty, ordertype='market', prefer='Close',#'PreviousC' delay=0 ), type='enter', label='EnterSHORT') applyStrategy(s, p, verbose = FALSE) #applyStrategy(s, p, prefer='Open', verbose = FALSE) updatePortf(p, Symbols="VXX", , Dates=paste('::',as.Date(Sys.time()),sep='')) ############################################################################### dev.new() chart.Posn(p, "VXX") VXX.signals <- signals(VXX, "2009-02-10") add_TA(VXX.signals < 0, border=NA, col="lightsalmon", on=-1) add_TA(getPortfolio(p)$symbols[["VXX"]]$posPL$Pos.Qty) add_TA(getPortfolio(p)$symbols[["VXX"]]$posPL$Pos.Value) print(getOrderBook(p)) txns <- getTxns(p, "VXX") txns mktdata["2009-02-10/2009-02-12"] #> mktdata["2009-02-10/2009-02-12"] # VXX.Open VXX.High VXX.Low VXX.Close VXX.Volume VXX.Adjusted signal cash long short #2009-02-10 404.0 422.64 395.12 418.40 64200 418.40 -1 0 0 1 #2009-02-11 416.2 419.32 408.40 412.28 21500 412.28 0 1 0 0 #2009-02-12 421.0 432.00 411.04 411.04 25500 411.04 0 0 0 0 # Well, I want this: #> print(getOrderBook(p)) #$qs.test #$qs.test$VXX # Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime Prefer Order.Set Txn.Fees Rule #2009-01-30 "0" NA "init" "long" "0" "closed" "2009-01-30" "" "" "0" "" #2009-02-10 "-1" "418.4" "market" "short" NA "closed" "2009-02-11 00:00:00" "Close" NA "-1.9" "EnterSHORT" #2009-02-12 "all" "421.0" "market" "short" NA "closed" "2009-02-12 00:00:00" "Open" NA "-1.9" "ExitShort2CASH" # # #attr(,"class") #[1] "order_book" #> # > txns <- getTxns(p, "VXX") #> txns # Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL #2009-01-30 0 0.00 0.0 0.00 0.00 0.00 #2009-02-10 -1 418.40 -1.9 -418.40 418.40 -1.90 #2009-02-12 1 421.00 -1.9 421.00 421.00 -6.40 # But I get this: #> print(getOrderBook(p)) #$qs.test #$qs.test$VXX # Order.Qty Order.Price Order.Type Order.Side Order.Threshold Order.Status Order.StatusTime Prefer Order.Set Txn.Fees Rule #2009-01-30 "0" NA "init" "long" "0" "closed" "2009-01-30" "" "" "0" "" #2009-02-10 "-1" "418.4" "market" "short" NA "closed" "2009-02-11 00:00:00" "Close" NA "-1.9" "EnterSHORT" #2009-02-12 "all" "416.2" "market" "short" NA "closed" "2009-02-12 00:00:00" "Open" NA "-1.9" "ExitShort2CASH" # # #attr(,"class") #[1] "order_book" #> # > txns <- getTxns(p, "VXX") #> txns # Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL #2009-01-30 0 0.00 0.0 0.00 0.00 0.00 #2009-02-11 -1 412.28 -1.9 -412.28 412.28 -1.90 #2009-02-12 1 411.04 -1.9 411.04 411.04 -0.66