Quantstrat:如何在 add.rule 上应用 allowMagicalThinking?

我知道有人问过这个问题 herehere,但是 allowMagicalThinking=TRUE 在仅应用于一个规则时不起作用。

我想对信号应用 allowMagicalThinking 以在信号的“高位”退出同一天执行。仅在退出规则上。

library(quantstrat)

# read indicators data
AA <- read.zoo(header = TRUE,as.is = TRUE,index.column = 1,format = "%m/%d/%y",text = "
        Date   Open   High    Low  Close    Volume Adj.Close     enterLong exitLong
1   12/21/15 201.41 201.88 200.09 201.67  99094300    197.43             0            0
2   12/22/15 202.72 203.85 201.55 203.50 111026200    199.22             0            0
3   12/23/15 204.69 206.07 204.58 206.02 110987200    201.69             0            0
4   12/24/15 205.72 206.33 205.42 205.68  48539600    201.36             0            0
5   12/28/15 204.86 205.26 203.94 205.21  65899900    200.90             0            0
6   12/29/15 206.51 207.79 206.47 207.40  92640700    203.04             0            0
7   12/30/15 207.11 207.21 205.76 205.93  63317700    201.60             0            0
8   12/31/15 205.13 205.89 203.87 203.87 102929500    199.58             0            0
9     1/4/16 200.49 201.03 198.59 201.02 222353500    196.79             1            0
10    1/5/16 201.40 201.90 200.05 201.36 110845800    197.13             0            0
11    1/6/16 198.34 200.06 197.60 198.82 152112600    194.64             0            0
12    1/7/16 195.33 197.44 193.59 194.05 213436100    189.97             0            1
13    1/8/16 195.19 195.85 191.58 191.92 209817200    187.89             0            0
14   1/11/16 193.01 193.41 189.82 192.11 187941300    188.07             0            0
15   1/12/16 193.82 194.55 191.14 193.66 172330500    189.59             0            0
")
AA <- as.xts(AA)

# Set the timezone to UTC
Sys.setenv(TZ = "UTC")

# Set the currency to USD 
currency("USD")
stock("AA",currency = "USD")

# Define your trade size and initial equity
tradesize <- 100000
initeq <- 100000

# Define the names of your strategy,portfolio and account
strategy.st <- "firststrat"
portfolio.st <- "firststrat"
account.st <- "firststrat"

# Remove the existing strategy if it exists
rm.strat(strategy.st)

# initialize the portfolio
initPortf(portfolio.st,symbols = "AA")

# initialize the account
initacct(account.st,portfolios = portfolio.st,initEq = initeq)

# initialize the orders
initOrders(portfolio.st)

# set position limits
addPosLimit(portfolio.st,"AA",start(AA),100)

# store the strategy
strategy(strategy.st,store = TRUE)

add.signal(strategy.st,name = "sigThreshold",arguments = list(column = "enterLong",threshold = 1,relationship = "eq",cross = FALSE),label = "thresholdentry")

add.signal(strategy.st,arguments = list(column = "exitLong",label = "thresholdexit")


# add Rules
add.rule(strategy.st,name = "ruleSignal",arguments = list(sigcol = "thresholdentry",sigval = TRUE,ordertype = "market",orderside = "long",orderqty = 100,replace = FALSE,prefer = "Open",osFUN = osMaxPos,tradeSize = tradesize,maxSize = tradesize),type = "enter")

add.rule(strategy.st,arguments = list(sigcol = "thresholdexit",orderqty = "all",prefer = "High",allowMagicalThinking = TRUE),type = "exit")


applyStrategy(strategy.st,portfolio.st)
# actual result
# [1] "2016-01-05 00:00:00 AA 100 @ 201.4"
# [1] "2016-01-08 00:00:00 AA -100 @ 195.85"

预期结果是

2016-01-05 AA 100  # enter one day after the signal (default)
2016-01-07 AA -100 # exit the same day of the signal on the "High" (with allowMagicalThinking=TRUE)

如果将 allowMagicalThinking = TRUE 应用于策略,结果为:

applyStrategy(strategy.st,portfolio.st,allowMagicalThinking = TRUE)
[1] "2016-01-04 00:00:00 AA 100 @ 200.49"
[1] "2016-01-07 00:00:00 AA -100 @ 195.33"
Scorpio_Huang 回答:Quantstrat:如何在 add.rule 上应用 allowMagicalThinking?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/1224672.html

大家都在问