0

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)
  • Provide complete code that you have tried and a sample dataset using `dput(x)` – Nad Pat Dec 22 '21 at 07:02
  • Thank you Nad Pat, this is my first experience using stack overflow. I have made an adjustment, now but the error is similar... It only happens when I include more than 7 indicators to the quantstrat strategy. I appreciate your time, hope I can contribute to this group as soon as I become an expert at these libraries. I included the code as requested. – Daniel Calliari Dec 22 '21 at 12:03

2 Answers2

0

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

Hoanheoo
  • 1
  • 2
  • As I read the Guide for Quantstrat I think this is the way out of that limit... Thank you, I will advance with this solution and report the result here. – Daniel Calliari Dec 24 '21 at 08:31
0

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

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 28 '21 at 21:52
  • @Daniel Calliari Note you should only need to call `apply Indicators()` once, even with a large number of indicators. Your real constraint on # of indicators is simply the RAM you have available to generate the market data needed per symbol. Your original error looks like it relates to not having the necessary input for one of your indicators. As you seem to have solved your question, won't attempt to post an answer to your question unless you're still after one. – FXQuantTrader Jan 12 '22 at 02:11
  • Perfect, thank you FXQuantTrader... – Daniel Calliari Jan 14 '22 at 01:19