1

在记事本中编写策略时,我遇到了一个问题,即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. 如果可能的话,我怎样才能避免这种情况?
4

1 回答 1

1

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

其次,您使用update*单个日期调用函数,这(通常)没有意义。这会导致该书仅标记为该单个日期,这并不是特别有用。使用像"2008-04-30/2008-05-30".

非零 Unrealized.PL 是因为您的平仓交易发生在2008-05-30年底,这意味着您有当天的未实现盈亏。

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

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
于 2015-07-26T20:09:12.883 回答