1

这是关于R包amzn_testdemo中交易实现PL的问题。blotter这些交易是一系列 7 笔交易,在日内开仓和平仓。调用getTxns('amzn_port', 'amzn')返回

                    Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL
2010-01-14 00:00:00       0      0.00        0         0         0.00                   0
2010-01-14 10:18:50    -400    127.49        0    -50996       127.49               50996
2010-01-14 10:18:53     400    127.49        0     50996       127.49                   0
...

为什么Net.Txn.Realized.PL开仓交易非零,平仓交易为零?对于每日交易,在交易结束当天实现的损益将非零。

blotter在 64 位 Windows 上运行 0.9.1666。

谢谢你的耐心。

4

1 回答 1

0

这看起来像一个错误,addTxns因为如果您addTxn调用amzn.trades.

require(blotter)
# load the example data
data("amzn")
currency("USD")
stock("amzn",currency="USD",multiplier=1)
# Initialize the Portfolio
initPortf("amzn_port",symbols="amzn",initDate="2010-01-14")
initAcct("amzn_acct",portfolios="amzn_port",initDate="2010-01-14", initEq=10000)
# Add the transactions to the portfolio
for (i in 1:nrow(amzn.trades)) {
  x <- amzn.trades[i,]
  addTxn("amzn_port", "amzn", index(x), coredata(x$TxnQty), coredata(x$TxnPrice))
}
# update the portfolio`
updatePortf("amzn_port",Dates="2010-01-14")
getTxns("amzn_port", "amzn")

发现/修复错误后,我将进行调查并报告。


这现在在修订版 1693中得到修复。这是补丁:

--- pkg/blotter/R/addTxn.R  2015/04/15 02:33:20 1685
+++ pkg/blotter/R/addTxn.R  2015/08/09 16:29:59 1693
@@ -246,9 +246,11 @@
     NewTxns$Pos.Qty <- cumsum(initPosQty + NewTxns$Txn.Qty)
     # only pass non-zero initial position qty and average cost
     NewTxns$Pos.Avg.Cost <- .calcPosAvgCost_C(initPosQty[1], initPosAvgCost[1], NewTxns$Txn.Value, NewTxns$Pos.Qty, ConMult)
-    # need lagged position average cost
+    # need lagged position average cost and quantity
     lagPosAvgCost <- c(initPosAvgCost[1], NewTxns$Pos.Avg.Cost[-nrow(NewTxns)])
+    lagPosQty <- c(initPosQty[1], NewTxns$Pos.Qty[-nrow(NewTxns)])
     NewTxns$Gross.Txn.Realized.PL <- NewTxns$Txn.Qty * ConMult * (lagPosAvgCost - NewTxns$Txn.Avg.Cost)
+    NewTxns$Gross.Txn.Realized.PL[abs(lagPosQty) < abs(NewTxns$Pos.Qty) | lagPosQty == 0] <- 0
     NewTxns$Net.Txn.Realized.PL <- NewTxns$Gross.Txn.Realized.PL - NewTxns$Txn.Fees
     NewTxns$Con.Mult <- ConMult
于 2015-08-09T01:00:01.330 回答