How to call OnCalculate() function at a specific timing

 
Hello Guys!

The OnCalculate() function is called when a new tick is received.
Is it possible to call it at a specific timing?

Example:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
            //
  }
void OnChartEvent(const int id,
                  const long& lparam,
                  const double& dparam,
                  const string& sparam
                  )
  {
     if(id==CHARTEVENT_CHART_CHANGE)
     {
          //Call OnCalculate() again here
     }
  }

 

 

 


Documentation on MQL5: Language Basics / Functions / Event Handling Functions
Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
 
stitchtrader:
Hello Guys!

The OnCalculate() function is called when a new tick is received.
Is it possible to call it at a specific timing?

...

No you can't. Why do you need that, maybe there is a workaround.
 
angevoyageur:
No you can't. Why do you need that, maybe there is a workaround.
Hi Alain! Thank you xx :)

I got it.
I need that because to modify the objects size and location when the chart size changes.
And time[], open[], high[], low[] and close[] arrays are necessary.

Maybe I should work around to make arrays with like CopyTime function et cetera?

 

 
stitchtrader:
Hi Alain! Thank you xx :)

I got it.
I need that because to modify the objects size and location when the chart size changes.
And time[], open[], high[], low[] and close[] arrays are necessary.

Maybe I should work around to make arrays with like CopyTime function et cetera?

 

Yes you are right, the best way is to use CopyXXXX functions in the OnChartEvent().

Another solution would be to set a flag when OnChartEvent() is raised and to process your objects in OnCalculate() but that would be on the next tick.

 
angevoyageur:

Yes you are right, the best way is to use CopyXXXX functions in the OnChartEvent().

Another solution would be to set a flag when OnChartEvent() is raised and to process your objects in OnCalculate() but that would be on the next tick.

Thank you Alain!  I'll try that.

Have a nice day ♫

 
Alain Verleyen #:

Yes you are right, the best way is to use CopyXXXX functions in the OnChartEvent().

Another solution would be to set a flag when OnChartEvent() is raised and to process your objects in OnCalculate() but that would be on the next tick.

Hi Alain

How can I set a flag of "OnCalculate" On so that I could call it again?

 
Ahmed Sabry Mohammed Youssef Elgendi #:

Hi Alain

How can I set a flag of "OnCalculate" On so that I could call it again?

You can't set a flag to "call it again".

Why do you need to call OnCalculate() yourself ?

 

in MQL4, the "Start" can be called in functions like this :

void handleButtonClicks()
  {
   if(ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
     {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
      start();
     }
  }

but in MQL5, since it's "OnCalculate" function, we won't able to do like :

void handleButtonClicks()
  {
   if(ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
     {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;

      OnCalculate();
     }
  }


my main objective is to "hide/show" lines of zigzag indicator by clicking on a button, 

 

You don't need to call OnCalculate() at all to hide/show a line.

You can just play with DRAW_TYPE :

  PlotIndexSetInteger(plotIndex,PLOT_DRAW_TYPE,DRAW_NONE);
 

I also tried below code, it hiding the line correctly, but it won't replace (show) the line afterwards :

void handleButtonClicks()
  {
   if(ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE))
     {
      ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false);
      show_data = !show_data;
  

   if(show_data)
     {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed);
      PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_SECTION); // PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
      
     ChartRedraw(0);
     }
   else
     {
      ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color);
      ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed);
      PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_NONE);
     }  
     
     
     
     }
  }