我调查osMaxPos
只是试图更好地理解quantstrat,然而,有两行代码混淆了我,我认为它们是拼写错误。
源代码可以在this link找到。简而言之,两个拼写错误是:1。源代码行212 if(orderqty+pos>PosLimit[,"MaxPos"]) orderqty <- PosLimit[,"MinPos"]-pos
,但我认为作者希望它是if (orderqty+pos < PosLimit[, "MinPos"]) orderqty <- PosLimit[,"MinPos"]-pos
; 2.源代码行226是orderqty <- pos
,但我认为代码是orderqty <- -pos
在不久的将来R-Forge tracker可能会有这个问题的答案。如果没有,我希望有人可以通过stackoverflow帮助我。我试图通过以下简单的例子来证明第二个拼写错误。
此示例使用部分源代码并运行一些虚假数据来演示第二个拼写错误确实存在。但我不熟悉交易和R,所以我可能完全错了。有人可以帮我看看,谢谢。
# #### run a simplifed strategy to get values for pos and PosLimit
# portfolio <- "test"
# symbol <- "JQR_DAY"
# timestamp <- as.Date("2013-09-09")
# # get position quantity of timestamp
# pos <- getPosQty(portfolio, symbol, timestamp)
# # get positionlimits on timestamp
# PosLimit <- getPosLimit(portfolio, symbol, timestamp)
#### to make this example reproducible, I created pos and PosLimit values as following:
pos <- -4000
PosLimit <- xts(t(c(4000, 4, -4000, 4)), order.by = as.Date("2000-09-09"))
colnames(PosLimit) <- c("MaxPos", "LongLevels", "MinPos", "ShortLevels")
orderside <- "short" # existing positions are shorting
ruletype <- "risk" # ruletype is "risk"
# senario1: flatten all short positions
# orderqty <- "all"
# in the end, orderqty will be 4000 to be able to close all short positions
# senario2: orderqty is not enough to close all short positions
# orderqty <- 2000 # orderqty will be finally set as 2000
# senario3: orderqty is more than enough to close all short positions
orderqty <- 5000 # orderqty should be finally set as 4000,
# but it turns out to be -4000
# if using orderqty <- -pos, orderqty will be 4000
####################################################################
#### simplified code to perform buy_cover_short
####################################################################
# if orderside is short and orderqty > 0 to exit shorting
# if orderqty == "all", orderqty > 0 is true
if (orderqty > 0 & orderside == "short") {
# orderside == "short" suggests pos < 0
# if ruletype = risk
if (ruletype == "risk") {
# if orderqty = all
if (orderqty == "all")
# return -pos
orderqty <- (-1 * pos)
# meaning exit all shorting positions
# if orderqty != all, return orderqty itself
# only exit order quantity of short positions
else orderqty <- orderqty
}
# if orderqty > 0 but orderqty < |pos|
if ((orderqty + pos) <= 0) {
# return orderqty itself
# meaning leaving some shorting position alive
orderqty <- orderqty
}
# if orderqty + pos > 0
# meaning orderqty > -pos
# meaning orderqty > |pos|
else {
# it is a typo error ???????????????????
#flatten position, don't cross through zero
orderqty <- pos # orderqty <- -pos
}
}