Download MetaTrader 5
To add comments, please log in or register
Find out how to add a video in the messages editor
Tom
22
Tom 2014.02.01 18:56 

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


Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2014.02.01 20:24  
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.
Fernando Carreiro
2237
Fernando Carreiro 2014.02.01 20:25  

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!

Tom
22
Tom 2014.02.02 18:52  

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);
  }
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2014.02.02 19:16  
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);
               }
            }        
Tom
22
Tom 2014.02.02 21:17  
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! :)
Simon Gniadkowski
Moderator
17994
Simon Gniadkowski 2014.02.02 21:18  
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
/
To add comments, please log in or register