Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 创建真/假信号和相关函数_R_Quantstrat - Fatal编程技术网

R 创建真/假信号和相关函数

R 创建真/假信号和相关函数,r,quantstrat,R,Quantstrat,以下quantstrat系统产生以下错误: Error in `colnames<-`(`*tmp*`, value = "hardStop") : attempt to set 'colnames' on an object with less than two dimensions 这是函数的错误 hardStop <- function(strategy, portfolio, mktdata, sigcol) { if (sigcol==TRUE) { s

以下quantstrat系统产生以下错误:

Error in `colnames<-`(`*tmp*`, value = "hardStop") : 
  attempt to set 'colnames' on an object with less than two dimensions
这是函数的错误

hardStop <- function(strategy, portfolio, mktdata, sigcol) {
  if (sigcol==TRUE) {
    stopHere <- mktdata$loss.stopLimit
  }
  else {
    stopHere <- NA
  }
  stopHere <- na.locf(stopHere)
  out <- stopHere
  colnames(out) <- "hardStop"
  return(out)

hardStop为此,我在函数中运行了一个函数。这样我就不必处理将列数据带回另一个函数的问题。虽然我仍然觉得粘贴()或grep()应该能起作用

stopLimit <- function(HLC, n, maType, pctATR) {
  ATR <- ATR(HLC, n=n, maType=maType)
  ATR <- ATR$atr
  close <- Cl(HLC)
  atrStopProfit <- close+(ATR*300*pctATR)
  atrStopLoss <- close-(ATR*100*pctATR)
  atrStop <- cbind(atrStopProfit, atrStopLoss)
  colnames(atrStop) <- c("profit", "loss")
  return(atrStop)
}

hardStop <- function(HLC, m, n, maType, pctATR) {
  dayHigh <- Hi(HLC)[,1]
  runHigh <- periodHigh(HLC, m)
  runHigh <- replace(runHigh, is.na(runHigh), 0)
  potsLoss <- stopLimit(HLC, n, maType, pctATR)$loss
  potsLoss <- ifelse(dayHigh > runHigh, potsLoss, NA)
  potsLoss <- na.locf(potsLoss)
  out <- potsLoss
  potsWin <- stopLimit(HLC, n, maType, pctATR)$profit
  potsWin <- ifelse(dayHigh > runHigh, potsWin, NA)
  print(potsWin <- na.locf(potsWin))
  pots <- cbind(potsLoss, potsWin)
  colnames(pots) <- c("potsloss", "potswin")
  return(pots)
}

add.indicator(strategy.st, name="hardStop",
              arguments=list(HLC=quote(HLC(mktdata)), m=daysHigh,
                             n=period, wilder=TRUE, pctATR=pctATR),
              label="potsSpot")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("Close", "potswin.potsSpot"), relationship="lt", cross=FALSE),
           label="sellTriggerProfit")

stopLimit请提供一个示例,并注意您正在使用的库。如前所述,这是不可能回答的。谢谢你,亚历克斯。这是使用quantstrat。可复制代码的链接是:eliassimpson.com/investment/wp-content/uploads/system_7.r几乎没有人,包括我在内,会去另一个网站寻找可复制的示例。请在这里提供所述示例。当然——现在将其包含在原始问题中。我不确定这是一个编程问题。似乎你希望有人为你做研究,而不是你有一个特定的编程问题。你能澄清/简化你的问题吗?
require(quantstrat)

initDate="1990-01-01"
from="2011-07-07"
to="2015-12-12"
options(width=70)

options("getSymbols.warning4.0"=FALSE)
rm(list=ls(.blotter), envir=.blotter)

currency('USD')
Sys.setenv(TZ="UTC")

symbols <- "SPY"

suppressMessages(getSymbols(symbols, from=from, to=to, src="yahoo", adjust=TRUE))
stock(symbols, currency="USD", multiplier=1)

#trade sizing and initial equity settings
tradeSize <- 100000
initEq <- tradeSize*length(symbols)

strategy.st <- portfolio.st <- account.st <- "NewerTry"
rm.strat(portfolio.st)
rm.strat(strategy.st)
initPortf(portfolio.st, symbols=symbols, 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)

#parameters
daysHigh <- 20

pctATR <- .01
period <- 14

#create functions
periodHigh <- function(HLC, n) {
  high <- (runMax(Hi(HLC), n=n))
  out <- lag(high, 1)
  colnames(out) <- "periodHighest"
  return(out)
}

stopLimit <- function(HLC, n, maType, pctATR) {
  ATR <- ATR(HLC, n=n, maType=maType)
  ATR <- ATR$atr
  close <- Cl(HLC)
  atrStopProfit <- close+(ATR*300*pctATR)
  atrStopLoss <- close-(ATR*100*pctATR)
  atrStop <- cbind(atrStopProfit, atrStopLoss)
  colnames(atrStop) <- c("profit", "loss")
  return(atrStop)
}

hardStop <- function(strategy, portfolio, mktdata, sigcol) {
  if (sigcol==TRUE) {
    stopHere <- mktdata$loss.stopLimit
  }
  else {
    stopHere <- NA
  }
  stopHere <- na.locf(stopHere)
  out <- stopHere
  colnames(out) <- "hardStop"
  return(out)
}

#indicators and signals
add.indicator(strategy.st, name="periodHigh",
              arguments=list(HLC=quote(HLC(mktdata)), n=daysHigh),
              label="periodHighest")

add.indicator(strategy.st, name="stopLimit",
              arguments=list(HLC=quote(HLC(mktdata)), n=period, wilder=TRUE, pctATR=pctATR),
              label="stopLimit")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("High", "periodHighest"), relationship="gt", cross=TRUE),
           label="buyTrigger")

applyIndicatorSignals(strategy=strategy.st, portfolio=portfolio.st, mktdata, sigcol="buyTrigger")

add.indicator(strategy.st, name="hardStop",
              arguments=list(strategy=strategy.st, portfolio=portfolio.st, mktdata=quote(HLC(mktdata)),
                             sigcol="buyTrigger"),
              label="hardStop")

#rules
add.rule(strategy.st, name="ruleSignal", 
         arguments=list(sigcol="buyTrigger", sigval=TRUE, ordertype="market", 
                        orderside="long", replace=FALSE, prefer="Open", 
                        orderqty=10000, orderset="orders"), 
         type="enter", path.dep=TRUE,
         label="newEntry")

#run
t1 <- Sys.time()
out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
t2 <- Sys.time()
print(t2-t1)

#set up analytics
updatePortf(portfolio.st)

#performance analytics
chart.Posn(portfolio.st)
stopLimit <- function(HLC, n, maType, pctATR) {
  ATR <- ATR(HLC, n=n, maType=maType)
  ATR <- ATR$atr
  close <- Cl(HLC)
  atrStopProfit <- close+(ATR*300*pctATR)
  atrStopLoss <- close-(ATR*100*pctATR)
  atrStop <- cbind(atrStopProfit, atrStopLoss)
  colnames(atrStop) <- c("profit", "loss")
  return(atrStop)
}

hardStop <- function(HLC, m, n, maType, pctATR) {
  dayHigh <- Hi(HLC)[,1]
  runHigh <- periodHigh(HLC, m)
  runHigh <- replace(runHigh, is.na(runHigh), 0)
  potsLoss <- stopLimit(HLC, n, maType, pctATR)$loss
  potsLoss <- ifelse(dayHigh > runHigh, potsLoss, NA)
  potsLoss <- na.locf(potsLoss)
  out <- potsLoss
  potsWin <- stopLimit(HLC, n, maType, pctATR)$profit
  potsWin <- ifelse(dayHigh > runHigh, potsWin, NA)
  print(potsWin <- na.locf(potsWin))
  pots <- cbind(potsLoss, potsWin)
  colnames(pots) <- c("potsloss", "potswin")
  return(pots)
}

add.indicator(strategy.st, name="hardStop",
              arguments=list(HLC=quote(HLC(mktdata)), m=daysHigh,
                             n=period, wilder=TRUE, pctATR=pctATR),
              label="potsSpot")

add.signal(strategy.st, name="sigCrossover",
           arguments=list(columns=c("Close", "potswin.potsSpot"), relationship="lt", cross=FALSE),
           label="sellTriggerProfit")