r-试图理解吸墨纸帐户Unrealized.PL和End.Eq计算



在blotter中编程策略时,我遇到了交易后的End.Eq与手动计算的预期结果不匹配的问题。所以我写了一些简单的R代码来更好地理解吸墨纸是如何工作的。这里的结果也与我预期的不同。

也许有一个更有洞察力的人可以帮我解释一下。

# R version 3.2.1 (2015-06-18)
# Platform: x86_64-pc-linux-gnu (64-bit)
# blotter: 0.9.1666
require(blotter)
Sys.setenv(TZ="UTC")
rm(list =ls(envir=.blotter), envir=.blotter) 
initDate <- '2008-01-01'
initEq <- 1000
currency("USD")
stock("MDY", currency = "USD", multiplier = 1)
getSymbols("MDY", from='2008-01-01', to='2008-06-30', index.class="POSIXct", adjust=T)
b.strategy <- "b.test"
initPortf(name = b.strategy, symbols = "MDY", initDate = initDate)
initAcct(name = b.strategy, portfolios = b.strategy, initDate = initDate, initEq = initEq)
addTxn(b.strategy, Symbol = "MDY", TxnDate = "2008-04-30",
   TxnPrice = as.numeric(Cl(MDY["2008-04-30"])), TxnQty = 1, TxnFees = 0)
updatePortf(b.strategy, Dates = "2008-04-30")
updateAcct(b.strategy, Dates = "2008-04-30")
updateEndEq(b.strategy, Dates = "2008-04-30")
addTxn(b.strategy, Symbol = "MDY", TxnDate = "2008-05-30",
   TxnPrice = as.numeric(Cl(MDY["2008-05-30"])), TxnQty = -1, TxnFees = 0)
updatePortf(b.strategy, Dates = "2008-05-30")
updateAcct(b.strategy, Dates = "2008-05-30")
updateEndEq(b.strategy, Dates = "2008-05-30")
perTradeStats(b.strategy, "MDY")
tradeStats(b.strategy)
getAccount(b.strategy)$summary

上面的代码用1000美元的现金初始化一个投资组合。我以"2008-04-30"的收盘价买了1个"MDY"。然后我在一个月后以"2008-05-30"的收盘价平仓。

[1] "2008-04-30 00:00:00 MDY 1 @ 151.920844224256"
[1] "2008-05-30 00:00:00 MDY -1 @ 160.178061444191"

由于投资组合不再包含未平仓头寸,我预计End.Eq的收益为1000+8.257217=1008.257217,但由于Unrealized.PL = -6.72,我看到的结果要低得多。

但这是从哪里来的?所有仓位都已平仓,所以我预计Unrealized.PL = 0。尽管交易似乎仍然给了我Net.Trading.PL = 8.257217的预期结果。

> perTradeStats(b.strategy, "MDY")
       Start        End Init.Pos Max.Pos Num.Txns Max.Notional.Cost Net.Trading.PL MAE      MFE
1 2008-04-30 2008-05-30        1       1        2          151.9208       8.257217   0 8.257217
  Pct.Net.Trading.PL Pct.MAE   Pct.MFE tick.Net.Trading.PL tick.MAE tick.MFE
1          0.0543521       0 0.0543521            825.7217        0 825.7217

查看投资组合账户,我发现Unrealized.PL = -6.72,我无法解释,但这显然也导致了意外的End.Eq结果。

> getAccount(b.strategy)$summary
           Additions Withdrawals Realized.PL Unrealized.PL Interest          Gross.Trading.PL Txn.Fees Net.Trading.PL
2008-01-01         0           0    0.000000          0.00        0         0.000000        0       0.000000
2008-04-30         0           0    0.000000          0.00        0         0.000000        0       0.000000
2008-05-30         0           0    8.257217         -6.72        0         1.535756        0       1.535756
           Advisory.Fees Net.Performance   End.Eq
2008-01-01             0        0.000000 1000.000
2008-04-30             0        0.000000 1000.000
2008-05-30             0        1.535756 1001.536

总之,我的两个问题是:

  1. 寻求解释Unrealized.PL是如何产生的
  2. 如果可能的话,我该如何避免

首先,您应该只在需要标记书本时调用update*函数。如果你实际上不需要使用它们计算的一些值,那么在每次交易后调用它们是低效的。

其次,您使用单个日期调用update*函数,这(通常(没有意义。这导致这本书只标记了一个日期,这并没有特别的帮助。使用"2008-04-30/2008-05-30"这样的日期范围会更有意义。

非零的Unrealized.PL是因为您的平仓交易发生在2008-05-30的,这意味着您有未实现的p&L代表那一天。

下面代码的修改版本应该会给出您期望的结果。

require(blotter)
Sys.setenv(TZ="UTC")
rm(list =ls(envir=.blotter), envir=.blotter) 
initDate <- '2008-01-01'
initEq <- 1000
currency("USD")
stock("MDY", currency = "USD", multiplier = 1)
getSymbols("MDY", from='2008-01-01', to='2008-06-30', index.class="POSIXct", adjust=T)
b.strategy <- "b.test"
initPortf(name = b.strategy, symbols = "MDY", initDate = initDate)
initAcct(name = b.strategy, portfolios = b.strategy, initDate = initDate, initEq = initEq)
addTxn(b.strategy, Symbol = "MDY", TxnDate = "2008-04-30",
  TxnPrice = as.numeric(Cl(MDY["2008-04-30"])), TxnQty = 1, TxnFees = 0)
addTxn(b.strategy, Symbol = "MDY", TxnDate = "2008-05-30",
  TxnPrice = as.numeric(Cl(MDY["2008-05-30"])), TxnQty = -1, TxnFees = 0)
updatePortf(b.strategy, Dates = "2008-04-30/2008-05-30")
updateAcct(b.strategy, Dates = "2008-04-30/2008-05-30")
updateEndEq(b.strategy, Dates = "2008-04-30/2008-05-30")
perTradeStats(b.strategy, "MDY")
tradeStats(b.strategy)
getAccount(b.strategy)$summary
getPortfolio(b.strategy)$summary

相关内容

  • 没有找到相关文章