How can I run the function with a button without waiting for a tick change for OnCalculate to work? [MT5]

 
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//|                                                                                |
//+------------------------------------------------------------------+
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[])
  {
   
   if(prev_calculated == 0)
     {
      ButtonCreate();
        
//First of all, this is working and a button is added to the screen. Anytime I want I click the button and it sets bool_1 to true.
     }
     
  if(bool_1)
    {
    // Fonk2();
     // As you know, "OnCalculate" is called on every tick event. So if there is no tick, it doesn't work here. 
I can run prev_calculate as soon as it is equal to zero but I want the function to run only when I click the button.
        // bool_1 = false;
        // bool_2 = true;
    }
    
   if(bool_2)
     {  
        // Fonk3();     
        //bool_2 = false;
     }  

   return(rates_total);
  }


void Fonk1()
{
 //BLABLA
}


void Fonk2()
  {
  //BLABLA
  }

int Fonc3()
  {
   //BLA BLA
  }

bool ButtonCreate()               
  {
  //BLA BLA
  }
  
void OnChartEvent(const int id,const long& lparam,const double& dparam,const string& sparam)
  {
   if(id == CHARTEVENT_OBJECT_CLICK)
     {
      if(sparam == "Button")
        {
             //BLA BLA
        }
     }
  }

void OnDeinit(const int reason)
  {
   ObjectsDeleteAll();
  }

When I put the indicator on the screen, the button appears immediately. When I click the button at any time I want, the indicator works without any problems. But only when there is "Tick" movement. I explained the details in the code above.
How can I run the function that I want it to calculate as soon as I click the Button, without needing a tick?
"OnStart" does not work on indicators.
 
ZiYaR1:
When I put the indicator on the screen, the button appears immediately. When I click the button at any time I want, the indicator works without any problems. But only when there is "Tick" movement. I explained the details in the code above.
How can I run the function that I want it to calculate as soon as I click the Button, without needing a tick?
"OnStart" does not work on indicators.
void OnChartEvent(const int id, const long & lparam, const double & dparam, const string & sparam)
{
  if(id == CHARTEVENT_OBJECT_CLICK) {
    if(sparam == "Button") {
      datetime time[];
      double open[],high[],low[],close[];
      long tick_volume[],volume[];
      int spread[];
      CopyTime(NULL,0,0,Bars,time); ArraySetAsSeries(time,true);
      CopyOpen(NULL,0,0,Bars,open); ArraySetAsSeries(open,true);
      CopyHigh(NULL,0,0,Bars,high); ArraySetAsSeries(high,true);
      CopyLow(NULL,0,0,Bars,low); ArraySetAsSeries(low,true);
      CopyClose(NULL,0,0,Bars,close); ArraySetAsSeries(close,true);
      CopyTickVolume(NULL,0,0,Bars,tick_volume); ArraySetAsSeries(tick_volume,true);
      CopyRealVolume(NULL,0,0,Bars,volume); ArraySetAsSeries(volume,true);
      CopySpread(NULL,0,0,Bars,spread); ArraySetAsSeries(spread,true);

      OnCalculate(Bars,0/*else 1*/,time,open,high,low,close,tick_volume,volume,spread);
    }
  }
}
 
Vitaly Muzichenko #:
Your suggestion worked. However, when I changed the timeframe, I got an "Array out of range" error. My codes are very long, there is nothing that can cause an error in the line where the error is given, the cause of the problem may be deep. I ran the function I wanted to run on "OnInit" without waiting for the tick, actually I tried this before but it didn't work, but it seems to work now. I close and open the terminal, try different timeframes etc... if I run into a problem because I'm running on "OnInit" as before, I'll update my question here. Thanks for your help.
 
Why don't you create the button in OnInit and run the timeseries copy functions in OnCalculate and have the OnChartEvent do only the action that is called when the button is clicked?
 
Tobias Johannes Zimmer #:
Why don't you create the button in OnInit and run the timeseries copy functions in OnCalculate and have the OnChartEvent do only the action that is called when the button is clicked?

I was tired and sleepless while opening the topic, it never occurred to me. :) And then I rearranged it as you wrote. Thank you :)

Edit: Of course, when I did this, my reason for opening the subject did'nt completely disappear. For example, when the market is closed on the weekend, there will be no tick data. The function I want to run only when I press the button will not work even if I click the button. Because I want the function to run only when I click the button, not when prev_calculate is equal to zero.

 
It would be nice if you could give an update, otherwise nobody can tell what is the problem.
Sounds as if some function triggered by the button is still depending on OnCalculate.
 
ZiYaR1 #: want to run only when I press the button will not work even if I click the button. Because I want the function to run only when I click the button, not when prev_calculate is equal to zero.

The button click triggers a chart event. OnCalculate and prev_calculate are irrelevant.

Perhaps you should read the manual.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Reason: