Title: | Generic Framework to Analyze Trading Strategies |
---|---|
Description: | Users can build and test customized quantitative trading strategies. Some quantitative trading strategies are already implemented, e.g. various moving-average filters with trend following approaches. The implemented class called "Strategy" allows users to access several methods to analyze performance figures, plots and backtest the strategies. Furthermore, custom strategies can be added, a generic template is available. The custom strategies require a certain input and output so they can be called from the Strategy-constructor. |
Authors: | Julian Busch |
Maintainer: | Julian Busch <[email protected]> |
License: | GPL |
Version: | 1.0.1 |
Built: | 2024-10-27 03:33:39 UTC |
Source: | https://github.com/cran/Strategy |
The dataset contains the price data (not returns!), each starting at a value of 100. The dates are randomly recreated by choosing the latest date as Sys.Date() going backwards on a daily basis per row.
assets
assets
An xts-object with 1000 rows and 10 variables:
Column with price data of a random walk called asset1.
Column with price data of a random walk called asset2.
...
Walk forward analysis backtest with the specified parameters on an object of class Strategy
. The backtest calibrates the parameters according to the specification given by the user (in-sample) and returns the trading signals for the following period (out-of-sample). This is iteratively repeated on a shifting time window. Computer performance is critical with this function.
backtest(object, horizon = "6m", data.width = "24m", keep.history = F, optim.param = NULL, optim.param.min = 1, optim.param.max = 10, optim.param.scale = 0.1, from = NULL, until = NULL, which = NULL, rf = 0, printSteps = F) ## S4 method for signature 'Strategy' backtest(object, horizon = "6m", data.width = "24m", keep.history = F, optim.param = NULL, optim.param.min = 1, optim.param.max = 10, optim.param.scale = 0.1, from = NULL, until = NULL, which = NULL, rf = 0, printSteps = F)
backtest(object, horizon = "6m", data.width = "24m", keep.history = F, optim.param = NULL, optim.param.min = 1, optim.param.max = 10, optim.param.scale = 0.1, from = NULL, until = NULL, which = NULL, rf = 0, printSteps = F) ## S4 method for signature 'Strategy' backtest(object, horizon = "6m", data.width = "24m", keep.history = F, optim.param = NULL, optim.param.min = 1, optim.param.max = 10, optim.param.scale = 0.1, from = NULL, until = NULL, which = NULL, rf = 0, printSteps = F)
object |
An object of class |
horizon |
The out-of-sample period length. |
data.width |
The in-sample period length used for calibration. |
keep.history |
If set to |
optim.param |
A character vector providing the names of the parameters to be calibrated. Parameters that are not provided will be kept fix. |
optim.param.min |
A numeric vector providing the minimum values of the parameters that are calibrated. |
optim.param.max |
A numeric vector providing the maximum values of the parameters that are calibrated. |
optim.param.scale |
A numeric vector providing the scaling of the parameters that are calibrated. It is advisable to set scaling of the parameters to the smallest unit that makes sense. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in backtest |
rf |
Risk free rate in decimal, e.g. |
printSteps |
This is a feature used mainly for debugging the constructor function in order to localize where unspecified errors occur. If set to true, the different steps run within the constructor is printed to the console. |
##Not run: # MA(200)-Strategy params <- list(k=20) # reduce dataset due to computation time assets_r <- assets[tail(zoo::index(assets),100)] myStrat.MA <- Strategy(assets=assets_r, strat="MA", strat.params=params) # Perform backtest on MA(20)-Strategy with # out-of-sample periods of 2 months # and in-sample-calibration of 2 months # This example requires a lot of computation time, # so this is only performed for 1 asset and high scaling. backtest(myStrat.MA, horizon="2m", data.width="2m" , optim.param="k", optim.param.min=5, optim.param.max=10 , optim.param.scale=5, printSteps = TRUE, which=1) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=20) # reduce dataset due to computation time assets_r <- assets[tail(zoo::index(assets),100)] myStrat.MA <- Strategy(assets=assets_r, strat="MA", strat.params=params) # Perform backtest on MA(20)-Strategy with # out-of-sample periods of 2 months # and in-sample-calibration of 2 months # This example requires a lot of computation time, # so this is only performed for 1 asset and high scaling. backtest(myStrat.MA, horizon="2m", data.width="2m" , optim.param="k", optim.param.min=5, optim.param.max=10 , optim.param.scale=5, printSteps = TRUE, which=1) ##End(Not run)
Strategy
-objects.Compare the portfolio performance indicators of an arbitrary number of objects of class Strategy
.
compare(..., from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.costs=TRUE , use.backtest=FALSE, include.params=FALSE) ## S4 method for signature 'Strategy' compare(..., from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.costs = TRUE, use.backtest = FALSE, include.params = FALSE)
compare(..., from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.costs=TRUE , use.backtest=FALSE, include.params=FALSE) ## S4 method for signature 'Strategy' compare(..., from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.costs = TRUE, use.backtest = FALSE, include.params = FALSE)
... |
Objects of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
scaling.periods |
Vector with annualization factors for calculation. Default is 252, 52, 12, 4, 1 for daily, weekly, monthly, quarterly and yearly data respectively. |
include.costs |
If |
use.backtest |
If |
include.params |
If |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # EWMA(0.05)-Strategy params <- list(lambda=0.05) myStrat.EWMA <- Strategy(assets=assets, strat="EWMA", strat.params=params) # Compare annualized performance of MA(200) and EWMA(0.05) # compare(myStrat.MA, myStrat.EWMA, use.backtest=TRUE, scaling.periods=252) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # EWMA(0.05)-Strategy params <- list(lambda=0.05) myStrat.EWMA <- Strategy(assets=assets, strat="EWMA", strat.params=params) # Compare annualized performance of MA(200) and EWMA(0.05) # compare(myStrat.MA, myStrat.EWMA, use.backtest=TRUE, scaling.periods=252) ##End(Not run)
Expected Shortfall of the assets or portfolio of an object of class Strategy
.
ES(object, alpha=0.05, V=1 , type="normal.distribution", method="full" , of="portfolio", from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' ES(object, alpha = 0.05, V = 1, type = c("normal.distribution", "historical"), method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
ES(object, alpha=0.05, V=1 , type="normal.distribution", method="full" , of="portfolio", from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' ES(object, alpha = 0.05, V = 1, type = c("normal.distribution", "historical"), method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
alpha |
The significance level |
V |
Volume that is invested. The linear factor for the ES calculation. Either a single value for portfolio or a vector for each asset. |
type |
Type of ES calculation. Use |
method |
Method of loss calculation. Use |
of |
ES to be calculated for assets separately or the portfolio. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
scaling.periods |
Vector with annualization factors for calculation. Default is 252, 52, 12, 4, 1 for daily, weekly, monthly, quarterly and yearly data respectively. |
include.weights |
Only relevant if |
include.costs |
If |
use.backtest |
If |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get ES of MA(200)-Strategy portfolio ES(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest ES of MA(200)-Strategy (backtest would need to be executed first!) # ES(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get ES of MA(200)-Strategy portfolio ES(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest ES of MA(200)-Strategy (backtest would need to be executed first!) # ES(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
Strategy
-objectGets the backtest parameter values of an object of class Strategy
that were used for backtesting the strategy. This includes the information about the parameters,
getBacktestSetup(object) ## S4 method for signature 'Strategy' getBacktestSetup(object)
getBacktestSetup(object) ## S4 method for signature 'Strategy' getBacktestSetup(object)
object |
An object of class |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get backtest setup from MA(200)-Strategy getBacktestSetup(myStrat.MA) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get backtest setup from MA(200)-Strategy getBacktestSetup(myStrat.MA) ##End(Not run)
Strategy
-objectReturns the fixed and relative trading costs of an object of class Strategy
..
getCosts(object) ## S4 method for signature 'Strategy' getCosts(object)
getCosts(object) ## S4 method for signature 'Strategy' getCosts(object)
object |
An object of class |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function from MA(200)-Strategy MA.costs <- getCosts(myStrat.MA) # return fix costs MA.costs$fix # return relative costs MA.costs$relative ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function from MA(200)-Strategy MA.costs <- getCosts(myStrat.MA) # return fix costs MA.costs$fix # return relative costs MA.costs$relative ##End(Not run)
Strategy
-objectGets the strategy values of an object of class Strategy
that were output from strategy calculation.
getFilters(object, which = NULL) ## S4 method for signature 'Strategy' getFilters(object, which = NULL)
getFilters(object, which = NULL) ## S4 method for signature 'Strategy' getFilters(object, which = NULL)
object |
An object of class |
which |
Which filters shall be returned. Either list number or names to be passed. |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy values from MA(200)-Strategy getFilters(myStrat.MA) # all strategy values returned ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy values from MA(200)-Strategy getFilters(myStrat.MA) # all strategy values returned ##End(Not run)
Strategy
-objectGets the indicators data of an object of class Strategy
that was used within strategy calculation.
getIndicators(object, from = NULL, until = NULL, which = NULL) ## S4 method for signature 'Strategy' getIndicators(object, from = NULL, until = NULL, which = NULL)
getIndicators(object, from = NULL, until = NULL, which = NULL) ## S4 method for signature 'Strategy' getIndicators(object, from = NULL, until = NULL, which = NULL)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or list-number of indicators that should be included. If |
##Not run: # MA(200)-Strategy params <- list(k=200) randreturns <- xts::xts(rnorm(nrow(assets)), order.by= seq(from=Sys.Date()-nrow(assets)+1, to=Sys.Date(), by="d")) indicators <- list(returns=randreturns) # example: random returns myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params, indicators=indicators) # Get indicator data from MA(200)-Strategy getIndicators(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) randreturns <- xts::xts(rnorm(nrow(assets)), order.by= seq(from=Sys.Date()-nrow(assets)+1, to=Sys.Date(), by="d")) indicators <- list(returns=randreturns) # example: random returns myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params, indicators=indicators) # Get indicator data from MA(200)-Strategy getIndicators(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
Strategy
-objectGets the strategy function parameters of an object of class Strategy
that were used for strategy calculation.
getParameters(object, use.backtest = FALSE) ## S4 method for signature 'Strategy' getParameters(object, use.backtest = FALSE)
getParameters(object, use.backtest = FALSE) ## S4 method for signature 'Strategy' getParameters(object, use.backtest = FALSE)
object |
An object of class |
use.backtest |
If set to |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get parameters from MA(200)-Strategy getParameters(myStrat.MA) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get parameters from MA(200)-Strategy getParameters(myStrat.MA) ##End(Not run)
Strategy
-objectGets the price data of an object of class Strategy
that was used within strategy calculation.
getPrices(object, from = NULL, until = NULL, which = NULL) ## S4 method for signature 'Strategy' getPrices(object, from = NULL, until = NULL, which = NULL)
getPrices(object, from = NULL, until = NULL, which = NULL) ## S4 method for signature 'Strategy' getPrices(object, from = NULL, until = NULL, which = NULL)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or column-number of assets that should be included. If |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get price data from MA(200)-Strategy getPrices(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get price data from MA(200)-Strategy getPrices(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
Strategy
-objectGets the trading signals of an object of class Strategy
that were output from strategy calculation.
getSignals(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE) ## S4 method for signature 'Strategy' getSignals(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE)
getSignals(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE) ## S4 method for signature 'Strategy' getSignals(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or column-number of assets that should be returned. If |
use.backtest |
If set to |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get signals from MA(200)-Strategy # all signals returned getSignals(myStrat.MA) # backtest signals for first two assets returned # getSignals(myStrat.MA, which=c(1,2), use.backtest=TRUE) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get signals from MA(200)-Strategy # all signals returned getSignals(myStrat.MA) # backtest signals for first two assets returned # getSignals(myStrat.MA, which=c(1,2), use.backtest=TRUE) ##End(Not run)
Strategy
-objectGets the strategy function of an object of class Strategy
that was used for strategy calculation.
getStratFUN(object) ## S4 method for signature 'Strategy' getStratFUN(object)
getStratFUN(object) ## S4 method for signature 'Strategy' getStratFUN(object)
object |
An object of class |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function from MA(200)-Strategy MA.FUN <- getStratFUN(myStrat.MA) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function from MA(200)-Strategy MA.FUN <- getStratFUN(myStrat.MA) ##End(Not run)
Strategy
-objectGets the strategy function name of an object of class Strategy
that was used for strategy calculation. This function is for aesthetic purposes only and does not have any numerical relevance.
getStratName(object, include.params = FALSE) ## S4 method for signature 'Strategy' getStratName(object, include.params = FALSE)
getStratName(object, include.params = FALSE) ## S4 method for signature 'Strategy' getStratName(object, include.params = FALSE)
object |
An object of class |
include.params |
If set to |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function name from MA(200)-Strategy getStratName(myStrat.MA) # returns "MA" getStratName(myStrat.MA, include.params=TRUE) # returns "MA(200)" ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get strategy function name from MA(200)-Strategy getStratName(myStrat.MA) # returns "MA" getStratName(myStrat.MA, include.params=TRUE) # returns "MA(200)" ##End(Not run)
Strategy
-objectGets the trades of an object of class Strategy
that were performed within strategy calculation.
getTrades(object, from = NULL, until = NULL, which = NULL, of = "signals", use.backtest = FALSE) ## S4 method for signature 'Strategy' getTrades(object, from = NULL, until = NULL, which = NULL, of = c("signals", "weights"), use.backtest = FALSE)
getTrades(object, from = NULL, until = NULL, which = NULL, of = "signals", use.backtest = FALSE) ## S4 method for signature 'Strategy' getTrades(object, from = NULL, until = NULL, which = NULL, of = c("signals", "weights"), use.backtest = FALSE)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or column-number of assets that should be included. If |
of |
Trades to be calculated on basis of trading |
use.backtest |
If set to |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get price data from MA(200)-Strategy getTrades(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get price data from MA(200)-Strategy getTrades(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
Strategy
-objectGets the weights data of an object of class Strategy
that was used within strategy calculation.
getWeights(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE) ## S4 method for signature 'Strategy' getWeights(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE)
getWeights(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE) ## S4 method for signature 'Strategy' getWeights(object, from = NULL, until = NULL, which = NULL, use.backtest = FALSE)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or column-number of assets that should be included. If |
use.backtest |
If set to |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get weights data from MA(200)-Strategy getWeights(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get weights data from MA(200)-Strategy getWeights(myStrat.MA, from="2015-01-01", until="2015-12-31") ##End(Not run)
Gets the hitratio of the signals of an object of class Strategy
.
hitratio(object, of="portfolio" , from=NULL, until=NULL, which=NULL , type="per.signal", include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' hitratio(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, type = c("per.signal", "per.trade"), include.costs = TRUE, use.backtest = FALSE)
hitratio(object, of="portfolio" , from=NULL, until=NULL, which=NULL , type="per.signal", include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' hitratio(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, type = c("per.signal", "per.trade"), include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
of |
Hit Ratio to be calculated for assets separately or the portfolio (weighted hit ratios according to average asset weights). |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
type |
If the hitratio shall be calculated per trade with |
include.costs |
If |
use.backtest |
If set to |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get hit ratio of MA(200)-Strategy portfolio hitratio(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get hit ratio of MA(200)-Strategy (daily data = 252 trading days) # hitratio(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get hit ratio of MA(200)-Strategy portfolio hitratio(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get hit ratio of MA(200)-Strategy (daily data = 252 trading days) # hitratio(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)
Losses over time of an assets or portfolio of an object of class Strategy
.
loss(object, V=100, method="full", of="portfolio" , from=NULL, until=NULL, which=NULL , include.weights=TRUE, include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' loss(object, V = 100, method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
loss(object, V=100, method="full", of="portfolio" , from=NULL, until=NULL, which=NULL , include.weights=TRUE, include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' loss(object, V = 100, method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
V |
Volume that is invested. The linear factor for the VaR calculation. Either a single value for portfolio or a vector for each asset. |
method |
Method of loss calculation. Use |
of |
Losses to be calculated for assets separately or the portfolio. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
include.weights |
Only relevant if |
include.costs |
If |
use.backtest |
If |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get VaR of MA(200)-Strategy portfolio myStrat.MA.losses <- loss(myStrat.MA, from="2015-01-01", until="2015-12-31") ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get VaR of MA(200)-Strategy portfolio myStrat.MA.losses <- loss(myStrat.MA, from="2015-01-01", until="2015-12-31") ## End(Not run)
Gets the maximum drawdown of the performance of an object of class Strategy
.
MDD(object, of="portfolio" , from=NULL, until=NULL, which=NULL , type="relative", include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' MDD(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, type = c("absolute", "relative"), include.costs = TRUE, use.backtest = FALSE)
MDD(object, of="portfolio" , from=NULL, until=NULL, which=NULL , type="relative", include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' MDD(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, type = c("absolute", "relative"), include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
of |
Maximum Drawdown to be calculated for assets separately or the portfolio. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
type |
If the |
include.costs |
If |
use.backtest |
If set to |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get MDD of MA(200)-Strategy portfolio MDD(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get MDD of MA(200)-Strategy (daily data = 252 trading days) # MDD(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get MDD of MA(200)-Strategy portfolio MDD(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get MDD of MA(200)-Strategy (daily data = 252 trading days) # MDD(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)
Creates a strategy function template file. This file can be used as template for the development of customized strategies.
newStrategyFunction(name = NULL, file.path = getwd(), overwrite = FALSE)
newStrategyFunction(name = NULL, file.path = getwd(), overwrite = FALSE)
name |
String as name of the new function (without spaces). |
file.path |
Valid file path of existing directory where the new function shall be stored in format file.path/name.R. |
overwrite |
If the strategy file already exists, it will be overwritten if value is |
##Not run: # Creates a file myNewStrat.R at the specific file path newStrategyFunction(name="myNewStrat", file.path=getwd(), overwrite=T) ##End(Not run)
##Not run: # Creates a file myNewStrat.R at the specific file path newStrategyFunction(name="myNewStrat", file.path=getwd(), overwrite=T) ##End(Not run)
Gets the performance of an object of class Strategy
.
performance(object, of = "portfolio", type = "performance", from = NULL, until = NULL, which = NULL, use.backtest = FALSE, include.costs = TRUE) ## S4 method for signature 'Strategy' performance(object, of = c("portfolio", "assets"), type = c("performance", "logReturns", "returns"), from = NULL, until = NULL, which = NULL, use.backtest = FALSE, include.costs = TRUE)
performance(object, of = "portfolio", type = "performance", from = NULL, until = NULL, which = NULL, use.backtest = FALSE, include.costs = TRUE) ## S4 method for signature 'Strategy' performance(object, of = c("portfolio", "assets"), type = c("performance", "logReturns", "returns"), from = NULL, until = NULL, which = NULL, use.backtest = FALSE, include.costs = TRUE)
object |
An object of class |
of |
Performance to be extracted from assets separately or the portfolio performance. |
type |
Which type of performance shall be returned. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in performance. If a portfolio performance from only a subset of the assets is calculated, the weights are scaled accordingly. |
use.backtest |
If |
include.costs |
If |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get performance of MA(200)-Strategy performance(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest performance of MA(200)-Strategy # performance(myStrat.MA, from="2015-01-01", until="2015-12-31" # , use.backtest=TRUE, type="logReturns") ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get performance of MA(200)-Strategy performance(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest performance of MA(200)-Strategy # performance(myStrat.MA, from="2015-01-01", until="2015-12-31" # , use.backtest=TRUE, type="logReturns") ## End(Not run)
Get a list of the performance indicators of an object of class Strategy
.
performanceIndicators(object, of="portfolio" , from=NULL, until=NULL, which=NULL, alpha=0.05 , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' performanceIndicators(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, alpha = 0.05, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
performanceIndicators(object, of="portfolio" , from=NULL, until=NULL, which=NULL, alpha=0.05 , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' performanceIndicators(object, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, alpha = 0.05, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
of |
Indicators to be calculated for assets separately or the portfolio. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
alpha |
The significance level |
scaling.periods |
Vector with annualization factors for calculation. Default is 252, 52, 12, 4, 1 for daily, weekly, monthly, quarterly and yearly data respectively. |
include.weights |
Only relevant if |
include.costs |
If |
use.backtest |
If set to |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get performance indicators of MA(200)-Strategy assets performanceIndicators(myStrat.MA, from="2015-01-01", until="2015-12-31") ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get performance indicators of MA(200)-Strategy assets performanceIndicators(myStrat.MA, from="2015-01-01", until="2015-12-31") ## End(Not run)
Strategy
-objectCalls a generic plot
function that can plot the data of any Strategy
-object. If a plotFUN
-function is given within the object, this user-defined function will be used. The generic function plots 3 parts:
Price area Plots the asset price data and filters.
Indicator area Plots the indicators and trading signals.
Performance area Plots performance of the strategy.
## S3 method for class 'Strategy' plot(x, y, from=NULL, until=NULL , which.assets=NULL, which.filters=NULL, which.indicators=NULL , main=NULL, show.signals=TRUE, include.costs=TRUE, ...)
## S3 method for class 'Strategy' plot(x, y, from=NULL, until=NULL , which.assets=NULL, which.filters=NULL, which.indicators=NULL , main=NULL, show.signals=TRUE, include.costs=TRUE, ...)
x |
An object of class |
y |
Standard plot argument that is not relevant for Strategy objects! |
from |
From date that chart is to be plotted. |
until |
Until date that chart is to be plotted. |
which.assets |
Which assets shall be plotted (each one will result in single plot) |
which.filters |
Which filters shall be added to price plot. Default value |
which.indicators |
Which indicators shall be added to indicator plot. Default value |
main |
The main title of the plot. |
show.signals |
If |
include.costs |
If |
... |
Further arguments passed to custom plotFUN (if available) of the object (x). |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot first asset of MA(200)-Strategy plot(myStrat.MA, from="2015-01-01", until="2015-12-31", which.assets=1) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot first asset of MA(200)-Strategy plot(myStrat.MA, from="2015-01-01", until="2015-12-31", which.assets=1) ##End(Not run)
Plots drawdowns of the performance of an object of class Strategy
.
plotDrawdowns(object, from = NULL, until = NULL, which = NULL, of = "portfolio", type = "relative", include.costs = TRUE, use.backtest = FALSE, returnValues = FALSE, ...) ## S4 method for signature 'Strategy' plotDrawdowns(object, from = NULL, until = NULL, which = NULL, of = c("portfolio", "assets"), type = c("relative", "absolute"), include.costs = TRUE, use.backtest = FALSE, returnValues = FALSE, ...)
plotDrawdowns(object, from = NULL, until = NULL, which = NULL, of = "portfolio", type = "relative", include.costs = TRUE, use.backtest = FALSE, returnValues = FALSE, ...) ## S4 method for signature 'Strategy' plotDrawdowns(object, from = NULL, until = NULL, which = NULL, of = c("portfolio", "assets"), type = c("relative", "absolute"), include.costs = TRUE, use.backtest = FALSE, returnValues = FALSE, ...)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in performance. If a portfolio performance from only a subset of the assets is calculated, the weights are scaled accordingly. |
of |
Performance to be extracted from assets separately or the portfolio performance. |
type |
If the |
include.costs |
If |
use.backtest |
If |
returnValues |
If |
... |
Further arguments that can be passed to the underlying plot()-function. |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy drawdowns plotDrawdowns(myStrat.MA, from="2015-01-01", until="2015-12-31") # Plot backtested MA(200)-Strategy drawdowns # plotDrawdowns(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy drawdowns plotDrawdowns(myStrat.MA, from="2015-01-01", until="2015-12-31") # Plot backtested MA(200)-Strategy drawdowns # plotDrawdowns(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
Plots performance of an object of class Strategy
.
plotPerformance(object, which = NULL, of = "portfolio", from = NULL, until = NULL, use.backtest = FALSE, include.costs = TRUE, plot.params = TRUE, plot.params.names = NULL, plot.params.first = TRUE, ...) ## S4 method for signature 'Strategy' plotPerformance(object, which = NULL, of = c("portfolio", "assets"), from = NULL, until = NULL, use.backtest = FALSE, include.costs = TRUE, plot.params = TRUE, plot.params.names = NULL, plot.params.first = TRUE, ...)
plotPerformance(object, which = NULL, of = "portfolio", from = NULL, until = NULL, use.backtest = FALSE, include.costs = TRUE, plot.params = TRUE, plot.params.names = NULL, plot.params.first = TRUE, ...) ## S4 method for signature 'Strategy' plotPerformance(object, which = NULL, of = c("portfolio", "assets"), from = NULL, until = NULL, use.backtest = FALSE, include.costs = TRUE, plot.params = TRUE, plot.params.names = NULL, plot.params.first = TRUE, ...)
object |
An object of class |
which |
Names or number of assets that should be included in performance. If a portfolio performance from only a subset of the assets is calculated, the weights are scaled accordingly. |
of |
Performance to be extracted from assets separately or the portfolio performance. |
from |
The date in character format |
until |
The date in character format |
use.backtest |
If |
include.costs |
If |
plot.params |
If set to TRUE, the parameters used for the performance periods are plotted into the chart. Requires that use.backtest is set to |
plot.params.names |
New parameter names to be shown can be supplied. Requires that use.backtest is set to |
plot.params.first |
If |
... |
Further arguments that can be passed to the underlying plot()-function. |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy plotPerformance(myStrat.MA, from="2015-01-01", until="2015-12-31") # Plot backtested MA(200)-Strategy # plotPerformance(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy plotPerformance(myStrat.MA, from="2015-01-01", until="2015-12-31") # Plot backtested MA(200)-Strategy # plotPerformance(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ##End(Not run)
Plots the weights of the portfolio of an object of class Strategy
.
plotWeights(object, from = NULL, until = NULL, ...) ## S4 method for signature 'Strategy' plotWeights(object, from = NULL, until = NULL, ...)
plotWeights(object, from = NULL, until = NULL, ...) ## S4 method for signature 'Strategy' plotWeights(object, from = NULL, until = NULL, ...)
object |
An object of class |
from |
The date in character format |
until |
The date in character format |
... |
Currently not active. |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy weights plotWeights(myStrat.MA) ##End(Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Plot MA(200)-Strategy weights plotWeights(myStrat.MA) ##End(Not run)
Get the sharpe ratio of the performance of an object of class Strategy
.
sharpe(object, rf=0, of="portfolio" , from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' sharpe(object, rf = 0, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.costs = TRUE, use.backtest = FALSE)
sharpe(object, rf=0, of="portfolio" , from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.costs=TRUE , use.backtest=FALSE) ## S4 method for signature 'Strategy' sharpe(object, rf = 0, of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
rf |
Risk free rate in decimal, e.g. |
of |
Sharpe ratio to be calculated for assets separately or the portfolio sharpe. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
scaling.periods |
Vector with annualization factors for sharpe ratio calculation. Default is 252, 52, 12, 4, 1 for daily, weekly, monthly, quarterly and yearly data respectively. |
include.costs |
If |
use.backtest |
If |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get sharpe of MA(200)-Strategy portfolio sharpe(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest annualized sharpe of MA(200)-Strategy (daily data = 252 trading days) # sharpe(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE, scaling.periods=252) ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get sharpe of MA(200)-Strategy portfolio sharpe(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest annualized sharpe of MA(200)-Strategy (daily data = 252 trading days) # sharpe(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE, scaling.periods=252) ## End(Not run)
Creates an object of class Strategy
with the given portfolio data and strategy-function.
Strategy(assets, strat = "buyhold" , assetValueType = c("price", "logReturn"), weights = NULL, indicators = list() , strat.params = list(), volume = 1000000 , costs.fix = 0, costs.rel = 0 , printSteps = FALSE)
Strategy(assets, strat = "buyhold" , assetValueType = c("price", "logReturn"), weights = NULL, indicators = list() , strat.params = list(), volume = 1000000 , costs.fix = 0, costs.rel = 0 , printSteps = FALSE)
assets |
Time series of class |
strat |
The name of the strategy that should be applied. This can be either a predefined strategy like MA or EWMA or a self-written function in which case the full path to the function file to be called must be supplied. |
assetValueType |
Assets can be passed as prices or log returns. In order to identify the asset value types, either one of the types has to be selected. |
weights |
The portfolio weights for the given assets as time series (dynamic) or numerical (constant) weights. |
indicators |
A list of indicators that might be used within customized strategies. It is recommended to pass a named list. |
strat.params |
The list of parameters and their values required by the strategy function selected with parameter strat. |
volume |
Portfolio volume for trading. Default value is 1 Million. |
costs.fix |
The fix trading costs per trade. |
costs.rel |
The trading costs, relative to the volume. I.e. a value of |
printSteps |
This is a feature used mainly for debugging the constructor function in order to localize where unspecified errors occur. If set to true, the different steps run within the constructor is printed to the console. |
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Own MA-strategy-function # myStrat.MA <- Strategy(assets=assets, strat="C:/MA_function.R") ##End (Not run)
##Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Own MA-strategy-function # myStrat.MA <- Strategy(assets=assets, strat="C:/MA_function.R") ##End (Not run)
Strategy
-ClassAn S4 class to store quantitative strategies and compute various performance figures.
prices
Price data of the assets. If return data was given within the constructor, starting at 100.
weights
Time series of class xts
indicating row wise weights of the assets.
indicators
List of indicators of class xts
.
strat
Name of the strategy function to be called. Could be a full file path to a custom strategy.
strat.params
List of parameters as input for the strategy function. List entry names should match parameter names.
stratFUN
Contains the custom strategy function or NULL
.
plotFUN
Contains the custom strategy function or NULL
.
filters
List with filtered price data (e.g. MA(200)-data).
signals
Time series with trading signals of class xts
.
backtest.signals
Time series with trading signals of the backtest of class xts
.
backtest.parameters
List of parameters of the backtest.
backtest.setup
Matrix showing the backtest preferences.
volume
Numeric vector indicating the initial investment volume per asset.
costs.fix
Numeric vector indicating the fixed costs per trade per asset.
costs.rel
Numeric vector indicating the relative costs per trade per asset.
Value at Risk of the assets or portfolio of an object of class Strategy
.
VaR(object, alpha=0.05, V=1, type="normal.distribution" , method="full", of="portfolio" , from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' VaR(object, alpha = 0.05, V = 1, type = c("normal.distribution", "historical"), method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
VaR(object, alpha=0.05, V=1, type="normal.distribution" , method="full", of="portfolio" , from=NULL, until=NULL, which=NULL , scaling.periods=NULL, include.weights=TRUE , include.costs=TRUE, use.backtest=FALSE) ## S4 method for signature 'Strategy' VaR(object, alpha = 0.05, V = 1, type = c("normal.distribution", "historical"), method = c("full", "linear"), of = c("portfolio", "assets"), from = NULL, until = NULL, which = NULL, scaling.periods = NULL, include.weights = TRUE, include.costs = TRUE, use.backtest = FALSE)
object |
An object of class |
alpha |
The significance level |
V |
Volume that is invested. The linear factor for the VaR calculation. Either a single value for portfolio or a vector for each asset. |
type |
Type of VaR calculation. Use |
method |
Method of loss calculation. Use |
of |
VaR to be calculated for assets separately or the portfolio. |
from |
The date in character format |
until |
The date in character format |
which |
Names or number of assets that should be included in calculation. |
scaling.periods |
Vector with annualization factors for calculation. Default is 252, 52, 12, 4, 1 for daily, weekly, monthly, quarterly and yearly data respectively. |
include.weights |
Only relevant if |
include.costs |
If |
use.backtest |
If |
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get VaR of MA(200)-Strategy portfolio VaR(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest VaR of MA(200)-Strategy # VaR(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)
## Not run: # MA(200)-Strategy params <- list(k=200) myStrat.MA <- Strategy(assets=assets, strat="MA", strat.params=params) # Get VaR of MA(200)-Strategy portfolio VaR(myStrat.MA, from="2015-01-01", until="2015-12-31") # Get backtest VaR of MA(200)-Strategy # VaR(myStrat.MA, from="2015-01-01", until="2015-12-31", use.backtest=TRUE) ## End(Not run)