如何在Quantstrat中触发高/低价格的追踪止损?

时间:2019-05-16 15:28:08

标签: r quantstrat

我正在使用OHLC数据集在Quantstrat中设置止损功能,而我无法正确获得追踪止损。在高/低价格上会按预期触发正常的无追踪止损,并在阈值价格上进行填充。

另一方面,追踪停止,似乎是根据收盘价而不是高/低价格进行评估。

这是一个非常基本的示例:

install.packages("devtools") # if not installed
install.packages("FinancialInstrument") #if not installed
install.packages("PerformanceAnalytics") #if not installed

# next install blotter from GitHub
devtools::install_github("braverock/blotter")
# next install quantstrat from GitHub
devtools::install_github("braverock/quantstrat")

require(quantstrat)
require(blotter)
require(FinancialInstrument)

#testdata
data(ttrc)
ttrc[,"Open"] <- 100
ttrc[,"High"] <- 100
ttrc[,"Low"] <- 100
ttrc[,"Close"] <- 100
ttrc$Close[1] <- 105
ttrc$Low[5] <- 75

#stop loss / trailing stop on/off
stoploss <- FALSE
trailing <- TRUE

#xts time series
ttrc$Date <- as.POSIXct(ttrc$Date, format = "%Y-%m-%d")
data <- read.zoo(ttrc)
XYZ <- as.xts(data)

#settings
tax <- 0.075
tradesize <- 1
initeq <- 1

#dates
initdate <- "1970-01-01"
from <- "1970-01-01"
to <- "2099-12-31"

# Set currency to USD 
currency("USD")

# Initialize coin & set trade size
stock("XYZ",currency="USD")

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

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

# Initialize portfolio, account, orders and strategy
initPortf(portfolio.st, symbols = "XYZ", initDate = initdate, currency = "USD")
initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store = TRUE)

# Set transaction cost
txnFUN <- function(TxnQty, TxnPrice, Symbol, pct = tax) {
  fees <- abs(TxnQty) * TxnPrice * pct/100
  # Fees are a negative deduction for the trade:
  if (fees > 0) fees <- -fees

  fees
}

# signal: buy if price is 105 
my_signal <- function(close) {
  s <- ifelse(close == 105,1,0)
  s <- as.xts(s)
  names(s) <- "s"
  return(s)
}

#indicator
add.indicator(strategy.st, name = my_signal, arguments = list(close = Cl(data)), label = "signal")

#signals
add.signal(strategy.st, name = "sigThreshold", arguments = list(column = "s.signal", threshold = 1, relationship = "eq", cross = FALSE), label = "enter_buy")

#position limit
addPosLimit(portfolio.st, "XYZ", maxpos = tradesize, timestamp = start(XYZ)-1)

# Enter buy
add.rule("test", name = "ruleSignal", arguments=list(sigcol = "enter_buy", sigval = TRUE, orderqty = tradesize, osFUN = osMaxPos, TxnFees="txnFUN",  ordertype = "market", orderside = "long", orderset = "ocolong", replace = FALSE, prefer = "Close"), type = "enter", label= "EnterLong")

止损-这是止损和尾随止损。

# Stoploss long
add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "enter_buy" ,
                          sigval = TRUE,
                          replace = FALSE,
                          orderside = "long",
                          TxnFees="txnFUN",
                          ordertype = "stoplimit",
                          tmult = TRUE,
                          threshold = 0.01,
                          orderqty = "all",
                          orderset = "ocolong"),
         type = "chain",
         parent = "EnterLong",
         label = "StopLossLONG",
         enabled = stoploss)

# Trailing stop long
add.rule(strategy = strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "enter_buy", 
                          sigval = TRUE, 
                          replace = FALSE, 
                          orderside = "long",
                          TxnFees="txnFUN",
                          ordertype = "stoptrailing", 
                          tmult = TRUE, 
                          threshold = 0.01,
                          orderqty = "all", 
                          orderset = "ocolong"), 
         type = "chain", 
         parent = "EnterLong", 
         label = "StopTrailingLong", 
         enabled = trailing)

应用策略并进行交易:

# Apply strategy
out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)

# Update portfolio
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

# Update account
updateAcct(account.st, daterange)
updateEndEq(account.st)

尾随止损未触发。

现在,如果我将收盘价更改为与低价相同:

ttrc$Close[5] <- 75

尾随止损被触发。

如何在高/低价格上触发追踪止损,并在阈值水平上执行定单?

0 个答案:

没有答案