Thank you for for reposting a readable question, though the origin of quantstrat
and IKTrading on github still took some study, and I cannot see where mktdata
was supposed to come from.
If you get no expert response here, you might get a more appropriate set of
users if you ask this question on the R-Sig-finance mailing list (where readers
might actually recognize the issue offhand), or by asking your question at the
quantstrat issues forum [1]. Packages that don't build clean enough to
install from CRAN can really lead to wild goose chases debugging the package
rather than helping someone understand R (the topic of this mailing list).
No matter where you ask this question, getting the code to be internally
complete including data (study up on the dput function in the links I pointed
you at
to before) will really help your helpers and you will learn more about R
yourself. The reprex package I mentioned makes it easy to confirm that others
have a good chance to see the behavior you saw when they run your example.
[1] https://github.com/Braddock/quantstrat
On July 14, 2018 6:02:08 AM PDT, Pietro Fabbro via R-help <r-help at
r-project.org> wrote:>I apologize if the data I will insert will not be enough.
>
>So, I am trying to run a strategy through the package Quantstrat.
>
>install.packages("quantstrat")
>
>
>My problem is that I get the following error
>Error incolnames<-(tmp, value = seq(ncol(tmp_val))) :
>attempt to set 'colnames' on an object with less than two dimensions
>
>when I try to run the following command:
>
>> out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)
>I do not have this problem if I use, as indicator, one or more
>indicators, which are already defined by the package TTR.
>
>I have this error only when I try to use a custom indicator. Here is
>the code for the custom indicator that I use:
>
>wma <- WMA(Cl(mktdata), 4, wts=c(1:4))
>wmamaxt <- rollmaxr(wma, 30, fill = NA)
>wmamint <- - rollmaxr(- wma, 30, fill = NA)
>CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) /
>(wmamaxt - wmamint)}
>Please refer to the following code:
>
>library(devtools)
>library(quantmod)
>library(quantstrat)
>library(TTR)
>library(png)
>library(IKTrading)
>
>wma <- WMA(Cl(mktdata), 4, wts=c(1:4))
>wmamaxt <- rollmaxr(wma, 30, fill = NA)
>wmamint <- - rollmaxr(- wma, 30, fill = NA)
>CNOwma <- function (mktdata=quote(mktdata),x) {(wma - wmamint) /
>(wmamaxt - wmamint)}
>initdate <- "2010-01-01"
>from <- "2012-01-01" #start of backtest
>to <- "2017-31-12" #end of backtest
>
>Sys.setenv(TZ= "EST") #Set up environment for timestamps
>
>currency("USD") #Set up environment for currency to be used
>
>symbols <- c("RUT", "IXIC") #symbols used in our
backtest
>getSymbols(Symbols = symbols, src = "google", from=from, to=to,
adjust
>= TRUE) #receive data from google finance, adjusted for
>splits/dividends
>
>stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat
what
>instruments present and what currency to use
>
>tradesize <-10000 #default trade size
>initeq <- 100000 #default initial equity in our portfolio
>
>strategy.st <- portfolio.st <- account.st <- "firststrat"
#naming
>strategy, portfolio and account
>
>#removes old portfolio and strategy from environment
>rm.strat(portfolio.st)
>rm.strat(strategy.st)
>
>#initialize portfolio, account, orders and strategy objects
>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)
>
>add.indicator(strategy = strategy.st,
>name = 'CNOwma',
>arguments = list(x = quote(Cl(mktdata)), n=4),
>label = 'CNOwma4')
>
>
>
>
>
>add.signal(strategy.st, name = "sigThreshold",
>arguments = list(column = "CNOwma4", threshold = 0.6,
>relationship = "gt", cross = TRUE),
>label = "longthreshold")
>
>
>add.signal(strategy.st, name = "sigThreshold",
>arguments = list(column = "CNOwma4", threshold = 0.6,
>relationship = "lt", cross = TRUE),
>label = "shortthreshold")
>
>
>
>
>add.rule(strategy.st, name = "ruleSignal",
>arguments = list(sigcol = "longthreshold", sigval = TRUE,
>orderqty = "all", ordertype = "market",
>orderside = "long", replace = FALSE,
>prefer = "Open"),
>type = "enter")
>
>
>add.rule(strategy.st, name = "ruleSignal",
>arguments = list(sigcol = "shortthreshold", sigval = TRUE,
>orderqty = "all", ordertype = "market",
>orderside = "long", replace = FALSE,
>prefer = "Open"),
>type = "exit")
>
>add.rule(strategy.st, name = "ruleSignal",
>arguments = list(sigcol = "shortthreshold", sigval = TRUE,
>orderqty = "all", ordertype = "market",
>orderside = "short", replace = FALSE,
>prefer = "Open"),
>type = "enter")
>
>add.rule(strategy.st, name = "ruleSignal",
>arguments = list(sigcol = "longthreshold", sigval = TRUE,
>orderqty = "all", ordertype = "market",
>orderside = "short", replace = FALSE,
>prefer = "Open"),
>type = "exit")
>
>
>
>out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
>
>
>When I run the traceback() of the error, this is what I get:
>> traceback()
>4: stop("attempt to set 'colnames' on an object with less than
two
>dimensions")
>3: `colnames<-`(`*tmp*`, value = seq(ncol(tmp_val)))
>2: applyIndicators(strategy = strategy, mktdata = mktdata, parameters
>parameters,
>...)
>1: applyStrategy(strategy = strategy.st, portfolios = portfolio.st
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
--
Sent from my phone. Please excuse my brevity.