We use indicators external to trading data that we merge with an OLHC object. Our objective is to build a quantstrat model that addresses multiple equities, but we continue to get error messages that indicate we have not properly built such. Furthermore, the error refers to EMA even though we do not explicitly use an EMA.
I started with #FXQuantTrader code accepted as the answer here: quantstrat: how to create multiple indicators, signal rules, which I can make work with indicators external to OHLC data and a custom function called by add.signal, but cannot make the jump to multiple equities.
This Stack Overflow entry R - Quantstart: Testing Strategy on Multiple Equities purports to address the subject directly, but the example provided offers the suggestion to
“load a test strategy you'd use your own”
which begs the question of how to build a multi-equity strategy. Trying to implement the applyStrategy call from this post using my strategy yields the error
“formal argument 'n' matched by multiple actual arguments”.
Commenting out the parameters section of this call gets me back to the original error.
The Quantstrat documentation https://www.rdocumentation.org/packages/quantstrat/versions/0.16.2 refers to the following, but each example uses only one equity: (a) MaCross (body of the documentation), (b) DataCamp course https://www.datacamp.com/community/blog/financial-trading-in-r-with-ilya-kipnis (I took it in its entirety), (c) the presentation of qauntstrat http://past.rinfinance.com/agenda/2018/BrianPeterson.html#1 at the R/Finance 2018 conference uses one EFT: EEM.
Other resources include Guy Yollin’s notes http://www.r-programming.org/papers, but slides 18, 21 and 41 of his first (setup) deck presents the same standard deviation and lookback “n” as does the SO post referred to above without an explanation. I tried various combinations, but still had the same errors.
Ilna Kipnis’s “Nuts & Bolts…” blog posts https://quantstrattrader.wordpress.com/2014/09/09/nuts-and-bolts-of-quantstrat-part-i/ demonstrate the use of multiple equities, but he does not list parameters in the applyStrategy call, so perhaps this is not where my problem is. This seems to be confirmed by to Tim Trice’s online quantstrat book, (another resource to which the quantstrat documentation refers) where he says – in the context of a multiple equity application – that
“There is no need to get into additional parameters at the moment.” (Section 5.5).
I also experimented with the “apply” function but without any success.
.blotter <- new.env()
.strategy <- new.env()
fastMA = 12
slowMA = 26
currency('USD')
startDate='2017-03-24'
endDate = "2017-08-05"
initEq=1000000
portfolio.st='macd'
account.st='macd'
symbols <- c("NOV", # National-Oilwell Varco, Inc.
"AERI", # Aerie Pharmaceuticals Inc
"AGN" # Allergan plc
)
Cx.AERI <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0)
Cx.AGN <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
Cx.NOV <- c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0)
getSymbols(symbols,from=startDate, to=endDate) # gets xts object
Tplus <- merge.xts(AERI, AGN, NOV, Cx.AERI,Cx.AGN,Cx.NOV)
stock.st='Tplus'
initPortf(portfolio.st,symbols=stock.st)
initAcct(account.st,portfolios=portfolio.st)
initOrders(portfolio=portfolio.st)
stock(symbols,currency="USD", multiplier =1)
strat.st<-portfolio.st
strategy(strat.st, store=TRUE)
add.indicator(strat.st, name = "MACD",
arguments = list(x=quote(Cl(mktdata)),
nFast=fastMA,
nSlow=slowMA),
label='_'
)
macdSMAsig2 <- function(data) {
sig <- data[, "Cx._"] >0 & data[, "macd._"] > 0
colnames(sig) <- "upSig"
sig
}
add.signal(strat.st,name="macdSMAsig2",
arguments = list(data = quote(mktdata)),
label="enterSig"
)
add.signal(strat.st,name="sigThreshold",
arguments = list(column="signal._",
relationship="lt",
threshold=0,
cross=TRUE),
label="signal.lt.zero"
)
add.rule(strat.st,name='ruleSignal',
# be careful to get the label of the signal column correct:
arguments = list(sigcol="upSig.enterSig",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long',
threshold=NULL),
type='enter',
label='enter',
storefun=FALSE
)
add.rule(strat.st,name='ruleSignal',
arguments = list(sigcol="signal.lt.zero",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long',
threshold=NULL,
orderset='exit2'),
type='exit',
label='exit'
)
out<-applyStrategy(strat.st , portfolios=portfolio.st,verbose=TRUE)
I expected some trades, but instead got this error message:
>Error in EMA(c(45.849998, 45.549999, 45.450001, 45.25, 45.450001,
45.349998, : ncol(x) > 1. EMA only supports univariate 'x'