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)

| improve this answer | |
  • $\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$ – Sergey Bushmanov Oct 9 '15 at 11:09

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.