EA freeze how to solve this?

 

I am running an EA now for some time and I found out that it sometimes just looks like to freeze. In the picture below I coded that the open order should be exited at the top BB. And this goes good in 90% of the situations. But sometimes not, and the order is closed just after a new compilation of the EA or a restart of the total terminal. I checked the common things like internet connection, terminal connection etc. and that all seems okay. I tried also several brokers, all having the same problem. I read something about indicators getting stuck and EA time outs after 2.5 seconds... My question is does somebody know why this happens and how I can solve this? Compiling the EA every hour for the rest of my life to avoid this doesn't sound like a real solution haha


 
Tomb:

I am running an EA now for some time and I found out that it sometimes just looks like to freeze.


Sounds like you have an infinite loop, find it and fix it, we can't help more specifically, we can't see your code.
 

Your screenshot does not help much. In fact, the chart you show does not even have an EA attached to it.

There is probably a bug in the EA code that may be causing it (maybe an endless loop of some sort).

If the EA was not coded by you, then maybe you can contact the developer and see if they can sort out the problem.

If the EA is your code and you are willing to show it, then that is probably the only way someone here is going to be able help, by analysing it to see if they can spot a bug. If however, you are not able to divulge the code, then it is going to be difficult to help.

I don't know, maybe someone has another idea of what can cause it.

EDIT: Oops! RaptorUK posted before I completed my text!

 

Thanks for your replies both of you. Below is the code of the EA. I only took out the custom indicators for discretion. Since I think you can see how I used them and since I think the problem will be in the loops above the custom indicators, this should not be a problem. It would be really nice if you can give me some help! Thanks.

extern int magicnumber=1;
extern double lots =0.1;

int init()
  {
   return(0);
  }
int deinit()
  {
   return(0);
  }

int start()
  {
  int ticket,i,total;
  int buytrade=0;
  int selltrade=0;
  double custom1,custom2,custom3,custom4;
  total=OrdersTotal();
  
  //look if buy should be closed
  for(i=0;i<total;i++)
  {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderMagicNumber() == magicnumber && OrderSymbol() == Symbol()) 
      {
            if(OrderType()==OP_BUY)
            {
               for(i=0;i<=iBarShift(Symbol(),0,OrderOpenTime(),true);i++)
               {
                  if(High[i]>iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_UPPER,i)) OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
               }
            }                 
      }
  }
  
  //look if sell should be closed
  for(i=0;i<total;i++)
  {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderMagicNumber() == magicnumber && OrderSymbol() == Symbol()) 
      {
            if(OrderType()==OP_SELL)
            {
               for(i=0;i<=iBarShift(Symbol(),0,OrderOpenTime(),true);i++)
               {
                  if(Low[i]<iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_LOWER,i)) OrderClose(OrderTicket(),OrderLots(),Ask,3,Red);
               }
            }   
      }
  }
  
  //look if there is already an order open on this bar
  for(i=0;i<total;i++)
  {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderMagicNumber() == magicnumber && OrderSymbol() == Symbol()) 
      {
          if(OrderType()==OP_BUY) buytrade=1;
          if(OrderType()==OP_SELL) selltrade=1;
          if(iBarShift(Symbol(),0,OrderOpenTime(),true)==0) return(0);
          
      }      
  }
  
  //look if there is an order closed on this bar
  for(i=0;i<OrdersHistoryTotal();i++)
  {      
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderMagicNumber()==magicnumber && OrderSymbol()==Symbol() && iBarShift(Symbol(),0,OrderCloseTime(),true)==0) return(0);
  }
  
  custom3=//
  custom4=//
  custom1=//
  custom2=//
  
         if( custom3>0 && custom1>0 && Open[0]<iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_UPPER,0))//buy
         {
               ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"vforcersibuy",magicnumber,0,Green);
               if(ticket<0)
               {
                Print("OrderSend failed with error #",GetLastError());
                return(0);
               }
               return(0);
         }
         
         if( custom2>0 && custom4>0 && Open[0]>iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_LOWER,0))//sell
         {                              
               ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"vforcersisell",magicnumber,0,Red);
               if(ticket<0)
               {
                Print("OrderSend failed with error #",GetLastError());
                return(0);
               }
               return(0);
         }
         
         if( (custom3>0 || custom4>0) && (buytrade==1 || selltrade==1) )
         {
               if(buytrade==1 && Open[0]<iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_UPPER,0))
               {
                  ticket=OrderSend(Symbol(),OP_BUY,lots,Ask,3,0,0,"vforcersibuy",magicnumber,0,Green);
                  if(ticket<0)
                  {
                   Print("OrderSend failed with error #",GetLastError());
                   return(0);
                  }                  
               }
               if(selltrade==1 && Open[0]>iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_LOWER,0))
               {
                  ticket=OrderSend(Symbol(),OP_SELL,lots,Bid,3,0,0,"vforcersisell",magicnumber,0,Red);
                  if(ticket<0)
                  {
                   Print("OrderSend failed with error #",GetLastError());
                   return(0);
                  }                 
               }              
         }                  
                
   return(0);
  }
 
Tomb:

Thanks for your replies both of you. Below is the code of the EA. I only took out the custom indicators for discretion. Since I think you can see how I used them and since I think the problem will be in the loops above the custom indicators, this should not be a problem. It would be really nice if you can give me some help! Thanks.

There are two different types of issues I see . . . the way your loop runs to close orders will not work, you MUST count down in t=you loop, shere here for more info: Loops and Closing or Deleting Orders


Also, you have one loop within the other . . . bot using the same variable, so the outside loop is being effected by the inside loop . . . yoiu need to use different variables for each of these loops . . .

  for(i=0;i<total;i++)   //  outside loop using  i
  {
      OrderSelect(i,SELECT_BY_POS);
      if(OrderMagicNumber() == magicnumber && OrderSymbol() == Symbol()) 
      {
            if(OrderType()==OP_BUY)
            {
               for(i=0;i<=iBarShift(Symbol(),0,OrderOpenTime(),true);i++)    //  inside loop also using  i
               {
                  if(High[i]>iBands(Symbol(),0,20,2,0,PRICE_CLOSE,MODE_UPPER,i)) OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
               }
            }        
 
Thanks! The i is really a beginners mistake sorry for bothering you with that, I am ashamed. But your post on the loops and closing is really helpful and interesting thanks for that! :)
 
Tomb:
Thanks! The i is really a beginners mistake sorry for bothering you with that, I am ashamed. But your post on the loops and closing is really helpful and interesting thanks for that! :)
You are welcome
 
Hi.please normalizedouble for ask and low and other prices .
 
After close positions do i--. Becuse numeration of your opened position be smaller .
 
Mohamadhasan Rahmani #: please normalizedouble for ask and low and other prices .
Mohamadhasan Rahmani #: After close positions do i--. Becuse numeration of your opened position be smaller .
  1. Do you really expect that people are still watching this thread after nine (9) years?
    Don't resurrect old threads without a very good reason. A lot has changed since Build 600 and Higher. (2014)

  2.  NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum #10 (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  3. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:

    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum

    2. For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11

    and check OrderSelect in case other positions were deleted.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

Reason: