walk.forward: Rolling Walk Forward Analysis

View source: R/walk.forward.R

walk.forwardR Documentation

Rolling Walk Forward Analysis

Description

A wrapper for apply.paramset and applyStrategy, implementing a Rolling Walk Forward Analysis (WFA).

Usage

walk.forward(
  strategy.st,
  paramset.label,
  portfolio.st,
  account.st,
  period,
  k.training,
  nsamples = 0,
  audit.prefix = NULL,
  k.testing,
  obj.func = function(x) {     which(x == max(x)) },
  obj.args = list(x = quote(tradeStats.list$Net.Trading.PL)),
  anchored = FALSE,
  include.insamples = TRUE,
  ...,
  verbose = FALSE,
  savewf = FALSE,
  saveenv = FALSE,
  psgc = TRUE
)

Arguments

strategy.st

the name of the strategy object

paramset.label

a label uniquely identifying within the strategy the paramset to be tested

portfolio.st

the name of the portfolio object

account.st

the name of the account object

period

the period unit, as a character string, eg. 'days' or 'months'

k.training

the number of periods to use for training, eg. '3' months

nsamples

the number of sample param.combos to draw from the paramset for training; 0 means all samples (see also apply.paramset)

audit.prefix

default NULL. if not NULL, walk.forward will store the audit environment from each training period, containing an enviroment called .audit, with all in-sample portfolios and orderbooks as well as information as to which param.combos were evaluated, and the result of the objective function. In addition, a special file is generated that contains portfolio and orderbook for the concatenated testing param.combos as selected by the objective function, plus (optionally) complete in-sample portfolios and orderbooks for reference (see include.insamples)

k.testing

the number of periods to use for testing, eg. '1 month'

obj.func

a user provided function returning the best param.combo from the paramset, based on training results; defaults to 'max'

obj.args

a user provided argument to obj.func, defaults to quote(tradeStats.list$Net.Trading.PL)

anchored

whether to use a fixed start for the training window (TRUE), or a sliding start (FALSE); defaults to FALSE

include.insamples

will optionally run a full backtest for each param.combo in the paramset, and add the resulting in-sample portfolios and orderbooks to the audit environment; default TRUE

...

optional parameters to pass to apply.paramset()

verbose

dumps a lot of info during the run if set to TRUE, defaults to FALSE

savewf

boolean, default FALSE. if TRUE, saves audit information on training and testing periods to working directory for later analysis

saveenv

boolean, default FALSE. if TRUE, save the paramset environment information for each trial, and not just the tradeStats and chosen paramset

psgc

boolean, if TRUE, the default, run gc() in each worker session to conserve RAM.

Details

walk.forward executes a strategy on a portfolio, while rolling a re-optimization of one of the strategies parameter sets during a specified time period (training window), then selecting an optimal parameter combination from the parameter set using an obj function, then applying the selected parameter combo to the next out-of-sample time period immediately following the training window (testing window). Once completed, the training window is shifted forward by a time period equal to the testing window size, and the process is repeated. The final testing window may be shorter than the full testing window, if the length of the time series does not allow a full testing window.

'anchored' walk forward forces all training windows to start on the first observation of the market data. This can be useful if the indicators make use of all the data, e.g. for a risk metric such as a volatility estimator, for a regime model, or for a long-memory process of some sort. If anchored=TRUE and you have specified k.training, then the performance of each paramset will only be evaluated on the rolling training window, even though larger (anchored) periods are used for input calculations.

Note that walk.forward will generate out of sample (OOS) transactions using the chosen parameter set into the portfolio designated by portfolio.st. So walk.forward shoud be supplied with a 'clean' portfolio environment to avoid issues such as out of order transactions.

The psgc argument is a tradeoff between memory efficiency and speed. R does garbage collection promarily when it is running low on RAM, but this automatic detection works poorly in parallel processes. If TRUE, the default, walk.proward and apply.paramset will call gc() at key points to limit RAM usage. For small tests, this is probably unecessary and will only slow the test. For large tests, even on substantial hardware, it may be the difference between completing the test and crashing R.

Value

a list consisting of a slot containing detailed results for each training + testing period, as well as the portfolio and the tradeStats() for the portfolio

Author(s)

Jan Humme, Brian G. Peterson

References

Tomasini, E. and Jaekle, U. Trading Systems. 2009. Chapter 6

See Also

applyStrategy , apply.paramset , chart.forward , chart.forward.training , endpoints , tradeStats


braverock/quantstrat documentation built on Sept. 15, 2023, 11:32 a.m.