Indian Equity Backtesting in R

I am trying to use RELIANCE.BO and SBIN.NS for backtesting my trading strategy in Indian Markets by quantstrat in R. However I always get the following error:

RELIANCE.BO contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.

Even after using na.omit() and others, the errors keep on coming.

I am using this website. My strategy is working fine in SPY and other US/Europe indices, but it is the Indian equities where I am facing this issue.

A minimal reprex would helping in helping you diagnose your problem. Right now there isn't much information to go on. (REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.)

Sure, the issues popping out are below:

Warning message:
RELIANCE.BO contains missing values. Some functions will not work if objects contain missing values in the middle of the series. Consider using na.omit(), na.approx(), na.fill(), etc to remove or replace them.

applyStrategy(strategy.st, portfolios = portfolio.st)
Error in runSum(x, n) : Series contains non-leading NAs
checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)
Error in checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE) :
could not find function "checkBlotterUpdate"

I have used na.omit after the first warning message.

Please find below the full code for your reference:

Sys.setenv(TZ = "UTC")
currency('INR')
init_date <- "2007-12-31"
start_date <- "2008-01-01"
end_date <- "2009-12-31"
init_equity <- 1e4 # $10,000
adjustment <- TRUE
basic_symbols <- function() {
  symbols <- c(
    #"SPY"
    "RELIANCE.BO"
    #"SBIN.NS",
    #"ICICIBANK.NS"
  )
}
.blotter <- new.env()
.strategy <- new.env()
print(basic_symbols())
symbols <- basic_symbols()
getSymbols(Symbols = symbols, 
           src = "yahoo",
           from = start_date, 
           to = end_date,
           auto.assign=TRUE)
symbols <- na.omit(symbols)
stock(symbols, 
      currency = "INR", 
      multiplier = 1)
portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"
initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init_date)
initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date,
         initEq = init_equity)
initOrders(portfolio = portfolio.st,
           symbols = symbols,
           initDate = init_date)
strategy(strategy.st, store = TRUE)
add.indicator(strategy = strategy.st,
              name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 10),
              label = "nFast")
add.indicator(strategy = strategy.st, 
              name = "SMA", 
              arguments = list(x = quote(Cl(mktdata)), 
                               n = 30), 
              label = "nSlow")
add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "gte"),
           label = "long")
add.signal(strategy = strategy.st,
           name="sigCrossover",
           arguments = list(columns = c("nFast", "nSlow"),
                            relationship = "lt"),
           label = "short")
add.rule(strategy = strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "long",
                          sigval = TRUE,
                          orderqty = 100,
                          ordertype = "stoplimit",
                          orderside = "long", 
                          threshold = 0.0005,
                          prefer = "High", 
                          TxnFees = 0, 
                          replace = FALSE),
         type = "enter",
         label = "EnterLONG")
add.rule(strategy.st,
         name = "ruleSignal",
         arguments = list(sigcol = "short",
                          sigval = TRUE,
                          orderqty = -100,
                          ordertype = "stoplimit",
                          threshold = -0.005, 
                          orderside = "short", 
                          replace = FALSE, 
                          TxnFees = 0, 
                          prefer = "Low"),
         type = "enter",
         label = "EnterSHORT")
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "short", 
                          sigval = TRUE, 
                          orderside = "long", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = 0, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2SHORT")
add.rule(strategy.st, 
         name = "ruleSignal", 
         arguments = list(sigcol = "long", 
                          sigval = TRUE, 
                          orderside = "short", 
                          ordertype = "market", 
                          orderqty = "all", 
                          TxnFees = 0, 
                          replace = TRUE), 
         type = "exit", 
         label = "Exit2LONG")
cwd <- getwd()
setwd("./_data/")
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
  load(results_file)
} else {
  results <- applyStrategy(strategy.st, portfolios = portfolio.st)
  updatePortf(portfolio.st)
  updateAcct(account.st)
  updateEndEq(account.st)
  if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
    save(list = "results", file = results_file)
    save.strategy(strategy.st)
  }
}
setwd(cwd)
rm.strat(portfolio.st)
rm.strat(account.st)
symbols <- basic_symbols()
getSymbols(Symbols = symbols, src = "yahoo", 
           from = start_date, to = end_date, auto.assign=TRUE)
symbols <- na.omit(symbols)
initPortf(name = portfolio.st, symbols = symbols, initDate = init_date)
initAcct(name = account.st, portfolios = portfolio.st, initDate = init_date, 
         initEq = init_equity)
portfolio.st <- "Port.Luxor"
account.st <- "Acct.Luxor"
strategy.st <- "Strat.Luxor"
cwd <- getwd()
setwd("./_data/")
load.strategy(strategy.st)
setwd(cwd)
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st,
          symbols = symbols,
          initDate = init_date)
initAcct(name = account.st,
         portfolios = portfolio.st,
         initDate = init_date, 
         initEq = init_equity)
initOrders(portfolio = portfolio.st,
           initDate = init_date)
applyStrategy(strategy.st, portfolios = portfolio.st)
checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
tstats <- tradeStats(portfolio.st)
kable(t(tstats))

Could someone please help?

I think you left out the necessary libraries from your code

Okay, please find the libraries before the code below.

library(quantstrat)
library(ggplot2)
library(data.table)
library(dplyr)
library(DT)
library(htmltools)
library(htmlwidgets)
library(knitr)
library(lattice)
library(pander)
library(tidyr)
library(webshot)

I think thats excessive, where do you use webshot for example ?
can you pair this down to a minimal code ?

Hi, I am simply using the libraries as shown here:

I am still a beginner, so pardon if extra libraries are used. Please do run the code on your machine and help me. Thank you so much.

two issues:
1)
you don't remove the NA's from the RELIANCE.BO object, rather you mistakenly try it on the symbols
#symbols <- na.omit(symbols) # wrong
RELIANCE.BO <- na.omit(RELIANCE.BO)
2)
Secondly the book you reference describe the absent function, so you must include it in your source code

Hi, I solved it using your suggestions. Many thanks.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.