3

I obtained the returns of the RUT index running a backtest in R in Quantstrat. Now I would like to create a bootstrap of the returns but I do not know how to do it. I know that the boot() function needs data, statistic, and number of bootstrap repetitions. In data I think I can add "returns" ( see code below ) But I do not know how to compute "statistic". Can someone help?

Please refer to the code below:

library(devtools) library(quantmod) library(quantstrat) library(TTR) library(png) library(IKTrading) install_github("braverock/blotter") install_github("braverock/quantstrat") install_github('IlyaKipnis/IKTrading') initdate <- "2010-01-01" from <- "2012-01-01" #start of backtest to <- "2017-31-12" #end of backtest n<- 30 Sys.setenv(TZ= "GMT") #Set up environment for timestamps currency("USD") #Set up environment for currency to be used symbols <- c("RUT") #symbols used in our backtest getSymbols("^RUT",src="yahoo", from="2012-01-01", to="2017-12-31", periodicity="daily") wma <- WMA(Cl(RUT), n=4, wts=c(1:4)) wmamaxt <- rollmaxr(wma, n, fill = NA) wmamint <- - rollmaxr(- wma, n, fill = NA) CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)} 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 = 'SMA', arguments = list(x = quote(Cl(mktdata)), n=100), label = 'SMA100') add.indicator(strategy = strategy.st, name = 'SMA', arguments = list(x = quote(Cl(mktdata)), n=30), label = 'SMA30') add.indicator(strategy = strategy.st, name = 'SMA', arguments = list(x = quote(Cl(mktdata)), n=15), label = 'SMA15') add.signal(strategy.st, name = "sigCrossover", arguments = list(columns=c("SMA15", "SMA30")), relationship = "lt", label = "Exitlong") add.signal(strategy.st, name = "sigCrossover", arguments = list(columns=c("SMA15", "SMA100")), relationship = "gt", label = "Enterlong") add.rule(strategy.st, name = "ruleSignal", arguments = list(sigcol = "Exitlong", sigval = TRUE, orderqty = "all", ordertype = "market", orderside = "long", replace = FALSE, prefer = "Open"), type = "exit") add.rule(strategy.st, name = "ruleSignal", arguments = list(sigcol = "Enterlong", sigval = TRUE, orderqty = 1, ordertype = "market", orderside = "long", replace = FALSE, prefer = "Open", osFUN = IKTrading::osMaxDollar, tradeSize = tradesize, maxSize = tradesize), type = "enter") out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st) updatePortf(portfolio.st) daterange <- time(getPortfolio(portfolio.st)$summary)[-1] updateAcct(account.st, daterange) updateEndEq(account.st) for(symbol in symbols){ chart.Posn(Portfolio = portfolio.st, Symbol = symbol, TA= c("add_SMA(n=15, col='blue')", "add_SMA(n=30, col='red')", "add_SMA(n=100, col='green')")) } # in “symbol” add “RUT” or “IXIC” final_acct <- getAccount(account.st) end_eq <- final_acct$summary$End.Eq returns <- Return.calculate(end_eq, method="log") library(boot) boot(returns, k=1, R=1000) Error in boot(returns, k = 1, R = 1000) : l'argomento "statistic" non è specificato e non ha un valore predefinito 
16
  • I removed tags for the bootstrap CSS framework and bootstrapping an application. You are interested in the statistical approach called bootstrapping. Please read the descriptions of the tags you add. I'm not sure if there is one for bootstrapping but you can check. Commented Jul 14, 2018 at 16:19
  • What is it that you mean by k=1? You need to tell boot what statistic to calculate on the samples. Commented Jul 14, 2018 at 16:22
  • I has read that you have to insert a function that produces the k statistics to be bootstrapped (k=1 if bootstrapping a single statistic)... k=1 was just a guess... The fact is that I do not know how to compute the statistic as I am asked to write a function that combined with the data produces the test statistic of interest, but my data already is a vector of the test statistic of interest that I am interested in and I do not need to combine it with a function to obtain it. I hope I was clear enough Commented Jul 14, 2018 at 16:28
  • For example you might put "mean" or "sd". In boot are you sampling from that vector? Maybe you want to start by bootstrapping the mean. Commented Jul 14, 2018 at 16:36
  • It's hard to understand what you are doing because you have a lot of code not related to the question. But it might be that Return.calculate is what you want. Commented Jul 14, 2018 at 16:39

2 Answers 2

1

I believe the following does what you want.

library(boot) boot_returns <- function(data, indices){ d <- data[indices] Return.calculate(d, method="log") } bret <- boot(end_eq, boot_returns, R=1000) dim(bret$t) bretMeans <- colMeans(bret$t, na.rm = TRUE) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Rui Barradas for you helpful answer. I have a question, I think that in data I have to insert my returns, but what do I need to insert as "indices"?
@Pietro Nothing, function boot will do it for you. And in data you will pass end_eq, not returns, this is what the function outputs. Check out bret$t0, identical(bret$t0, returns) outputs TRUE.
1

Here is a simple example of boot() [based on Chapter 5, Page 196 in ISLR] to calculate bootstrapped mean of a column in a dataframe. (R version 3.3.0). The boot() function needs data, a function that calculates a statistic of interest and number of bootstraps 'R'. Here, the function has to have two inputs i.e. data and an index to iterate.

Calculate mean using boot()

# Import libaries library(boot) # Create sample dataframe set.seed(1) x <- rnorm(1000, sd=10) y <- rnorm(1000, sd=1) df <- data.frame(x,y) # Create function to pass to boot() calc_mean = function(data, index){ mean_x <- mean(df$x[index]) return(mean_x) } boot(df$x, calc_mean, R=1000) 

enter image description here

Compare above bootstrap mean with that calculated directly with mean()

mean(df$x) 

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.