EA BUG

 

i'm trying to write an EA and i have little problem,

the EA calculate some bar's data at the end of every bar

i used code below:

if (candle1!=iTime(Symbol(),PERIOD_M1,1))  {candle1=iTime(Symbol(),PERIOD_M1,1); ....}

this code works perfectly every minutes;

when i run the EA for first time, EA results are completely correct, but after a minute it trying to calculate new data and at this time results are not correct,

so when i removed the EA and attach it again it works great;

i checkout all of variables and reset all value of them per while but it doesn't work yet,

actually i'm looking for a way to restart ea automatically.

i'm really sorry because ican't speak english carefully. :(

any help will much appricated.

 

and where is the rest of the code ?

this piece only checks bar 1 candle time.

 
Marco vd Heijden:

and where is the rest of the code ?

this piece only checks bar 1 candle time.

 

well the fact that 

Symbol()

Has received a new bar does not mean that all the others have received new bars also.

So that is not going to work.

It can easily take 30 seconds for a new tick to come in so you have to check all instruments for a new bar if you are going to do that.

 
Marco vd Heijden:

well the fact that 

Has received a new bar does not mean that all the others have received new bars also.

So that is not going to work.

It can easily take 30 seconds for a new tick to come in so you have to check all instruments for a new bar if you are going to do that.

thank you very much

do you have any suggestion to check all new bars?

 

yes

//+------------------------------------------------------------------+
//|                                                     New Bars.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

//--- declare bar times array
datetime bar_time[]; 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(500);
//--- resize time array
   ArrayResize(bar_time,SymbolsTotal(1),0);
//--- initialize time array  
   for(int symbol=0;symbol<SymbolsTotal(1);symbol++)
     {
      bar_time[symbol]=iTime(SymbolName(symbol,1),PERIOD_CURRENT,0);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
// we do not use OnTick because it is realted only to the chart instrument...
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- compare bar times
   bool newbars=1; // this flag is set to true assuming all bars are new 
   
//--- check to see if it holds true...
   for(int symbol=0;symbol<SymbolsTotal(1);symbol++)
     {
      if(iTime(SymbolName(symbol,1),PERIOD_CURRENT,0)==bar_time[symbol])
        {
         // however, if there is one or more bars that still have the old bar time, flag newbars is set to false...
         newbars=false;
        }
     }
     
//--- the outcome:
   if(newbars==1)
     {
      // all bars have been renewed at this point...
      // Do something...
      for(int symbol=0;symbol<SymbolsTotal(1);symbol++)
        {
         // overwrite the old bar times with the new bar times..
         bar_time[symbol]=iTime(SymbolName(symbol,1),PERIOD_CURRENT,0);
        }
     }
     
//--- otherwise, 
   else if(newbars==0)
     {
      // There are still some bars waiting for a new tick...
      // Do something else...
     }
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

yes

the code was really useful.

thank you so much.

 
   bool isNewbars=true; // this flag is set to true assuming all bars are new 
:
         newbars=false;
: 
   if(newbars==1)
:
   else if(newbars==0)
Bool is true and false not ones and zeros; don't use ints when you mean bool. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence. Write it correctly with good names
   bool allNewbars=true; // this flag is set to true assuming all bars are new 
:
         allNewbars=false; break;
: 
   if(allNewbars)
:
   else // It can only be one or the other if(!allNewbars)
Reason: