Alternatives to Monte Carlo testing - Matlab, R project and Python | futures io social day trading

Alternatives to Monte Carlo testing
 Updated: November 5th, 2013 (07:05 PM) Views / Replies: 1,027 / 6 Created: November 5th, 2013 (06:46 AM) by Big Mike Attachments: 0

 Welcome to futures io.

# Alternatives to Monte Carlo testing

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 46,336 since Jun 2009

Alternatives to Monte Carlo testing

Source:

Alternative to Monte Carlo Testing | rbresearch

Quoting
 When we backtest a strategy on a portfolio, it is a simple analysis of a single period in time. There are ways to “stress test” a strategy such as monte carlo, random portfolios, or shuffling the returns in a random order. I could never really wrap my head around monte carlo and shuffling the returns seemed to be a better approach because the actual returns of the backtest are used, but it misses one important thing… the impact of consecutive periods of returns. If we are backtesting a strategy and we want to minimize max drawdown, consecutive down periods have a significant impact on max drawdown. If, for example, the max drawdown occured due to 4 consecutive months during 2008, we wan’t to keep those 4 months together when shuffling returns. In my opinion, a better way to shuffle returns is to shuffle “blocks” of returns. This is nothing new, the TradingBlox software does monte carlo analysis this way. I had a look at the boot package and tseries package for their boot functions, but it was not giving me what I wanted. I wanted to visual a number of equity curves with blocks of returns randomly shuffled. To accomplish this in R, I wrote two functions. The shuffle_returns function takes an xts object of returns, the number of samples to run (i.e. how many equity curves to generate), and a number for how many periods of returns makes up a ‘block’ as arguments.The ran_gen function function is a function within the shuffle_returns function that is used to generate random blocks of returns. shuffle_returns returns an xts object with the random blocks of returns so we can do further analysis such as max drawdown, plotting, or pretty much anything in the PerformanceAnalytics package that takes an xts object as an argument. This is not a perfect implementation of this idea, so if anybody knows of a better way I’d be glad to hear from you. The example below uses sample data from edhec and generates 100 equity curves with blocks of 5 consecutive period of returns. Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).

R

Code
 ```require(PerformanceAnalytics) #Function that grabs a random number and then repeats that number r times ran_gen <- function(x, r){ #x is an xts object of asset returns #r is for how many consecutive returns make up a 'block' vec <- c() total_length <- length(x) n <- total_length/r for(i in 1:n){ vec <- append(vec,c(rep(sample(1:(n*100),1), r))) } diff <- as.integer(total_length - length(vec)) vec <- append(vec, c(rep(sample(1:(n*100),1), len = diff))) return(vec) } shuffle_returns <- function(x, n, r){ #x is an xts object of asset returns #n is the number of samples to run #r is for how many consecutive returns make up a 'block' and is passed to ran_gen mat <- matrix(data = x, nrow = length(x)) for(i in 1:n){ temp_random <- ran_gen(x = x, r = r) temp_mat <- as.matrix(cbind(x, temp_random)) temp_mat <- temp_mat[order(temp_mat[,2]),] temp_ret_mat <- matrix(data = temp_mat[,1]) mat <- cbind(mat, temp_ret_mat) } final_xts <- xts(mat, index(x)) return(final_xts) } #get sample data data(edhec) a <- edhec[,1] a <- head(a, -1) start <- Sys.time() yy <- shuffle_returns(a, 100, 5) chart.CumReturns(yy[,1:NCOL(yy)], wealth.index = TRUE, ylab = "Equity", main ="Generated Equity Curve of Shuffled Returns") end <- Sys.time() print(end-start)```
Mike

 The following 2 users say Thank You to Big Mike for this post:

 November 5th, 2013, 06:46 AM #2 (permalink) Quick Summary Quick Summary Post Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 46,336 since Jun 2009

I posted this because I wanted to ask input about his conclusions. Specifically:

Quoting
 In my opinion, a better way to shuffle returns is to shuffle “blocks” of returns.

So let's say during the meltdown of 2008 your portfolio took a nose dive. But you have a 10-year series of data to feed your monte carlo sim, is it really fair to allow all 10 years to be mixed and mashed up?

No, I would agree with him -- it is better to keep those blocks (like 2008) together since they represented real world decline that did not exist in other areas of the simulated series.

Bootstrap sampling... Thoughts?

@treydog999, @artemiso

Mike

 The following user says Thank You to treydog999 for this post:

 November 5th, 2013, 06:28 PM #5 (permalink) Elite Member New York, NY   Futures Experience: Beginner Platform: thinkorswim Broker/Data: TD Ameritrade Favorite Futures: Stocks   Posts: 958 since Jul 2012 Thanks: 640 given, 1,959 received I actually don't fully understand what @treydog999 said, so I won't comment on that. Just eyeballing it, both the ran_gen and shuffle_returns functions seem to be very slow. There should be an R profiler that you can use to verify this. If you intend to employ this frequently, it should help to rewrite it. You can generalize this approach and improve on most Monte Carlo implementations written out there (R developers tend to be poor programmers). This is a fun, relatively simple, but rewarding project, so I recommend everyone to try - I did something similar before so I'll send you some hints. Last edited by artemiso; November 5th, 2013 at 07:53 PM.
 The following user says Thank You to artemiso for this post:

Platform: My own custom solution
Favorite Futures: E-mini ES S&P 500

Posts: 46,336 since Jun 2009

artemiso
 I actually don't fully understand what @treydog999 said, so I won't comment on that. Just eyeballing it, both the ran_gen and shuffle_returns function seem to be very slow. There should be an R profiler that you can use to verify this. If you intend to employ this frequently, it should help to rewrite it. You can generalize this approach and improve on most Monte Carlo implementations written out there (R developers tend to be poor programmers). This is a fun, relatively simple, but rewarding project, so I recommend everyone to try - I did something similar before so I'll send you some hints.

I agree, it is incredibly slow. But so is R and Matlab in general it seems, due to lack of multithreading. I am still trying to figure out if I can multithread individual functions.

Mike

Elite Member
New York, NY

Futures Experience: Beginner
Platform: thinkorswim
Favorite Futures: Stocks

Posts: 958 since Jul 2012

Big Mike
 I agree, it is incredibly slow. But so is R and Matlab in general it seems, due to lack of multithreading. I am still trying to figure out if I can multithread individual functions. Anyway, please contribute code improvements Mike

Not sure if it's useful, but I sent you some hints anyway.

 futures io > Alternatives to Monte Carlo testing

Upcoming Webinars and Events (4:30PM ET unless noted)

Elite only

## Digging into the Details of iSystems w/Stage 5 & iSystems

Jun 5

 Similar Threads Thread Thread Starter Forum Replies Last Post kbeary33 NinjaTrader Programming 3 August 13th, 2013 06:45 PM wallsteetking NinjaTrader Programming 2 October 21st, 2011 06:36 AM Xeno NinjaTrader 8 April 12th, 2011 11:10 AM Big Mike Psychology and Money Management 3 February 7th, 2010 11:06 AM Big Mike Traders Hideout 1 June 14th, 2009 01:41 AM

All times are GMT -4. The time now is 06:54 PM.

 Copyright © 2018 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io All information is for educational use only and is not investment advice.There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts
Page generated 2018-05-23 in 0.12 seconds with 19 queries on phoenix via your IP 54.81.0.22