The error message I receive when I add more than 7 indicators to the strategy and use the applyIndicators function:

Error in (function (HLC, n = 20, maType, c = 0.015, ...) : Price series must be either High-Low-Close, or Close/univariate. In addition: Warning message: In log(x) : NaNs produced


The code is as follows:

library(tsibble)
library(rlang)
library(dplyr)
library(tawny)
library(kernlab)
library(corpcor)
library(xts)

library(tidyquant)
library(rsample)
library(tidyr)
library(dplyr)

library(ggplot2)
library(dplyr)
library(magrittr)
library(scales)
library(tsibble)
library(purrr)
library(timetk)
library(kableExtra)

library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(FinancialInstrument)
library(foreach)
library(blotter)
library(quantstrat)
library(ggplot2)
library(dplyr)

library(IKTrading)
library(png)
library(devtools)

####### Part 1: Boiler Plate Set Up ####### 

# Set up Blotter
rm(list = ls(.blotter), envir = .blotter)

# Parameters and Dates
initdate <- "2010-01-01"
from <- "2011-01-01" #start of backtest
to <- "2021-12-01" #end of backtest

Sys.setenv(TZ= "EST") #Set up environment for timestamps
currency("USD") #Set up environment for currency to be used


# Get Data
symbols <- c("SPY","AAPL", "MSFT", "GOOG", "FB", "TWTR", "AMZN", "IBM", "NFLX", "NVDA","BAC", "UNH", "TSLA", "ZM", "PTON", "QCOM", "GE") #symbols used in our backtest
getSymbols(Symbols = symbols, src = "yahoo", from=from, to=to, adjust = TRUE) #receive data from google finance,  adjusted for splits/dividends

# Tells quanstrat what instruments present and what currency to use
stock(symbols, currency = "USD", multiplier = 1) 

# removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 


###Set Up Strategy Parameters

# Portfolio Size and Trade Size
tradesize <-10000 # default trade size. What if i do it in terms of % of the Portfolio?
initeq <- 100000 # default initial equity in our portfolio

# Naming Strategy, Portfolio and Account
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 Strategy, Portfolio and Account 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)


####### Part 2: Indicators, Signals and Rules ####### 

##### 2.1 Indicators

chartSeries(TSLA,type="line",subset='2019-02::2021-12',theme=chartTheme('white'))
candleChart(IBM, up.col = "black", dn.col = "red", theme = "white")
addSMA(n = c(200,50), on = 1, col = c("red", "blue"))
plot(RSI(Cl(AMZN), n=10)) #Plots the RSI with lookback equal to 10 days 

## The add.indicator() Function
# Parameter List
.RSI.n = 3
.RSI.Up = 80
.RSI.Down = 20
.MACDSlow = 26
.MACDFast = 12
.Bbands.n = 20
.Bbands.sd = 2
.ROCSlow = 30
.ROCFast = 14


## The add.indicator() Function
add.indicator(strategy = strategy.st, name = "RSI", arguments = list(price = quote(Cl(mktdata)), n=.RSI.n), label = 'RSI')
add.indicator(strategy = strategy.st, name = "MACD", arguments = list(x=quote(Cl(mktdata)),nFast=.MACDFast, nSlow=.MACDSlow),label='MACD' )
add.indicator(strategy = strategy.st, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), n = .Bbands.n, maType = "SMA", sd = .Bbands.sd), label = "Bbands")
add.indicator(strategy = strategy.st, name = "ROC", arguments = list(x=quote(Cl(mktdata)),n=.ROCFast,type='continuous'),label="ROCFAST")
add.indicator(strategy = strategy.st, name = "ROC", arguments = list(x=quote(Cl(mktdata)),n=.ROCSlow,type='continuous'),label="ROCSLOW")
add.indicator(strategy = strategy.st, name = "momentum", arguments = list(x=quote(Cl(mktdata)),n=3),label="MOMFAST")
add.indicator(strategy = strategy.st, name = "momentum", arguments = list(x=quote(Cl(mktdata)),n=20),label="MOMSLOW")
add.indicator(strategy = strategy.st, name = "CCI", arguments = list(HLC = quote(HLC(mktdata)), n = 20, maType = "SMA", c = 0.015), label = "CCI")

test <- applyIndicators(strategy.st, mktdata=OHLC(AAPL))
tail(test,10)
2

There are 2 best solutions below

1
On

I think you can write your own indicator combining all indicators you want to add to your chart and use 1 add.indicator() to add them all

3
On

I mannaged to solve the problem by separating the indicators in batches and stating the applyIndicators() function more than once. I separated indicators by arguments. The code worked fine after this was done. Quantstrat can support multiple indicators to build more complex strategies