# Code to test combination of conditions

Dear fellows,

I wrote the following code to use and test various conditions. This is the concept: I will run the strategy tester from 1 to 2^10 (because I have 10 conditions, and at each test one condition can be used or can be not used).

In order to combine the conditions in all combinations possible I have written the following code, which is compiling fine.

Each condition has its own function, lets say, condition_1 is crossing of moving averages; condition_2 is today's volume greater than volumes of last 5 days, and so on...

My question to you guys, is how can I call a function inside a for loop  without having to write the names of the conditions (which is what I did).

I would like to call the functions witha  parameter lets say:

for(i=0 to i=20)

{

int call = condition_1 *  condition_2 *  condition_3 * ... *  condition_20

} // somethin like that, but leaving the invocation of the functions parametrized, if you understand what I mean.

what is the way to invoke a function without having to literraly write its name. Can I call a function using a built string? for example "condition_"+"1" ?

here is the code I did:

//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_1() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_2() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_3() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_4() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_5() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_6() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_7() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_8() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_9() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int condicao_10() {
int resposta=0;

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int array_combinacao_de_condicoes[];
int array_sinais_das_condicoes[];
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int combinacao_de_condicoes(int param1) {

int resposta=param1;

int chave_condicao_1=0;
int chave_condicao_2=0;
int chave_condicao_3=0;
int chave_condicao_4=0;
int chave_condicao_5=0;
int chave_condicao_6=0;
int chave_condicao_7=0;
int chave_condicao_8=0;
int chave_condicao_9=0;
int chave_condicao_10=0;

chave_condicao_1 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[0]=chave_condicao_1;
array_sinais_das_condicoes[0]=condicao_1();

chave_condicao_2 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[1]=chave_condicao_2;
array_sinais_das_condicoes[1]=condicao_2();

chave_condicao_3 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[2]=chave_condicao_3;
array_sinais_das_condicoes[2]=condicao_3();

chave_condicao_4 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[3]=chave_condicao_4;
array_sinais_das_condicoes[3]=condicao_4();

chave_condicao_5 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[4]=chave_condicao_5;
array_sinais_das_condicoes[4]=condicao_5();

chave_condicao_6 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[5]=chave_condicao_6;
array_sinais_das_condicoes[4]=condicao_5();

chave_condicao_7 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[6]=chave_condicao_7;
array_sinais_das_condicoes[6]=condicao_7();

chave_condicao_8 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[7]=chave_condicao_8;
array_sinais_das_condicoes[7]=condicao_8();

chave_condicao_9 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[8]=chave_condicao_9;
array_sinais_das_condicoes[8]=condicao_9();

chave_condicao_10 = param1 - 3*(param1/3);
param1=param1/3;
array_combinacao_de_condicoes[9]=chave_condicao_10;
array_sinais_das_condicoes[9]=condicao_10();

Comment(
"chave_condicao_1: ",chave_condicao_1
,"\n","chave_condicao_2: ",chave_condicao_2
,"\n","chave_condicao_3: ",chave_condicao_3
,"\n","chave_condicao_4: ",chave_condicao_4
,"\n","chave_condicao_5: ",chave_condicao_5
,"\n","chave_condicao_6: ",chave_condicao_6
,"\n","chave_condicao_7: ",chave_condicao_7
,"\n","chave_condicao_8: ",chave_condicao_8
,"\n","chave_condicao_9: ",chave_condicao_9
,"\n","chave_condicao_10: ",chave_condicao_10

,"\n","param1: ",resposta
,"\n","aux: ",1
);

for(int i=0;i<ArraySize(array_combinacao_de_condicoes);i++)
{
if(array_combinacao_de_condicoes[i]==0)

if(array_combinacao_de_condicoes[i]==1 && array_sinais_das_condicoes[i]==1)

if(array_combinacao_de_condicoes[i]==2 && array_sinais_das_condicoes[i]==1)

if(array_combinacao_de_condicoes[i]==1 && array_sinais_das_condicoes[i]==-1)

if(array_combinacao_de_condicoes[i]==2 && array_sinais_das_condicoes[i]==-1)

}// for i

return resposta;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

For what I think you're trying to do, you can use callbacks. You'll still need to set the variables to call them though (and be careful not to call a NULL function).

typedef int (*condCallback)(void);

int condition1()
{
return 1;
}

//... 2-5

int condition6()
{
return 6;
}

void OnInit()
{
condCallback functions[6];

functions[0] = condition1;
functions[1] = condition2;
functions[2] = condition3;
functions[3] = condition4;
functions[4] = condition5;
functions[5] = condition6;

for (int i=0; i<6; i++)
{
Print(functions[i]());
}
}

Manuel Alejandro Cercos Perez #:

For what I think you're trying to do, you can use callbacks. You'll still need to set the variables to call them though (and be careful not to call a NULL function).

many thanks I didnt know about "callback".

I will study the documentation regarding it.

Reason: