I need a fast package for backtesting in R. I'm going to optimize a lot so I'll be running my strategies many millions of times.
I know about packages like quantstrat
or SIT
but they are terribly slow for my purposes, I have one strategy, I'm not going to model 20 portfolios at the same time and such.
Which package can you recommend?
UPD=========
Yes, I implemented something very simple like
signals <- sample(c(-1,0,1),30,replace = T)
-1
open sale
1
open buy
0
do nothing and close any position
prices <- cumsum(rnorm(30))+100
count_balance <- function(prices,signals){
p <- c(0,diff(prices))
s <- c(0,signals[-length(signals)])
return( cumsum(p*s) )
}
count_balance(p = prices,sig = signals)
or equivalent in Rcpp
even faster 30 times
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector count_balance_cpp(NumericVector prices, NumericVector signals) {
int n = prices.size();
NumericVector result(n);
result[0] = 0;
for (int i = 1; i < n; ++i) {
result[i] = result[i-1] + signals[i-1] * (prices[i] - prices[i-1]);
}
return result;
}
But I would like a little more
- take into account the commission
- have a trade log so that I know the ratio of profitable losing trades
In princepe I can implement this too, but I will not be completely sure that I did everything right, since I am a very bad programmer. I'm not even sure about the functions that I posted above)
That's why I was looking for a simple, fast, ready-made and most importantly proven library