Download MetaTrader 5
To add comments, please log in or register
Interested in the Market activity? Come and track!
rafal85
22
rafal85 2016.03.21 14:35 

Hello,

I need get rid of infinite loop

if((cmd==OP_BUY)&&(OrderProfit()<0))
{
      while(true)
    {
           result=OrderSend(Symbol(), OP_SELL, LOTS*2, Bid, 90, 0, 0, NULL, 0,0, clrNONE );
           
      if(result!=TRUE) { error=GetLastError(); Alert("Last1 = ",error); }
      else break;
    }
}
if((cmd==OP_SELL)&&(OrderProfit()<0))
{
      while(true)
    {
           result=OrderSend(Symbol(), OP_BUY, LOTS*2, Ask, 90, 0, 0, NULL, 0,0, clrNONE );

      if(result!=TRUE) { error=GetLastError(); Alert("Last2 = ",error); }
      else break;
    }
}
//+------------------------------------------------------------------+
if((OrderProfit()>0))
{
      while(true)
    {
           result=close();

      if(result!=TRUE) { error=GetLastError(); Alert("Last2 = ",error); }
      else break;
    }
}

}
else { Print( "Error when order select ", GetLastError()); break; }
}
}
//+------------------------------------------------------------------+
bool close()
{
bool result;
int cmd,error;
int total = OrdersTotal()-1;

//for (int i = OrdersTotal()-1 ; i < total ; i++)
//for (int i = 0; i<total; i++)
  for( int i = total;i>0;i--)
{
    if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
cmd=OrderType();

if(cmd==OP_BUY)
{
      while(true)
    {
           result=OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
           
      if(result!=TRUE) { error=GetLastError(); Alert("LastError4 = ",error); }
      else break;
    }
}              
if(cmd==OP_SELL)  
{
      while(true)
    {
           result=OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

      if(result!=TRUE) { error=GetLastError(); Alert("LastError5 = ",error); }
      else break;
    }
}
else { Print( "Error when order select ", GetLastError()); break; }
}
}
return(0);
}
Carl Schreiber
5937
Carl Schreiber 2016.03.21 15:46  
which infinite loop?
rafal85
22
rafal85 2016.03.21 15:51  

if that code closes all trades it do not stop run and returns error 0.

if((OrderProfit()>0))
{
      while(true)
    {
           result=close();

      if(result!=TRUE) { error=GetLastError(); Alert("Last2 = ",error); }
      else break;
Carl Schreiber
5937
Carl Schreiber 2016.03.21 16:08  

Read about GetLastError (F1)?

this function resets lastError..

Try to develop a better readable coding style!

/
To add comments, please log in or register