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?


  • $\begingroup$ Take a look at the quantstrat package @alonch7 $\endgroup$
    – Rime
    Oct 6 '15 at 20:02
  • $\begingroup$ Hi Rime, I know quanstart but I find it difficult to use it with the output my model is making. $\endgroup$
    – alonch7
    Oct 7 '15 at 18:05
  • $\begingroup$ You can find much information on how to use the package. here is a link to Guy Yollin's page where you can download the code he uses to go over a strategy in quantstrat: r-programming.org/papers @alonch7 an alternative would be converting the b and s signals into 1 for long, -1 for short, and 0 for do nothing... and then multiplying them by the returns $\endgroup$
    – Rime
    Oct 7 '15 at 18:31
  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)

  • $\begingroup$ Hi bushmanov, can you be more specific where to look on SIT? $\endgroup$
    – alonch7
    Oct 9 '15 at 9:10
  • $\begingroup$ @alonch7 This is github repo, this is the blog with usage examples (continued here ) $\endgroup$ Oct 9 '15 at 11:09

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.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.