r - Quantstrat 多种货币。 Blotter::UpdateAcct 中可能有错误吗?

标签 r quantmod quantstrat blotter

一般信息:

R 版本: 3.1.0

记事本: 0.8.19

问题描述:

我正在尝试实现一个使用不同货币的多个投资组合的 Quantstrat 账户。

这是我的基本设置:

  • 1 个欧元账户
  • 1 个美元投资组合

因此,为了使其发挥作用,我必须设置一个汇率,该汇率基于从雅虎检索到的数据。然后我应该运行我的基本策略,转换将在最后一步通过 updateAcct 函数自动完成。

现在问题来了...我认为 updateAcct 函数有一个错误。

我的代码:

initDate="1990-01-01"
from="2007-01-01"
to="2012-12-31"
options(width=70)

options("getSymbols.warning4.0"=FALSE) 
currency(c('USD','EUR'))
exchange_rate("USDEUR", tick_size = 0.01)
USDEUR <- Cl(getSymbols("EUR=X",src="yahoo", auto.assign = FALSE))
Sys.setenv(TZ="UTC") 
#not sure why this might work
.blotter <- new.env()
.strategy <- new.env()

symbols <- c("^IXIC" #Nasdaq
             ) 
if(!"XLB" %in% ls()) {
  suppressMessages(getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE))  
  } 

#need this to remove index call symbol (yahoo.) from string. I.e. get ^IXIC, but named IXIC
symbols<-gsub("\\^", "", symbols)

stock(symbols, currency="USD", multiplier=1)

#trade sizing and initial equity settings
tradeSize <- 10000
initEq <- tradeSize*length(symbols) 
strategy.st <- portfolio.st <- account.st <- "TradeNasdaq100"

#clear old strategies etc.
suppressWarnings(try(rm.strat(strategy.st), silent=TRUE))

#initialize portfolio and account
initPortf(portfolio.st, symbols=symbols, initDate=initDate, currency='USD')
initAcct(account.st, portfolios=portfolio.st, initDate=initDate, currency='EUR',initEq=initEq)
initOrders(portfolio.st, initDate=initDate)
strategy(strategy.st, store=TRUE)

然后我使用一些指标、信号、规则等......

#apply strategy
t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1) 
#set up analytics
updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(account.st,dateRange)

一切正常,直到代码到达最后一行。

最后一行将给出错误消息: isTRUE(invert) 中的错误:找不到对象“invert”

可能的错误: 所以我决定检查 updateAcct 函数,尝试在这里进行一些调试...我很确定代码中有错误。第 63 行中的 if 子句查询 isTRUE(invert),但仅当它确实为 true 时才会创建 invert(请参阅 else 子句第 46 行)。但 invert 没有初始化,因此如果它实际上是 false,代码将会失败。

这是源代码记事簿(原始)

function (name = "default", Dates = NULL) 
{
    Account <- getAccount(name)
    if (!is.null(attr(Account, "currency"))) {
        a.ccy.str <- attr(Account, "currency")
    }
    Portfolios = names(Account$portfolios)
    if (is.null(Dates)) 
        Dates <- unique(do.call(c, c(lapply(Portfolios, function(x) index(.getPortfolio(x)$summary)), 
            use.names = FALSE, recursive = FALSE)))[-1]
    if (!length(Dates)) 
        return(name)
    if (last(index(Account$summary)) > .parseISO8601(Dates)$first.time) {
        whichi <- first(Account$summary[paste(.parseISO8601(Dates)$first.time, 
            "::", sep = ""), which.i = TRUE])
        if (!is.null(whichi)) 
            whichi = whichi - 1
        if (whichi < 1) 
            whichi = 1
        Account$summary = Account$summary[1:whichi, ]
    }
    for (pname in Portfolios) {
        Portfolio = .getPortfolio(pname)
        if (!is.null(attr(Portfolio, "currency"))) {
            p.ccy.str <- attr(Portfolio, "currency")
        }
        psummary = Portfolio$summary[Dates]
        if (a.ccy.str != p.ccy.str) {
            CcyMult <- NA
            port_currency <- try(getInstrument(p.ccy.str), silent = TRUE)
            if (inherits(port_currency, "try-error") | !is.instrument(port_currency)) {
                warning("Currency", p.ccy.str, " not found, using currency multiplier of 1")
                CcyMult <- 1
            }
            else {
                FXrate.str <- paste(p.ccy.str, a.ccy.str, sep = "")
                FXrate <- try(get(FXrate.str), silent = TRUE)
                if (inherits(FXrate, "try-error")) {
                  FXrate.str <- paste(a.ccy.str, p.ccy.str, sep = "")
                  FXrate <- try(get(FXrate.str), silent = TRUE)
                  if (inherits(FXrate, "try-error")) {
                    warning("Exchange Rate", FXrate.str, " not found for symbol,',Symbol,' using currency multiplier of 1")
                    CcyMult <- 1
                  }
                  else {
                    invert = TRUE
                  }
                }
            }
            if (is.na(CcyMult) && !is.na(FXrate)) {
                if (inherits(FXrate, "xts")) {
                  CcyMult <- FXrate[Dates]
                  CcyMult <- na.locf(merge(CcyMult, index(psummary)))
                  CcyMult <- drop(CcyMult[index(psummary)])
                }
                else {
                  CcyMult <- as.numeric(FXrate)
                }
            }
            else {
                CcyMult <- 1
            }
            if (isTRUE(invert)) {
                CcyMult <- 1/CcyMult
            }
            psummary <- psummary * CcyMult
        }
        Account$portfolios[[pname]] = rbind(Account$portfolios[[pname]], 
            psummary)
    }
    summary = NULL
    table = .getByPortf(Account, "Net.Trading.PL", Dates)
    obsLength = length(index(table))
    obsDates = index(table)
    if (obsLength > 1) 
        on = periodicity(table)$units
    else on = "none"
    Attributes = c("Additions", "Withdrawals", "Realized.PL", 
        "Unrealized.PL", "Interest", "Gross.Trading.PL", "Txn.Fees", 
        "Net.Trading.PL", "Advisory.Fees", "Net.Performance", 
        "End.Eq")
    for (Attribute in Attributes) {
        switch(Attribute, Realized.PL = , Unrealized.PL = , Gross.Trading.PL = , 
            Txn.Fees = , Net.Trading.PL = {
                table = .getByPortf(Account, Attribute, Dates)
                result = xts(rowSums(table, na.rm = TRUE), order.by = index(table))
            }, Additions = {
                result = if (on == "none") as.xts(sum(Account$Additions[paste("::", 
                  obsDates, sep = "")]), order.by = index(table)) else {
                  if (length(Account$Additions[obsDates]) > 0) period.apply(Account$Additions[obsDates], 
                    endpoints(Account$Additions[obsDates], on = on), 
                    sum) else xts(rep(0, obsLength), order.by = obsDates)
                }
            }, Withdrawals = {
                result = if (on == "none") as.xts(sum(Account$Withdrawals[paste("::", 
                  obsDates, sep = "")]), order.by = index(table)) else {
                  if (length(Account$Additions[obsDates]) > 0) period.apply(Account$Withdrawals[obsDates], 
                    endpoints(Account$Withdrawals[obsDates], 
                      on = periodicity(table)$units), sum) else xts(rep(0, 
                    obsLength), order.by = obsDates)
                }
            }, Interest = {
                result = if (on == "none") as.xts(sum(Account$Interest[paste("::", 
                  obsDates, sep = "")]), , order.by = index(table)) else {
                  if (length(Account$Additions[obsDates]) > 0) period.apply(Account$Interest[obsDates], 
                    endpoints(Account$Interest[obsDates], on = periodicity(table)$units), 
                    sum) else xts(rep(0, obsLength), order.by = obsDates)
                }
            }, Advisory.Fees = , Net.Performance = , End.Eq = {
                result = xts(rep(0, obsLength), order.by = obsDates)
            })
        colnames(result) = Attribute
        if (is.null(summary)) {
            summary = result
        }
        else {
            summary = cbind(summary, result)
        }
    }
    summary[is.na(summary)] <- 0
    Account$summary <- rbind(Account$summary, summary)
    assign(paste("account", name, sep = "."), Account, envir = .blotter)
    return(name)
}

我认为它应该是这样的(片段第 28-50 行)...

if (a.ccy.str != p.ccy.str) {
  CcyMult <- NA
  port_currency <- try(getInstrument(p.ccy.str), silent = TRUE)
  if (inherits(port_currency, "try-error") | !is.instrument(port_currency)) {
    warning("Currency", p.ccy.str, " not found, using currency multiplier of 1")
    CcyMult <- 1
  }
  else {
    FXrate.str <- paste(p.ccy.str, a.ccy.str, sep = "")
    FXrate <- try(get(FXrate.str), silent = TRUE)
    invert=FALSE #THIS IS THE LINE NEEDED FOR FIXING
    if (inherits(FXrate, "try-error")) {
      FXrate.str <- paste(a.ccy.str, p.ccy.str, sep = "")
      FXrate <- try(get(FXrate.str), silent = TRUE)
      if (inherits(FXrate, "try-error")) {
        warning("Exchange Rate", FXrate.str, " not found for symbol,',Symbol,' using currency multiplier of 1")
        CcyMult <- 1
      }
      else {
        invert = TRUE
      }
    }
  }

TL;DR

我认为 blotter:updateAcct 中存在错误,当货币转换不需要反转汇率时会发生该错误...

问题: 我说得对吗这是一个错误?或者我错过了什么?

附注:

我通常会将其作为错误提交,但是 A) 我不知道如何向作者提交错误 B) 我仍然是 quantstrat、blotter 和 Co. 的新手,我认为其他人应该检查一下这个好吧(作者也经常在这里闲逛)...

最佳答案

感谢您提供可重现的示例。为了将来引用,最好提供一个 diff,而不是 20-30 行代码。我花了一段时间才注意到您刚刚添加了一行。

> svn diff blotter/R/updateAcct.R 
Index: blotter/R/updateAcct.R
===================================================================
--- blotter/R/updateAcct.R  (revision 1681)
+++ blotter/R/updateAcct.R  (working copy)
@@ -51,6 +51,7 @@
                 FXrate.str<-paste(p.ccy.str,a.ccy.str,sep='') # currency quote convention is EURUSD which reads as "USD per EUR"
                 FXrate<-try(get(FXrate.str), silent=TRUE)
                 #TODO FIXME: this uses convention to sort out the rate, we should check $currency and $counter_currency and make sure directionality is correct 
+                invert=FALSE
                 if(inherits(FXrate,"try-error")){
                     FXrate.str<-paste(a.ccy.str,p.ccy.str,sep='')
                     FXrate<-try(get(FXrate.str), silent=TRUE)

已修复 revision 1682 。感谢您的报告!

关于r - Quantstrat 多种货币。 Blotter::UpdateAcct 中可能有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367523/

相关文章:

r - dplyr::filter() 没有产生预期的输出

r - 在 R : Column. 标签中生成 HTML 表,或者在 stargazer 中的数据框表上生成 dep.var.caption

r - 使映射到某个 ID 的变量值统一

r - 使用日期结构略有不同的 xts

r - quantstrat:在开盘时买入下一根柱线

r - 使用管道更改特定列名

r - 在R库quantmod中,getQuote有新问题吗?

从环境中删除 NULL 对象

r - 在 quantstrat 中生成不同周期的指标

r - 使用 quantstrat 执行多时间框架策略的正确方法是什么?