0

osMaxPos只是想更好地理解 quantstrat,但是,有两行代码让我感到困惑,我认为它们是拼写错误。

源代码可以在这个链接找到简单地说,两个错字错误是: 1. source code line 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 跟踪器上可能会有这个问题的答案。如果没有,我希望有人可以通过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

    }
}
4

0 回答 0