Is there a good backtesting package in R?

Quantitative Finance Asked by alonch7 on October 17, 2020

My model exports a vector that have for each day b-buy s-sell or h- hold
it’s look like this:

sig [1] b b s s b b b s s b s b s s b s b s s s s b b s s b b b b b
b s b b b b b b b

I want to backtest that it will buy or sell all the equity in the portfolio at the end of each day and for hold will do nothing.
what is the best way to backtest in R or other method this strategy?


3 Answers

To add another possibility: Here is how such a model could be run in the PMwR package (which I maintain). It seems that sig holds the desired position. Suppose we have a time series of prices P.

sig <- c("b", "b", "s", "s", "b", "b", "b", "s",
         "s", "b", "s", "b", "s", "s", "b", "s",
         "b", "s", "s", "s", "s", "b", "b", "s",
         "s", "b", "b", "b", "b", "b", "b", "s",
         "b", "b", "b", "b", "b", "b", "b")

P <- cumsum(sample(c(1, -1), replace = TRUE, size = length(sig))) + 100

Then with PMwR::btest, the backtest could be run as follows:

signal <- function(sig)
    switch(sig[Time()], "b" = 1, "s" = 0, NULL)

bt <- btest(P, signal, sig = sig)
## initial wealth 0  =>  final wealth  10 

The signal function maps sig at every point in time to a position of either 1 or 0. The result, stored in bt, is a list that stores the details of the backtest.

##     instrument  timestamp  amount  price
## 1      asset 1          2       1    100
## 2      asset 1          4      -1    100
## 3      asset 1          6       1     98
## 4      asset 1          9      -1    101
## 5      asset 1         11       1    101
## [....]

## etc.

Answered by Enrico Schumann on October 17, 2020

With the following packages I think you have enough tools to develop a backtest:

  • quantmod
  • PerformanceAnalytics
  • xts

I prefer to understand what's happening rather than have all the complexity abstracted away. There are multiple examples in Joshua Ulrich's blog on how to develop a backtest, that should be enough to get started. I've personally also found useful using data.table in combination with these packages.

Answered by marbel on October 17, 2020

  1. In R, there are basically two packages to backtest your strategy: SIT and quantstrat. I personally prefer the former because it's much faster and more transparent in terms of how your positions are managed. In addition, SIT gives your more flexibility in how your trading signals are formed.
  2. If you have a very basic strategy, like long/short/stay on the sidelines, perhaps the best approach to quickly test your strategy is to have 2 vectors like @Rime advised above: one for returns and the other for your positions (either 1, -1, or 0), and multiply them to get returns for your positions when you are in the market (either short or long). Two pieces of advice if I may:

    • Shift your action (buy or sell) one period forward relative to the signal.

    • Short position. Think carefully how you would accumulate profits over multiple periods. If you happen to short a stock which had lost 10% each day for 3 days in a row (i.e. from 100 to 90 on first day, to 81 on second, and to 72.9 on the third day) that wouldn't make you wealthier by 33.1% (1.1^3 -1), as if it were for positive returns on the long side. Your return would rather be 27.1% which you would not get by simply multiplying returns and positions... (more at this blogpost at SIT)

Answered by Sergey Bushmanov on October 17, 2020

Add your own answers!

Related Questions

PDs for negative credit spreads

1  Asked on November 15, 2021 by cettt


Low volatility in factor regression

1  Asked on November 13, 2021 by circus_beta


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP