2014-11-03 9 views
2

当我使用:pennyPerShare quantstrat不工作

stratRank <- add.rule(stratRank, name="ruleSignal", 
      arguments=list(sigcol="EntryCond", sigval=TRUE, orderqty=max.size, 
      ordertype="market", orderside="long", pricemethod="market", 
      replace=FALSE,osFUN=osMaxPos,TxnFees=pennyPerShare), type="enter") 


    stratRank <- add.rule(stratRank, name="ruleSignal", 
       arguments=list(sigcol="ExitCond", sigval=TRUE, orderqty="all", 
       ordertype="market", orderside="long", pricemethod="market", 
       replace=FALSE,TxnFees=pennyPerShare), type="exit") 

我得到一个错误:

Error in `[.xts`(ordersubset, , "Order.Status") : unsupported type 

我认为pennyPerShare功能可以添加?难道我做错了什么?

注意,that works,如果我用静态交易成本:

stratRank <- add.rule(stratRank, name="ruleSignal", 
      arguments=list(sigcol="EntryCond", sigval=TRUE, orderqty=max.size, 
      ordertype="market", orderside="long", pricemethod="market", 
      replace=FALSE,osFUN=osMaxPos,TxnFees=-5), type="enter") 


    stratRank <- add.rule(stratRank, name="ruleSignal", 
       arguments=list(sigcol="ExitCond", sigval=TRUE, orderqty="all", 
       ordertype="market", orderside="long", pricemethod="market", 
       replace=FALSE,TxnFees=-5), type="exit") 

而且session info:为建立当期的。伟大的任何建议。顺祝商祺

sessionInfo() 
R version 3.0.2 (2013-09-25) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 

locale: 
[1] LC_COLLATE=Swedish_Sweden.1252 LC_CTYPE=Swedish_Sweden.1252 LC_MONETARY=Swedish_Sweden.1252 
[4] LC_NUMERIC=C     LC_TIME=Swedish_Sweden.1252  

attached base packages: 
[1] graphics grDevices utils  datasets stats  methods base  

other attached packages: 
[1] quantstrat_0.8.2   foreach_1.4.1    blotter_0.9.1644   PerformanceAnalytics_1.1.0 
[5] FinancialInstrument_1.1.9 quantmod_0.4-0    TTR_0.22-0.1    Defaults_1.1-1    
[9] xts_0.9-7     zoo_1.7-10     ggplot2_0.9.3.1   

loaded via a namespace (and not attached): 
[1] codetools_0.2-8 colorspace_1.2-3 dichromat_2.0-0 digest_0.6.3  fBasics_3010.86 
[6] fGarch_3010.82  grid_3.0.2   gtable_0.1.2  iterators_1.0.6 labeling_0.2  
[11] lattice_0.20-23 MASS_7.3-29  munsell_0.4.2  plyr_1.8   proto_0.3-10  
[16] rCharts_0.4.2  RColorBrewer_1.0-5 reshape2_1.2.2  RJSONIO_1.0-3  ROAuth_0.9.3  
[21] scales_0.2.3  stabledist_0.6-6 stringr_0.6.2  timeDate_3010.98 timeSeries_3010.97 
[26] tools_3.0.2  whisker_0.3-2  yaml_2.1.7 

Code:

library(quantstrat) 
library(PerformanceAnalytics) 

ttz<-Sys.getenv('TZ') 
#Sys.setenv(TZ='UTC') 
Sys.setenv(TZ='GMT') 

currency("USD") 
symbols <- c("XLY", "XLP", "XLE", "AGG", "IVV") 
stock(symbols, currency="USD") 

# get data for the symbols 
getSymbols(symbols, from="2005-01-01", to="2012-12-31") 

# create an xts object of monthly adjusted close prices 
symbols.close <- monthlyPrices(symbols) 

# create an xts object of the symbol ranks 
sym.rank <- applyRank(x=symbols.close, rankFun=ave3ROC, n=c(2, 4, 6)) 

# this is an important step in naming the columns, e.g. XLY.Rank 
# the "Rank" column is used as the trade signal (similar to an indicator) 
# in the qstratRank function 
colnames(sym.rank) <- gsub(".Close", ".Rank", colnames(sym.rank)) 

# ensure the order of order symbols is equal to the order of columns 
# in symbols.close 
stopifnot(all.equal(gsub(".Close", "", colnames(symbols.close)), symbols)) 

# bind the rank column to the appropriate symbol market data 
# loop through symbols, convert the data to monthly and cbind the data 
# to the rank 
for(i in 1:length(symbols)) { 
    x <- get(symbols[i]) 
    x <- to.weekly(x,indexAt='lastof',drop.time=TRUE) 
    indexFormat(x) <- '%Y-%m-%d' 
    colnames(x) <- gsub("x",symbols[i],colnames(x)) 
    x <- cbind(x, sym.rank[,i]) 
    assign(symbols[i],x) 
} 

bt <- qstratRank(symbols=symbols, init.equity=100000, top.N=2, 
       max.size=1000, max.levels=1) 

Functions:

qstratRank <- function(symbols, init.equity=100000, top.N=1, 
          max.size=1000, max.levels=1) { 
     # The qstratRank function uses the quantstrat framework to backtest a 
     # ranking or relative strength strategy 
     # 
     # args 
     # symbols  : character vector of symbols 
     # init.equity : initial equity 
     # top.N  : trade the top N ranked assets 
     # max.size : maximum position size 
     # max.levels : maximum levels to scale in a trade 
     # max.size and max.levels are passed to addPosLimit 
     # 
     # return value 
     # returns a list: end.eq, returns, book, stats 

     # remove variables 
     suppressWarnings(rm("order_book.Rank", pos=.strategy)) 
     suppressWarnings(rm("account.Rank", "portfolio.Rank", pos=.blotter)) 
     suppressWarnings(rm("account.st", "port.st", "stock.str", "stratRank", 
          "initDate", "initEq", 'start_t', 'end_t')) 


     # set initial variables 
     initDate <- "1900-01-01" 
     initEq <- init.equity 
     port.st <- "Rank" 
     account.st <- "Rank" 

     # trade the top "N" ranked symbols 
     N <- top.N 

     # initialize quantstrat objects 
     initPortf(port.st, symbols=symbols, initDate=initDate) 
     initAcct(account.st, portfolios=port.st, initDate=initDate,initEq=initEq) 
     initOrders(portfolio=port.st, initDate=initDate) 

     # initialize a strategy object 
     stratRank <- strategy("Rank") 

     # there are two signals 
     # the first signal is when Rank is less than or equal to N 
     # (i.e. trades the #1 ranked symbol if N=1) 
     stratRank <- add.signal(strategy=stratRank, name="sigThreshold", 
           arguments=list(threshold=N, column="Rank", 
              relationship="lte", cross=FALSE), 
           label="Rank.lte.N") 

     # the second signal is when Rank is greter than or equal to N 
     # (i.e. trades the #1 ranked symbol if N=1) 
     stratRank <- add.signal(strategy=stratRank, name="sigThreshold", 
           arguments=list(threshold=N, column="Rank", 
              relationship="gt", cross=FALSE), 
           label="Rank.gt.N") 

     # add buy rule 
     stratRank <- add.rule(strategy=stratRank, name='ruleSignal', 
          arguments = list(sigcol="Rank.lte.N", sigval=TRUE, 
              orderqty=max.size, ordertype='market', 
              orderside='long', pricemethod='market', 
              replace=FALSE, osFUN=osMaxPos,TxnFees=pennyPerShare), 
          type='enter', path.dep=TRUE) 

     # add exit rule 
     stratRank <- add.rule(strategy = stratRank, name='ruleSignal', 
          arguments = list(sigcol="Rank.gt.N", sigval=TRUE, 
              orderqty='all', ordertype='market', 
              orderside='long', pricemethod='market', 
              replace=FALSE,TxnFees=pennyPerShare), 
          type='exit', path.dep=TRUE) 

     #set max position size and levels 
     for(symbol in symbols){ addPosLimit(port.st, symbol, initDate, max.size, max.levels) } 

     print("setup completed") 

     # apply the strategy to the portfolio 
     start_t <- Sys.time() 
     out <- try(applyStrategy(strategy=stratRank, portfolios=port.st)) 
     end_t <- Sys.time() 
     print(end_t-start_t) 

     # update Portfolio 
     start_t <- Sys.time() 
     updatePortf(Portfolio=port.st, Dates=paste('::', as.Date(Sys.time()), sep='')) 
     end_t <- Sys.time() 
     print("trade blotter portfolio update:") 
     print(end_t - start_t) 

     # update account 
     updateAcct(account.st) 

     # update ending equity 
     updateEndEq(account.st) 

     # get ending equity 
     eq <- getEndEq(account.st, Sys.Date()) + initEq 

     # view order book to confirm trades 
     order.book <- getOrderBook(port.st) 

     # get trade statistics 
     stats <- tradeStats(port.st) 

     # portfolio returns 
     ret1 <- PortfReturns(port.st) 
     ret1$total <- rowSums(ret1, na.rm=TRUE) 

     return(list(end.eq=eq, returns=ret1, book=order.book, stats=stats)) 
    } 



    ##### monthlyAd function ##### 
monthlyAd <- function(x){ 
    # Converts daily data to monthly and returns only the monthly close 
    # Note: only used with Yahoo Finance data so far 
    # Thanks to Joshua Ulrich for the Monthly Ad function 
    # 
    # args: 
    # x = daily price data from Yahoo Finance 
    # 
    # Returns: 
    # xts object with the monthly adjusted close prices 

    sym <- sub("\\..*$", "", names(x)[1]) 
    Cl(to.weekly(x, indexAt = 'lastof', drop.time = TRUE, name = sym)) ##ändrade från to.monthly och till Close from Adjusted 
} 

##### monthlyReturns function ##### 
monthlyReturns <- function(symbols) { 
    # The function takes a character vector of symbols loaded into 
    # the environment and returns an xts object of simple returns 
    # Currently this is only for prepping monthly data 

    # symbols : character vector of symbols 

    ROC(x = monthlyPrices(symbols), n = 1, type = "discrete", na.pad = TRUE) 
} 

    ##### monthlyPrices function ##### 
    monthlyPrices <- function(symbols) { 
     # The function takes a character vector of symbols loaded into 
     # the environment and returns an xts object of Adjusted close prices 
     # Currently this is only for prepping monthly data 

     # symbols   : character vector of symbols 
     # list.sym  : list of symbols with market data 

     list.sym <- list() 
     for(i in 1:length(symbols)) { 
     list.sym[[symbols[i]]] <- get(symbols[i]) 
     } 

     do.call(merge, lapply(list.sym, monthlyAd)) 
    } 







     # RankningFunktioner ------------------------------------------------------ 
     # Functions for ways to rank assets based on rate of change 
     # TODO - add functions to rank based on other factors 

     # The functions defined below depend on functions in the xts and TTR packages 
     # library(TTR) 

     ##### applyRank ##### 
     applyRank <- function(x, rankFun, ...) { 
      # symbols : character vector of symbols 
      # rankFun : function that returns the rank 
      # rankFun should be ave3ROC, weightAve3ROC, strengthROC, strengthAve3ROC, 
      # etfReplayRank, or strengthSMA. 
      # x  : xts object of prices 
      # ...  : arguments to rankFun 

      FUN <- match.fun(rankFun) 
      FUN(x, ...) 
     } 

     ##### symbolRank ##### 
     symbolRank <- function(symbols, rank.obj) { 
      # loop through symbols 
      # convert the market data to monthly periodicity 
      # cbind the appropriate column from rank.obj to the market data 
      # makes the assumption that the order symbols and rank.obj are equal 

      # symbols : character vector of symbols 
      # rank.obj : xts object of ranks of each symbol 

      for(i in 1:length(symbols)) { 
      x <- get(symbols[i]) 
      x <- to.monthly(x,indexAt='lastof',drop.time=TRUE) 
      indexFormat(x) <- '%Y-%m-%d' 
      colnames(x) <- gsub("x", symbols[i], colnames(x)) 
      x <- cbind(x, rank.obj[,i]) 
      assign(symbols[i],x) 
      } 
     } 

     ##### rowRank ##### 
     rowRank <- function(x){ 
      # Computes the rank of an xts object of ranking factors 
      # ranking factors are the factors that are ranked (i.e. asset returns) 
      # 
      # x : xts object of ranking factors 
      # 
      # Returns an xts object with ranks 
      # For ranking asset returns, the asset with the greatest return 
      # receives a rank of 1 

      as.xts(t(apply(-x, 1, rank, na.last = "keep"))) 
     } 

     #Use the supplied TTR::ROC function for a straight ROC computation 

     ##### ave3ROC##### 
     ave3ROC <- function(x, n=c(1, 3, 6)){ 
      # Computes the average rate of change based on averaging 3 periods 
      # 
      # x : xts object of prices 
      # n : vector of periods to use n = (period1, period2, period3) 
      # ave : xts object of asset rate of change by averaging 3 periods 

      roc1 <- ROC(x, n = n[1], type = "discrete") 
      roc2 <- ROC(x, n = n[2], type = "discrete") 
      roc3 <- ROC(x, n = n[3], type = "discrete") 
      ave <- (roc1 + roc2 + roc3)/3 
      rowRank(ave) 
     } 
+0

您正在使用什么版本吸墨纸?这在几天前刚刚改变。 – 2014-11-03 16:05:57

+0

@JoshuaUlrich'packageVersion(“blotter”)0.8.18'最好的问候 – user1665355 2014-11-03 19:06:03

+0

@JoshuaUlrich在'[.xts'(ordersubset,“Order.Status”)中的错误:'0.9'版本的不支持类型。 1644'' – user1665355 2014-11-03 19:14:44

回答

5

我终于有时间一点点地考虑这一点。问题是TxnFees需要是一个字符串,其中包含函数名称而不是实际的函数本身。我将在addOrder的文档中更清楚地说明这一点。

在此期间,您的示例工作对我来说,如果我改变下面几行:

# add buy rule 
stratRank <- add.rule(strategy=stratRank, name='ruleSignal', 
    arguments = list(sigcol="Rank.lte.N", sigval=TRUE, 
        orderqty=max.size, ordertype='market', 
        orderside='long', pricemethod='market', 
        replace=FALSE, osFUN=osMaxPos,TxnFees="pennyPerShare"), 
    type='enter', path.dep=TRUE) 

# add exit rule 
stratRank <- add.rule(strategy = stratRank, name='ruleSignal', 
    arguments = list(sigcol="Rank.gt.N", sigval=TRUE, 
        orderqty='all', ordertype='market', 
        orderside='long', pricemethod='market', 
        replace=FALSE,TxnFees="pennyPerShare"), 
    type='exit', path.dep=TRUE) 
+0

谢谢!我更喜欢deparse(替代品(pennyPerShare))。什么是path.dep真的意味着什么?是否可以设置'pennyPerShare',使其将'Txn.Price'乘以百分比? I.E. 'pennyPerShareMine = function(Txn.Price,...){return(abs(Txn.Price)* -0.012)}'最好的问候 – user1665355 2014-11-06 14:37:31

+0

它应该是可能的。你试过了吗? – 2014-11-06 14:38:31

+0

是的,我试过但没有suceed。我认为如果函数(TxnQty,...) return(abs(TxnQty)* -0.01) }'使用'TxnQty'并且'bt $ trans.st'中的数量列名是'Txn .Qty',我将'pennyPerShare'改成'pennyPerShareMine = function(TxnPrice,...){return(abs(TxnPrice)* -0.01)}'因为'bt $ trans.st'中的列名是'Txn.Price '。虽然没有工作... – user1665355 2014-11-06 14:43:22