This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| title: A replication of the Practical Application section in 'The Probability of Backtest | |
| Overfitting' - Bailey et al. | |
| output: | |
| html_notebook: | |
| # html_document: | |
| # df_print: paged | |
| --- | |
| In their paper "The Probability of Backtest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(blotter) | |
| # Remove portfolio and account data if run previously | |
| try(rm("portfolio.txnsim_rnorm_port","account.txnsim_rnorm_acct",pos=.blotter), silent = TRUE) | |
| # load the example data | |
| currency("USD") | |
| stock("GSPC",currency="USD",multiplier=1) | |
| getSymbols('^GSPC', src='yahoo', index.class=c("POSIXt","POSIXct"),from='1998-01-01') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ############################################################################### | |
| # R (http://r-project.org/) Quantitative Strategy Model Framework | |
| # | |
| # Copyright (c) 2009-2017 | |
| # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich | |
| # | |
| # This library is distributed under the terms of the GNU Public License (GPL) | |
| # for full details see the file COPYING | |
| # | |
| # $Id$ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' calculate trade statistics for round turn trades. | |
| #' | |
| #' One 'trade' is defined as a series of transactions which make up a 'round turn'. | |
| #' It may contain many transactions. This function reports statistics on these | |
| #' round turn trades which may be used on their own or which are also used | |
| #' by other functions, including \code{\link{tradeStats}} and \code{\link{tradeQuantiles}} | |
| #' | |
| #' @details | |
| #' Additional methods of determining 'round turns' are also supported. | |
| #' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' calculate flat to flat per-trade statistics | |
| #' | |
| #' One 'trade' is defined as the entire time the symbol is not flat. | |
| #' It may contain many transactions. From the initial transaction that | |
| #' moves the position away from zero to the last transaction that flattens the | |
| #' position is all one 'trade' for the purposes of this function. | |
| #' | |
| #' This is sometimes referred to as 'flat to flat' analysis. | |
| #' | |
| #' Note that a trade that is open at the end of the measured period will |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' Monte Carlo simulate strategy results | |
| #' | |
| #' 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. | |
| #' | |
| #' 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ### Required returns due to testing multiplicity ------ Harvey and Liu | |
| ### (2014): "Backtesting", Duke University | |
| Profit_Hurdle <- function (num_tests, num_obs, alpha_sig, vol_annual, RHO){ | |
| ############################### | |
| ####### Parameter inputs ###### | |
| ### 'num_tests': No. of tests one allows for in multiple tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require(MASS) # for mvrnorm() | |
| ### Generate empirical p-value distributions based on Harvey, Liu and Zhu (2014) ------ Harvey and Liu | |
| ### (2014): "Backtesting", Duke University | |
| sample_random_multests <- function(rho, m_tot, p_0, lambda, M_simu){ | |
| ###Parameter input from Harvey, Liu and Zhu (2014) ############ | |
| ###Default: para_vec = [0.2, 1377, 4.4589*0.1, 5.5508*0.001,M_simu]########### | |
| p_0 <- p_0 ; # probability for a random factor to have a zero mean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' Monte Carlo simulate strategy results | |
| #' | |
| #' 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. | |
| #' | |
| #' 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #' Monte Carlo simulate strategy results | |
| #' | |
| #' 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. | |
| #' | |
| #' 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 |