mcsim: Monte Carlo simulate strategy results

View source: R/mcsim.R

mcsimR Documentation

Monte Carlo simulate strategy results

Description

This function resamples the daily transaction, cash equity, or percent-return P&L from a portfolio of trading results. It may be applied to both real (post-trade) and backtested transactions.

Usage

mcsim(Portfolio = NULL, Account = NULL, n = 1000, replacement = TRUE,
  ..., use = c("equity", "txns", "returns", "cash"), l = 1,
  varblock = FALSE, gap = 1, CI = 0.95, cashPL = NULL)

Arguments

Portfolio

string identifier of portfolio name

Account

string identifier of account name

n

number of simulations, default = 1000

replacement

sample with or without replacement, default TRUE

...

any other passthrough parameters

use

determines whether to use 'equity', 'txn', 'returns', or 'cash' P\&L, default = "equity" ie. daily

l

block length, default = 1

varblock

boolean to determine whether to use variable block length, default FALSE

gap

numeric number of periods from start of series to start on, to eliminate leading NA's

CI

numeric specifying desired Confidence Interval used in hist.mcsim(), default 0.95

cashPL

optional regular xts object of cash P&L if use='cash' and you don't want to use a blotter Portfolio to get P&L.

Details

The general argument here is that you can compare the performance of real portfolio equity or a backtest equity curve to a sampled version of the same.

We've chosen to use daily frequency for the resampling period. If your holding period is longer than one day, on average, the samples will increase variability in the overall path. If your average holding period is shorter than a day, the mcsim function will still provide a useful benchmark for comparing to other strategies, but you may additionally wish to sample round turn trades, as provided in (TODO: add link once function exists).

A few of the arguments and methods probably deserve more discussion as well.

use describes the method to use to generate the initial daily P\&L to be sampled from. The options are:

  • equitywill use daily portfolio cash P&L

  • txnwill use transaction Net.Trading.PL

Sampling may be performed either with or without replacement.

  • without replacementIf sampled **without** replacement, the replicated equity curves will use all the same data as the input series, only reordered. This will lead to all the replicates having identical final P\&L and mean returns, but different paths along the way.

  • with replacementIf sampled **with** replacement, individual observations may be re-used. This will tend to create more variability than replicates without replacement.

If the post-trade or backtested equity curve exhibits autocorrelation, runs, streaks, etc. it may be advantageous to utilize a block resampling method. The length of the block "l" may be fixed or variable. If a varblock method is used, the distribution of block lengths will be uniform random for replacement=FALSE and geometric random for replacement=TRUE. By sampling blocks, the resampled returns will contain more of the structure of the original series. If varblock=TRUE, the blocks will be of variable length, centered around l.

Value

a list object of class 'mcsim' containing:

  • replicates:an xts object containing all the resampled time series replicates

  • percreplicates:an xts object containing all the resampled time series replicates in percent

  • dailypl:an xts object containing daily P&L from the original backtest

  • percdailypl:an xts object containing daily P&L in percent from the original backtest

  • initeq:a numeric variable containing the initEq of the portfolio, for starting portfolio value

  • num:a numeric variable reporting the number of replicaes in the simulation

  • length:a numeric variable reporting the block length used in the simulation, if any

  • samplestats:a numeric dataframe of various statistics for each replicate series

  • percsamplestats:a numeric dataframe of various statistics for each replicate series in percent

  • original:a numeric dataframe of the statistics for the original series

  • percoriginal:a numeric dataframe of the statistics for the original series in percent terms

  • stderror:a numeric dataframe of the standard error of the statistics for the replicates

  • percstderror:a numeric dataframe of the standard error of the statistics for the replicates in percent

  • CI:numeric specifying desired Confidence Interval used in hist.mcsim(), default 0.95

  • CIdf:a numeric dataframe of the Confidence Intervals of the statistics for the bootstrapped replicates

  • CIdf_perc:a numeric dataframe of the Confidence Intervals of the statistics for the bootstrapped replicates in percent

  • w:a string containing information on whether the simulation is with or without replacement

  • use: a string with the value of the 'use' parameter, for checking later

  • seed: the value of .Random.seed for replication, if required

  • call:an object of type call that contains the mcsim call

If use='cash', you must also supply a daily (or other regular frequency) cash P&L xts object in the dailyPL argument.

Note that this object and its slots may change in the future. Slots replicates,dailypl,initeq, and call are likely to exist in all future versions of this function, but other slots may be added and removed as S3method's are developed.

Note

Requires boot package

Author(s)

Jasen Mackie, Brian G. Peterson

See Also

boot plot.mcsim hist.mcsim

Examples

## Not run: 

demo('longtrend', ask=FALSE)

nrsim <- mcsim("longtrend", "longtrend", n=1000, replacement=FALSE, l=1, gap=10, CI=0.95)
nrblocksim.75 <- mcsim("longtrend", "longtrend", n=1000, replacement=FALSE, l=10, gap=10, CI=0.75)
rsim <- mcsim("longtrend", "longtrend", n=1000, replacement=TRUE, l=1, gap=10)
varsim <- mcsim("longtrend", "longtrend", n=1000, replacement=TRUE, l=10, varblock=TRUE, gap=10)
nrvarsim <- mcsim("longtrend", "longtrend", n=1000, replacement=FALSE, l=10, varblock=TRUE, gap=10)

quantile(varsim)
quantile(nrsim)
quantile(nrvarsim)

summary(varsim, normalize=FALSE)
summary(nrsim)
summary(nrvarsim)

plot(nrsim, normalize=TRUE)
plot(nrsim, normalize=FALSE)
plot(varsim)
plot(rsim)
hist(nrblocksim.75)
hist(rsim)
hist(varsim)


## End(Not run) #end dontrun


braverock/blotter documentation built on Feb. 13, 2023, 1 p.m.