EA running every 30sec

 

Hello,

I am just wondering how to code EA which would run every 30 seconds (not every tick).
Also, my EA trades multiple currency pairs.

Thanks

 

Code your EA as a script.

start(){

   while(IsStopped() == false){
 
     ... do your EA stuff...
 
     Sleep(30000); // wait approximately 30 seconds
 
   }
   return(0);
}
 

phy,

thanks for your response. it seems to be that you are the only guy responding to my questions !

and I am glad that there is at least somebody ;-)

I have following questions for you:

1./ could you explain what "while(IsStopped() == false)" means? Is it like never ending loop which performs some commands once per 30sec?

2./ how do I break that loop if I need to

3./ what happen if the 'tick' comes while EA is in the loop? is that tick going to be 'ignored' (ignored because EA is busy looping) or not?

I appreciate your help

draho

 

IsStopped() returns TRUE if the program has been commanded to stop its operation. I don't know exactly, but I use it, it works.
In this case, while the program has NOT "been commanded to stop" my code runs.

You can break the loop with a return(0). That kills the script completely, you have to start it again manually.

Scripts don't care about ticks. Tick or no tick, you start the script, it runs until it is finished.
Other EA or Indicator operates normally using tick to trigger operation.

---

A problem with using a script like this is that it does not remain attached to the chart. If it stops, it has to be manually
restarted. An EA or an indicator can start automatically ( like when MT4 is restarted) if they are already attached to
a chart. Also, you cannot change timeframe or the script will stop.

Write a simple looping script and try it out.

int start(){

while(!IsStopped()){

Comment("Here is something moving -- ", GetTickCount());

Sleep(1000);

}

Comment("Script has stopped");

return(0);

}

 

Just now I remember something...

If you set a loop like this in an EA instead of a script, it will still work.

If the loop is broken, the next tick restarts the program (and restarts the loop)

Just get everything "ready", declare variables, do intializations, and then go into the loop.

 

#Srahl hallo this is interesant 


i have used it but the Programm stopped at the start

bei Testing


int start()        {

  while(IsStopped() == false){

       meincode();

    Sleep(30000); // wait approximately 30 seconds

   }

  // return(0);

}

can you please help me

 
Sebhy Kl #: i have used it but the Programm stopped at the start

That is what you told it to do: do, sleep, repeat.

 
EventSetTimer() will help you achieve what you need
Documentation on MQL5: Working with Events / EventSetTimer
Documentation on MQL5: Working with Events / EventSetTimer
  • www.mql5.com
EventSetTimer - Working with Events - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

//global
int wait30seconds;


Comment("");

if (TimeCurrent()<=wait30seconds) return;  wait30seconds=TimeCurrent()+30; //update seconds

Comment("Updating...");

Reason: