Simulating Win/Loss streaks with R rle function

May 17, 2011
By

(This article was first published on Intelligent Trading, and kindly contributed to R-bloggers)

The following script allows you to simulate sample runs of Win, Loss, Breakeven streaks based on a random distribution, using the run length encoding function, rle in R. Associated probabilities are entered as a vector argument in the sample function.

You can view the actual sequence of trials (and consequent streaks) by looking at the trades result.  maxrun returns a vector of maximum number of Win, Loss, Breakeven streaks for each sample run. And lastly, the transition table gives a table of proportion of run transitions from losing streak of length n to streak of all alternate lengths.

Example output (max run length of losses was 8 here):

100*prop.table(tt)
    lt.2
lt.1      1      2      3      4      5      6      7      8
   1 41.758 14.298  5.334  1.662  0.875  0.131  0.000  0.044
   2 14.692  4.897  1.924  0.787  0.394  0.087  0.131  0.000
   3  4.985  2.405  0.525  0.350  0.000  0.000  0.044  0.000
   4  1.662  0.875  0.306  0.087  0.000  0.000  0.000  0.000
   5  0.831  0.219  0.175  0.000  0.000  0.044  0.000  0.000
   6  0.087  0.131  0.044  0.000  0.000  0.000  0.000  0.000
   7  0.087  0.087  0.000  0.000  0.000  0.000  0.000  0.000
   8  0.044  0.000  0.000  0.000  0.000  0.000  0.000  0.000

maxrun
 B  L  W
 3  8 17

-----------------------------------------------------------------------------------------
#generate simulations of win/loss streaks use rle function

trades<-sample(c("W","L","B"),10000,prob=c('.6','.35','.05'),replace=TRUE)
traderuns<-rle(trades)
tr.val<-traderuns$values
tr.len<-traderuns$lengths
maxrun<-tapply(tr.len,tr.val,max)

#streaks of losing trades
lt<-tr.len[which(tr.val=='L')]
lt.1<-lt[1:(length(lt)-1)]
lt.2<-lt[2:(length(lt))]

#simple table of losing trade run streak(n) frequencies
table(lt)

#generate transition table streak(n) vs streak(n+1)
tt<-table(lt.1,lt.2)
#convert to proportions
options(digits=2)
100*prop.table(tt)
maxrun

To leave a comment for the author, please follow the link and comment on his blog: Intelligent Trading.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags:

Comments are closed.