Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc entry in bt.r, ta.r, @ issue and export index.xts in util.r, modify package script to use roxygen2 #13

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 55 additions & 54 deletions R/bt.r
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


###############################################################################
# Align dates, faster version of merge function
#' Align dates, faster version of merge function
#' @export
###############################################################################
bt.merge <- function
Expand Down Expand Up @@ -83,7 +83,7 @@ bt.merge <- function
return( list(all.dates = unique.dates, date.map = date.map))
}

# find location of given names in all names
#' find location of given names in all names
find.names <- function(find.names, all.names)
{
as.list(sapply(spl(find.names), function(x) {
Expand All @@ -93,7 +93,7 @@ find.names <- function(find.names, all.names)
}

###############################################################################
# Prepare backtest data
#' Prepare backtest data
#' @export
###############################################################################
bt.prep <- function
Expand Down Expand Up @@ -173,7 +173,7 @@ bt.prep <- function



# matrix form
#' matrix form
#' @export
bt.prep.matrix <- function
(
Expand Down Expand Up @@ -255,7 +255,7 @@ bt.prep.matrix.test <- function() {
}

###############################################################################
# Remove symbols from enviroment
#' Remove symbols from enviroment
#' @export
###############################################################################
bt.prep.remove.symbols.min.history <- function
Expand Down Expand Up @@ -306,7 +306,7 @@ bt.prep.trim <- function
}

###############################################################################
# Helper function to backtest for type='share'
#' Helper function to backtest for type='share'
#' @export
###############################################################################
bt.run.share <- function
Expand Down Expand Up @@ -353,32 +353,33 @@ bt.run.share <- function
}

###############################################################################
# Run backtest
#
# Inputs are assumed as if they were computed at point in time (i.e. no lags)
#
# For 'weight' back-test, the default action is to lage weights by one day,
# because weights are derived using all the information avalaible today,
# so we can only implement these weights tomorrow:
# portfolio.returns = lag(weights,1) * returns = weights * ( p / lag(p,1) - 1 )
# user can specify a different lag for weights, by changing the do.lag parameter.
#
# For example, for the end of the month strategy: if we open position at the close
# on the 30th, hold position on the 31st and sell it at the close on the 1st. If our
# weights have 0 on the 30th, 1 on the 31st, 1 on the 1st, and 0 on the 2nd, we
# can specify do.lag = 0 to get correct portfolio.returns
#
# Alternatively, if our weights have 0 on the 29th, 1 on the 30st, 1 on the 31st, and 0 on the 1nd, we
# can leave do.lag = 1 to get correct portfolio.returns
#
# For 'share' back-test, the portfolio returns:
# portfolio.returns = lag(shares,1) * ( p - lag(p,1) ) / ( lag(shares,1) * lag(p,1) )
#
###############################################################################
# some operators do not work well on xts
# weight[] = apply(coredata(weight), 2, ifna_prev)
#' Run backtest
#'
#' Inputs are assumed as if they were computed at point in time (i.e. no lags)
#'
#' For 'weight' back-test, the default action is to lage weights by one day,
#' because weights are derived using all the information avalaible today,
#' so we can only implement these weights tomorrow:
#'
#' portfolio.returns = lag(weights,1) * returns = weights * ( p / lag(p,1) - 1 )
#'
#' user can specify a different lag for weights, by changing the do.lag parameter.
#'
#' For example, for the end of the month strategy: if we open position at the close
#' on the 30th, hold position on the 31st and sell it at the close on the 1st. If our
#' weights have 0 on the 30th, 1 on the 31st, 1 on the 1st, and 0 on the 2nd, we
#' can specify do.lag = 0 to get correct portfolio.returns
#'
#' Alternatively, if our weights have 0 on the 29th, 1 on the 30st, 1 on the 31st, and 0 on the 1nd, we
#' can leave do.lag = 1 to get correct portfolio.returns
#'
#' For 'share' back-test, the portfolio returns:
#' portfolio.returns = lag(shares,1) * ( p - lag(p,1) ) / ( lag(shares,1) * lag(p,1) )
#'
#' Some operators do not work well on xts
#' weight[] = apply(coredata(weight), 2, ifna_prev)
#' @export
###############################################################################
##############################################################
bt.run <- function
(
b, # enviroment with symbols time series
Expand Down Expand Up @@ -472,7 +473,7 @@ bt.run <- function
}

###############################################################################
# Backtest summary
#' Backtest summary
#' @export
###############################################################################
bt.summary <- function
Expand Down Expand Up @@ -640,7 +641,7 @@ bt.summary.test <- function() {
}

###############################################################################
# Remove all leading NAs in model equity
#' Remove all leading NAs in model equity
#' @export
###############################################################################
bt.trim <- function
Expand Down Expand Up @@ -702,8 +703,8 @@ bt.trim.test <- function() {
}


# bt.run - really fast with no bells or whisles
# working directly with xts is alot slower, so use coredata
#' bt.run - really fast with no bells or whisles
#' working directly with xts is alot slower, so use coredata
#' @export
bt.run.weight.fast <- function
(
Expand Down Expand Up @@ -734,9 +735,9 @@ bt.run.weight.fast <- function
}

###############################################################################
# Portfolio turnover
# http://wiki.fool.com/Portfolio_turnover
# sales or purchases and dividing it by the average monthly value of the fund's assets
#' Portfolio turnover
#' http://wiki.fool.com/Portfolio_turnover
#' sales or purchases and dividing it by the average monthly value of the fund's assets
#' @export
###############################################################################
compute.turnover <- function
Expand Down Expand Up @@ -788,7 +789,7 @@ compute.turnover <- function


###############################################################################
# Compute Portfolio Maximum Deviation
#' Compute Portfolio Maximum Deviation
#' @export
###############################################################################
compute.max.deviation <- function
Expand All @@ -802,7 +803,7 @@ compute.max.deviation <- function
}

###############################################################################
# Backtest Trade summary
#' Backtest Trade summary
#' @export
###############################################################################
bt.trade.summary <- function
Expand Down Expand Up @@ -888,7 +889,7 @@ bt.trade.summary <- function
return(out)
}

# helper function
#' helper function
#' @export
bt.trade.summary.helper <- function(trades)
{
Expand Down Expand Up @@ -920,7 +921,7 @@ bt.trade.summary.helper <- function(trades)
}

###############################################################################
# Apply given function to bt enviroment
#' Apply given function to bt enviroment
#' @export
###############################################################################
bt.apply <- function
Expand Down Expand Up @@ -971,8 +972,8 @@ bt.apply.matrix <- function
}

###############################################################################
# Remove excessive signal
# http://www.amibroker.com/guide/afl/exrem.html
#' Remove excessive signal
#' http://www.amibroker.com/guide/afl/exrem.html
#' @export
###############################################################################
exrem <- function(x) {
Expand All @@ -995,7 +996,7 @@ bt.exrem <- function(weight)


###############################################################################
# Backtest Test function
#' Backtest Test function
###############################################################################
bt.test <- function()
{
Expand Down Expand Up @@ -1083,7 +1084,7 @@ dev.off()
###############################################################################
# Analytics Functions
###############################################################################
# CAGR - geometric return
#' CAGR - geometric return
#' @export
###############################################################################
compute.cagr <- function(equity)
Expand Down Expand Up @@ -1118,17 +1119,17 @@ compute.sharpe <- function(x)
return(sqrt(temp) * mean(x)/sd(x) )
}

# http://alumnus.caltech.edu/~amir/mdd-risk.pdf
# The Calmar Ratio is equal to the compounded annual growth rate divided by the maximum drawdown.
# The maximum drawdown is typically measured over a three year period.
# Calmar Ratio = CAGR / MAXDD
#' http://alumnus.caltech.edu/~amir/mdd-risk.pdf
#' The Calmar Ratio is equal to the compounded annual growth rate divided by the maximum drawdown.
#' The maximum drawdown is typically measured over a three year period.
#' Calmar Ratio = CAGR / MAXDD
#' @export
compute.calmar <- function(x)
{
compute.cagr(x) / compute.max.drawdown(x)
}

# R2 equals the square of the correlation coefficient
#' R2 equals the square of the correlation coefficient
#' @export
compute.R2 <- function(equity)
{
Expand All @@ -1138,8 +1139,8 @@ compute.R2 <- function(equity)
return( cor(y,x)^2 )
}

# http://cssanalytics.wordpress.com/2009/10/15/ft-portfolio-with-dynamic-hedging/
# DVR is the Sharpe Ratio times the R-squared of the equity curve
#' http://cssanalytics.wordpress.com/2009/10/15/ft-portfolio-with-dynamic-hedging/
#' DVR is the Sharpe Ratio times the R-squared of the equity curve
#' @export
compute.DVR <- function(bt)
{
Expand Down Expand Up @@ -1231,7 +1232,7 @@ else
}

###############################################################################
# Example to illustrate a simeple backtest
#' Example to illustrate a simeple backtest
#' @export
###############################################################################
bt.simple <- function(data, signal)
Expand Down
27 changes: 15 additions & 12 deletions R/strategy.r
Original file line number Diff line number Diff line change
Expand Up @@ -2785,13 +2785,14 @@ target.vol.strategy <- function(model, weight,


#*****************************************************************
# Calendar Strategy
#*****************************************************************
#signals = calendar.signal(key.date, 0, 1, 2, -1, -2)
#names(signals)
#signals = calendar.signal(key.date, T0=0, 1, 2, N1=-1, -2,P2N2=-2:2)
#names(signals)
# advanced ... - offsets
#' Calendar Strategy
#'
#' signals = calendar.signal(key.date, 0, 1, 2, -1, -2)
#' names(signals)
#' signals = calendar.signal(key.date, T0=0, 1, 2, N1=-1, -2,P2N2=-2:2)
#' names(signals)
#' advanced ... - offsets
#' @export
calendar.signal <- function(key.date, ...) {
offsets = list( ... )
if( is.list(offsets[[1]]) ) offsets = offsets[[1]]
Expand All @@ -2814,11 +2815,13 @@ target.vol.strategy <- function(model, weight,
signals
}

#models = calendar.strategy(data, signals, universe = universe)
#names(models)
#models = calendar.strategy(data, A=signals[[1]], signals[[1]])
#names(models)
# advanced ... - signals
#' Calendar Strategy
#' models = calendar.strategy(data, signals, universe = universe)
#' names(models)
#' models = calendar.strategy(data, A=signals[[1]], signals[[1]])
#' names(models)
#' advanced ... - signals
#' @export
calendar.strategy <- function(data, ..., universe = data$prices > 0, do.lag.universe = 1) {
signals = list( ... )
if( is.list(signals[[1]]) ) signals = signals[[1]]
Expand Down
Loading