试图了解 blotter 帐户 Unrealized.PL 和 End.Eq 计算
Trying to understand blotter account Unrealized.PL and End.Eq calculation
在 blotter 中编写策略时,我遇到了交易后的 End.Eq
与手动计算的预期结果不匹配的问题。所以我写了一些简单的 R 代码来更好地理解 blotter 的工作原理。同样这里的结果与我预期的不同。
也许有更深入了解的人可以帮我解释一下。
# 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
总而言之,我的两个问题是:
- 寻求解释 Unrealized.PL 是如何产生的?
- 如果可能的话,我怎样才能避免这种情况?
首先,您应该只在需要给书做标记时才调用 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
在 blotter 中编写策略时,我遇到了交易后的 End.Eq
与手动计算的预期结果不匹配的问题。所以我写了一些简单的 R 代码来更好地理解 blotter 的工作原理。同样这里的结果与我预期的不同。
也许有更深入了解的人可以帮我解释一下。
# 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
总而言之,我的两个问题是:
- 寻求解释 Unrealized.PL 是如何产生的?
- 如果可能的话,我怎样才能避免这种情况?
首先,您应该只在需要给书做标记时才调用 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