【问题标题】:Quanstrat Error in Pos Limit for osMaxPososMaxPos 的 Pos 限制中的 Quantstrat 错误
【发布时间】:2019-12-20 11:27:54
【问题描述】:

我正在创建一个信号,我在 40 天高点买入并在 20 天低点收盘。我还想在策略中添加头寸限制。我希望最大头寸为 500 股,只有 1 个级别。该代码可以正常工作而无需添加位置限制。但是,一旦我添加了限制,我就会收到一个错误。我发现这个链接有点帮助。https://r.789695.n4.nabble.com/Quantstrat-code-works-for-long-position-but-not-short-position-td4715562.html

当我查看 mktdata 时,似乎我在仓位被触发做多时有一些 NA。此列“SPY.Close.fortyDayhigh.twentyDaylow”是做什么用的?

下面是代码。

library(quantstrat)
library(quantmod)
#buy when new 40 day high
#close when new 20 day low
getSymbols(symbols, from = "1990-01-01")

periodHigh = function(x, n){
      rollapplyr(x, width = n, FUN = max, partial = T)
}

periodLow = function(x, n){
      rollapplyr(x, width = n, FUN = min, partial = T)
}
rm.strat(strat)

strat = "learn"

startDate = "1995-01-01"
initEq = 100000

currency("USD")
symbols = c("GLD", "SPY", "VNQ", "SLV", "EEM", "TLT", "AGG", "HYG", "IBB")
symbols = "SPY"

for(symbol in symbols){
      stock(symbol, currency = "USD", multiplier = 1)
}

initPortf(strat, symbols = symbols)
initAcct(strat, portfolios = strat, initEq = initEq)
initOrders(portfolio = strat)
addPosLimit(portfolio = strat, symbol = symbols, maxpos = 500, longlevels = 1 ) #set max pos

strategy(strat, store = T)

add.indicator(strat, name = "periodHigh", arguments = list(x = quote(Cl(mktdata)),
                                                           n = 40),
              label = "fortyDayhigh")
add.indicator(strat, name = "periodLow", arguments = list(x = quote(Cl(mktdata)),
                                                          n = 20),
              label = "twentyDaylow")

add.signal(strat, name = "sigCrossover", arguments = list(columns = c("Close", "fortyDayhigh"),
                                                          relationship = "gte"),
           label = "Close.gt.high")
add.signal(strat, name = "sigCrossover", arguments = list(columns = c("Close", "twentyDaylow"),
                                                          relationship = "lte"),
           label = "Close.lt.low")
add.rule(strat, name = "ruleSignal", arguments = list(sigcol = "Close.gt.high",
                                                      sigval = T,
                                                      ordertype = "market",
                                                      orderqty = 500,
                                                      TxnFees = -2, 
                                                      orderside = "long",
                                                      threshold = NULL,
                                                      osFUN=osMaxPos

                                                      ),
         type = "enter"
)
add.rule(strat, name = "ruleSignal", arguments = list(sigcol = "Close.lt.low",
                                                      sigval = T,
                                                      ordertype = "market",
                                                      orderqty = "all",
                                                      TxnFees = -2,
                                                      orderside ="long",
                                                      threshold = NULL,
                                                      osFUN=osMaxPos

                                                      ),
         type = "exit"
         )
out = applyStrategy(strategy = strat, portfolios = strat)

【问题讨论】:

    标签: r quantmod quantstrat


    【解决方案1】:

    看起来你引用了我几年前写的一些代码。我也许可以帮助你。您收到的错误是什么?

    【讨论】:

      猜你喜欢
      • 2017-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多