Ordersend error 138

 

Hi everyone, im new at coding and still have not managed to get many things right. I keep receiving the error code 138 and ive heard a refreshrates() would fix it but im assuming there is something wrong with my brackets or maybe something else. Here is my code can anyone find why the strategy tester is giving me this error?

extern double    Lots=0.1;
extern int       Period_MA1=35;
extern int       Period_MA2=5;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

int init()
{
    
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
  int Work(double ma1_1,double ma2_1, double ma1_0, double ma2_0)
   { 
     static int last_direction=0;
     static int current_direction=0;
   
   if(ma1_1 > ma2_1 && ma1_0 < ma2_0)
     current_direction=1;
   
   if(ma1_1 < ma2_1 && ma1_0 > ma2_0)
     current_direction=2;
     
   if(current_direction!=last_direction)
    { last_direction=current_direction;
     return(last_direction);
     }
     else
     return(0);
     }
     
 int CloseAllSells()                    //
   { 
   while(OrdersTotal()>0)
     {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
      switch (OrderType())
      { 
      case OP_SELL: OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 4, Purple); break;
      case OP_SELLSTOP:OrderDelete(OrderTicket()); 
      case OP_SELLLIMIT: OrderDelete(OrderTicket());
      }
    }
    Sleep(10000);
   RefreshRates();
   return(OrderType()==0);   
  }
  int CloseAllBuys()
   {
   
  while(OrdersTotal()>0)
   {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
      switch (OrderType())
      {
      case OP_BUY: OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_BID), 4, Purple); break;
      case OP_BUYSTOP:OrderDelete(OrderTicket());      
      case OP_BUYLIMIT:OrderDelete(OrderTicket());     
      }
   }
   Sleep(10000);
   RefreshRates();
   return(OrderType()==0);  
 }
   
     
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
{

double ma1_0,
ma1_1,
ma2_0,
ma2_1,
bbg_0,
bbg_1,
bbp_0,
bbp_1;
int cnt,Ticket,total;

  
ma1_0=iMA(NULL,0,Period_MA1,0,MODE_EMA,PRICE_CLOSE,0);
ma1_1=iMA(NULL,0,Period_MA1,0,MODE_EMA,PRICE_CLOSE,2);
ma2_0=iMA(NULL,0,Period_MA2,0,MODE_EMA,PRICE_CLOSE,0);
ma2_1=iMA(NULL,0,Period_MA2,0,MODE_EMA,PRICE_CLOSE,2);
bbg_1=iCustom(NULL,0,"BB_MACD",12,26,10,2.5,1,2);
bbg_0=iCustom(NULL,0,"BB_MACD",12,26,10,2.5,1,0);
bbp_1=iCustom(NULL,0,"BB_MACD",12,26,10,2.5,2,2);
bbp_0=iCustom(NULL,0,"BB_MACD",12,26,10,2.5,2,0);

int order=Work(ma1_1,ma2_1,ma1_0,ma2_0);

total=OrdersTotal();



     
        if(total<1)
         {                                                                     
          
              if(order==1)
             {                                                                 
                     

            while(true)
                 {
                 Ticket = OrderSend(Symbol(),OP_BUY,Lots,Bid,30,Bid-1500*Point,Bid+1500*Point);                         
                if(Ticket<0)
                     {
                 RefreshRates();
                 continue;
                     }   
                 else
                   {
                Sleep(20000);
                return(Ticket==OrderTicket());
                break;  
                   }
                 }    
         
           }
                             
       
     

    if(order==2)
        {                                                                 
                     

            while(true)
               {
                 Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,30,Ask-1500*Point,Ask+1500*Point);                         
                if(Ticket<0)
                   {
                 RefreshRates();
                 continue;
                   }   
                 else
                  {
                Sleep(20000);
                return(Ticket==OrderTicket());
                break;  
                  }
               }    
        
           }
           }
           
           
  for(cnt=0;cnt<total;cnt++)
    {
     OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
     if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
       {
          if(OrderType()==OP_BUY)                                             //will only work for
             {
        
               if(bbg_1 <= bbp_1 && bbg_0 >= bbp_0)
                         {
                   CloseAllBuys();
                         return;
                         }
         else
                     {
          if(bbg_1 >= bbp_1 && bbg_0 <= bbp_0)
                         {
                   CloseAllSells();
                          return;
                         }
                          
                     }
                  }
              } 
         }
          return(0);
          
       }
       
        
 

Buy at Ask, Sell at bid . . .

Not this . . .

 if(order==1)
             {                                                                 
            while(true)
                 {
                 Ticket = OrderSend(Symbol(), OP_BUY  ,Lots,  Bid  ,30,Bid-1500*Point,Bid+1500*Point);   
 
RaptorUK:

Buy at Ask, Sell at bid . . .

Not this . . .


just changed it but im still getting the ordersend error 138
 
   while(OrdersTotal()>0)
     {
      OrderSelect(0, SELECT_BY_POS, MODE_TRADES);
      switch (OrderType())
      { 
      case OP_SELL: OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(),MODE_ASK), 4, Purple); break;
      case OP_SELLSTOP:OrderDelete(OrderTicket()); 
      case OP_SELLLIMIT: OrderDelete(OrderTicket());
      }
    }
  1. OrdersTotal()>0 and OrderSelect(0...) Makes the EA incompatible with every other including manual trading.
  2. If the OrderSelect fails so does everything else. Always test return codes
  3. If the OrderClose/OrderDelete fails you have an infinite loop. Always test return codes and print errors.
  4. Why use the MarketInfo function call instead of just Ask or the simpler OrderClosePrice()
    for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
        OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
    &&  OrderMagicNumber()  == magic.number             // my magic number
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    ){
        switch (OrderType()){ 
        case OP_SELL: 
            if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 4, Purple))
                Print("OrderClose failed: ", GetLastError());
            break;
        case OP_SELLSTOP:
        case OP_SELLLIMIT: 
            if (!OrderDelete(OrderTicket())
                Print("OrderDelete failed: ", GetLastError());
            break;
        }
    }

Sleep(10000);
   RefreshRates();
   return(OrderType()==0);  
Once the last order is deleted, what is OrderType()
Ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,30,Ask-1500*Point,Ask+1500*Point);                         
if(Ticket<0)...
else{
   Sleep(20000);
   return(Ticket==OrderTicket());
What is OrderTicket, you haven't selected anything.
 

What are these supposed to be doing . . .

return(Ticket==OrderTicket());

return(OrderType()==0); 

. . . they are called from Functions that return int yet this will return true or false . . . . and you don't even use the return value anyway ? so what is the point ?

 

If total = 0 this can make your code lock up . . .

for(cnt=0;cnt<total;cnt++)
    {
 
although this reply is too late, but think can help other too if searching. using strategy tester, set manual spread, do not use current spread on market weekend. i also got this error, tested other previous code suddenly the same problem. i forgot that i am using current spread on market closed @ weekend. so i set manual spread value, then no more error 138.
Reason: