Skip to content

Instantly share code, notes, and snippets.

@jaymon0703
Last active May 21, 2016 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jaymon0703/cb0717064200da7bc5c832d72354d07f to your computer and use it in GitHub Desktop.
Save jaymon0703/cb0717064200da7bc5c832d72354d07f to your computer and use it in GitHub Desktop.
A Monte Carlo Simulation function for your back-test results - in R
# Record script start time ----------------------------------------
t1 <- Sys.time()
# Load required packages ------------------------------------------
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(ggplot2)
library(timeSeries)
# Build the function ----------------------------------------------
mcsimr <- function(n, b = TRUE){
# Read price data and build xts object
data <- read.csv("yourdirectory/simSample.csv", header = TRUE, stringsAsFactors=F)
s1.dates <- as.Date(data[,2], format="%d-%m-%Y") #beware the formatting may need adjusting for your excel settings
s1 <- xts(data[,3], s1.dates)
# Calculate ROC
ret <- ROC(s1[,1])
# Chart cum returns
chart.CumReturns(ret)
# Set up for Sample() and Replicate()
ret_sample <- replicate(n,sample(as.vector(ret[-1,]), replace=b)) #use ret[-1] so we exclude 1st NA value from ROC calc
ret_cum_sample <- apply(ret_sample, 2, function(x) cumsum(x))
ret_cum_samplexts <- xts(ret_cum_sample, s1.dates[-1]) #use s1.dates[-1] so that length of dates is identical to length of ret_sample
# Build the 5% and 95% quantile datasets
ret_5 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .05))
ret_5 <- as.xts(ret_5)
ret_95 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .95))
ret_95 <- as.xts(ret_95)
ret_25 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .25))
ret_25 <- as.xts(ret_25)
ret_75 <- apply(ret_cum_samplexts, 1, function(x) quantile(x, .75))
ret_75 <- as.xts(ret_75)
charts <- merge(ret_5, ret_95, ret_25, ret_75)
# Draw the graph with a ribbon
h <- ggplot(charts, aes(x = index(charts))) +
geom_ribbon(aes(ymin = ret_25, ymax = ret_75, colour = "50%"), alpha = 0.3, fill = "red3") +
geom_ribbon(aes(ymin = ret_5, ymax = ret_95, colour = "90%"), alpha = 0.3, fill = "cornflowerblue") +
theme(axis.text.x = element_text(angle=0, hjust = 0),
axis.title = element_text(face = 'bold', size = 14),
title = element_text(face = 'bold', size = 16),
legend.position = 'bottom',
legend.title = element_blank(),
legend.text = element_text(size = 12),
legend.key.width = unit(2, 'cm'))
h <- h + geom_line(aes(y = cumsum(ret[-1,])), colour = "black", linetype = 1) +
ylab(label="Cumulative Returns") +
xlab(label="Time") +
ggtitle("Returns Distribution")
h
return(h)
}
# End of function -------------------------------------------------
# Run function ----------------------------------------------------
mcsimr(1000)
# Record and print time to run script -----------------------------
t2 <- Sys.time()
difftime(t2,t1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment