Generic funcion to iterate

 

Hi,


I use the loop for all open charts a lot of times on my code. So I'm repeating the code a lot.


Is there any way to create a generic function to iterate the charts, and do something variable?


Something like this

void sample()
{
        iterateCharts (do something complex);
}

void iterateCharts(PARAM_WITH_THINGS_TO_ITERATE)
{
   long currChart,prevChart=ChartFirst();
   int i=0,limit=100;
   
   while(i<limit)
   {
      //******************************
      //do something with prevChart
        PARAM_WITH_THINGS_TO_ITERATE
      //do something with prevChart
      //******************************
      currChart=ChartNext(prevChart); 
      if(currChart<0) break;          
      
      prevChart=currChart;
      i++;
   }  
}

Maybe with a callback? Passing a function by param? But, I don't know how do it :(


The problem is that i repeat this code many times, and I'd like to reuse it.


Thank you

Documentation on MQL5: Chart Operations / ChartOpen
Documentation on MQL5: Chart Operations / ChartOpen
  • www.mql5.com
ChartOpen - Chart Operations - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Hello @powerbucker

If i understood what you need is to have a list of functions and iterate through them for all charts 

I don't know what the scope of the parameters is per function (if this is the case) but 

if not "void" or same you could use a structure to carry the various parameters the subfunctions would need 

A function pointer is declared like this in general 

typedef int (*FunctionPointer)(int,int);

This returns an int and receives 2 ints 

You can also do initialization in the pointer

typedef int (*FunctionPointer)(int,int=0);

So in this case let's declare some "dummy" holder of all possible sub function parameters

struct carrier_of_parameters_for_chart{
int a;
double b;
datetime c;
bool d;
};

And then a pointer to a subfunction that receives the carrier and a chart id

typedef void (*PointerToFunctionWithParamCarry)(carrier_of_parameters_for_chart&,long=0);

Which returns void because i imagine your functions don't return anything , if they do you can also use the carrier since it's passed by reference anyway (if they don't return the same type)

Let's declare some subfunctions :

//function a
void function_a(carrier_of_parameters_for_chart &params,long chart_id=0){
Print("Execution of FunctionA for chart id "+IntegerToString(chart_id));
}

//function b
void function_b(carrier_of_parameters_for_chart &params,long chart_id=0){
Print("Execution of FunctionB for chart id "+IntegerToString(chart_id));
}

And the following would be your generic function that iterates through the charts 

//general execution function
void general(PointerToFunctionWithParamCarry function,
             carrier_of_parameters_for_chart &parameters_carrier){
//let's send some mock chart ids 
  for(int i=0;i<3;i++){//we are looping in charts , thats just for show ,we are not actually looping in charts
  //and call the function that was sent with this chart id
    function(parameters_carrier,i);
  }             
}

And then you would have (on the global scope + initialized once) a function stack that holds all your possible functions in an array

int OnInit()
  {
//---
  PointerToFunctionWithParamCarry function_stack[];
  //lets add a + b
    ArrayResize(function_stack,2,0);
    function_stack[0]=function_a;
    function_stack[1]=function_b;
  
  //and we loop to the function we want to execute outside of the general looping function
    carrier_of_parameters_for_chart uni_params;
    for(int i=0;i<ArraySize(function_stack);i++){
    general(function_stack[i],uni_params);
    }
//---
   return(INIT_SUCCEEDED);
  }

 And finally you would loop through the functions , call the general functions and you would achieve iteration through all functions for all charts .

You can also send the array of subfunction pointers to the general function and iterate per chart and then per function inside.

 
Lorentzos Roussos #:

Hello @powerbucker

If i understood what you need is to have a list of functions and iterate through them for all charts 

I don't know what the scope of the parameters is per function (if this is the case) but 

if not "void" or same you could use a structure to carry the various parameters the subfunctions would need 

A function pointer is declared like this in general 

This returns an int and receives 2 ints 

You can also do initialization in the pointer

So in this case let's declare some "dummy" holder of all possible sub function parameters

And then a pointer to a subfunction that receives the carrier and a chart id

Which returns void because i imagine your functions don't return anything , if they do you can also use the carrier since it's passed by reference anyway (if they don't return the same type)

Let's declare some subfunctions :

And the following would be your generic function that iterates through the charts 

And then you would have (on the global scope + initialized once) a function stack that holds all your possible functions in an array

 And finally you would loop through the functions , call the general functions and you would achieve iteration through all functions for all charts .

You can also send the array of subfunction pointers to the general function and iterate per chart and then per function inside.

It's just what I need, I will try. Thank you very much !!

 
powerbucker #:

It's just what I need, I will try. Thank you very much !!

anytime


 
powerbucker: Maybe with a callback? Passing a function by param? But, I don't know how do it :(
class ChartPredicate{ virtual void doChart(long chartID)=0; };
void iterateCharts(ChartPredicate& what)
{
   for(long currChart=ChartFirst(); currChart > 0; currChart=ChartNext(currChart))
      what.doChart(currChart);
}

class SomethingComplex : public ChartPredicate{
   void doChart(long chartID){ whatever(); }
};

void sample()
{
        SomethingComplex operation; iterateCharts(operation);
}
 
William Roeder #:

Thank you !!

Reason: