Skip to content

Instantly share code, notes, and snippets.

View jaymon0703's full-sized avatar

Jasen Mackie jaymon0703

View GitHub Profile
@jaymon0703
jaymon0703 / perTradeStats_InitQty_EndQty.R
Created March 9, 2017 19:56
perTradeStats with added columns Init.Qty and End.Qty
#' 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.
#'
@jaymon0703
jaymon0703 / perTradeStats_with_Increased.to.Reduced.R
Last active January 24, 2017 19:02
perTradeStats with "increased.to.reduced" assuming FIFO to line up start and end dates for round turn trades
#' 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
#' 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
@jaymon0703
jaymon0703 / Harvey & Liu - Profit Hurdle.R
Created November 4, 2016 20:12
R-view of Profit Hurdle in Harvey & Liu (2014)
### 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;
@jaymon0703
jaymon0703 / Haircut_SR with sample_random_multests.R
Last active November 17, 2016 19:42
Harvey and Liu - Backtesting
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
#' 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
@jaymon0703
jaymon0703 / mcsim_wMultipleStats.R
Last active September 19, 2016 09:56
mcsim with Sharpe Ratio added
#' 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
@jaymon0703
jaymon0703 / txnsim.R
Last active April 8, 2017 12:41
start of txnsim() function for simulating blotter trades whilst retaining strategy characteristics
#' Monte Carlo analysis of transactions
#'
#' Running simulations with similar properties as the backtest or production
#' portfolio may allow the analyst to evaluate the distribution of returns
#' possible with similat trading approaches and evaluate skill versus luck or
#' overfitting.
#'
#' @details
#'
#' If \code{update=TRUE} (the default), the user may wish to pass \code{Interval}
@jaymon0703
jaymon0703 / mcsim_gist.R
Last active July 5, 2016 20:14
mcsim gist for rapid prototypive collaborative narrative R
#' Monte Carlo simulate strategy results
#'
#' Return bands of returns based on Monte Carlo simulations of back-test results
#' @param Portfolio string identifier of portfolio name
#' @param Account string identifier of account name
#' @param n number of simulations, default = 1000
#' @param \dots any other passthrough parameters
#' @param l block length, default = 20
#' @param use determines whether to use 'daily' or 'txn' PL, default = "equity" ie. daily
#' @param gap numeric number of periods from start of series to start on, to eliminate leading NA's
@jaymon0703
jaymon0703 / mcsimr.R
Last active May 21, 2016 13:39
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)