We shall travel there now, and find a way to create a series of permutations between two parameters. If this sounds crazy, well it just means you haven't been stress testing your favorite trading system, that's all. You know the system I'm talking about. The one where you place trades based on where the 50-day and 200-day moving averages are in relation to one another. You can backtest this strategy based on these simple rules, but the true curve-fitting fun starts when you backtest a whole bunch of parameters. For example, the 50-day and the 180-day. Or the 40-day and the 160-day. You get the idea.
I wrote one such program in R recently, but I must admit it was a bit ... yawn ... slow. It was actually painful to watch. I felt bad for R, really. Well not to worry, we can amp it up a bit with a little C program that takes the burden off R and busts out the parameters lickety-split. The first part of this process is to get the C code right. There is no better way than to fire up vi and start a typing.
$ vi parameter_cruncher.cThat opens up a new window where you can start typing in the following code. Remember that to actually type in vi you have to select the "i" key. That puts you in insert mode. Anyway, here is the code:
// takes 6 arguments
#include <stdio.h>
int main(int argc, char **argv)
{
int m = atoi(argv[1]);
int n = atoi(argv[2]);
int p = atoi(argv[3]);
int q = atoi(argv[4]);
int r = atoi(argv[5]);
int s = atoi(argv[6]);
int i;
int j;
for( i=m; i<n+1; i=i+p)
for( j=q; j<r+1; j=j+s)
printf("Parameter 1 is %d, Parameter 2 is %d\n", i, j);
return 0;
} Once you get that saved, you can compile it from command line with the following:
$ gcc parameter_cruncher.c -o parmThis makes our executable a bit more manageable to type. Then we type the following with 6 arguments (it's in the comments, but you can also count the variables that call the atoi (ASCII to Integer) function).
$ ./parm 10 50 10 150 250 50This reads as from 10 to 50 in steps of 10 and from 150 to 250 in steps of 50. Basic permutation stuff, nothing complicated really. I wrote this to test that the code actually does what I want it to do before I implement it by calling it from R.
This list isn't too long (can you calculate the correct permutation count in your head? quick!). Here it is below:
Parameter 1 is 10, Parameter 2 is 150
Parameter 1 is 10, Parameter 2 is 200
Parameter 1 is 10, Parameter 2 is 250
Parameter 1 is 20, Parameter 2 is 150
Parameter 1 is 20, Parameter 2 is 200
Parameter 1 is 20, Parameter 2 is 250
Parameter 1 is 30, Parameter 2 is 150
Parameter 1 is 30, Parameter 2 is 200
Parameter 1 is 30, Parameter 2 is 250
Parameter 1 is 40, Parameter 2 is 150
Parameter 1 is 40, Parameter 2 is 200
Parameter 1 is 40, Parameter 2 is 250
Parameter 1 is 50, Parameter 2 is 150
Parameter 1 is 50, Parameter 2 is 200
Parameter 1 is 50, Parameter 2 is 250
0 comments:
Post a Comment