OrderSend error 138 during BackTesting

 

Hi Guys,

I'm testing my EA and I'm getting always the error 138 each time I run OrderSend function.

I edited the EA in order to just call OrderSend on each onTick event:


RefreshRates();   

   int test=OrderSend(_CROSS,OP_BUY,0.01,Ask,5,0,0,"",0,0,clrGreen);

   return;


In the code I only declare variables and call OrderSend in this way but no order is being created...

I searched a lot in the forum but I didn't find a suitable solution...

This is very strange...

Thanks to support!

 

for OrderSend I use this function

//+------------------------------------------------------------------+
void Buy(double lot,int magic)
  {
   if(OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,NULL,magic,0,clrNONE))
     {
      Print(Symbol()+" Buy @ "+Ask);
        }else{      Print("Buy Open ERROR");
     }
  }
//+------------------------------------------------------------------+
void Sell(double lot,int magic)
  {
   if(OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,NULL,magic,0,clrNONE))
     {
      Print(Symbol()+" Sell @ "+Bid);
        }else{      Print("Sell Open ERROR");
     }
  }
//+------------------------------------------------------------------+

for OrderClose I use

//+------------------------------------------------------------------+
void CloseBuy(int magic)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_BUY)
                  if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE))
                    {
                     Print(OrderTicket()+" "+Symbol()+" Buy Closed @ "+OrderClosePrice());
                       }else{      Print("Buy Close Error");
                    }
     }
  }
//+------------------------------------------------------------------+
void CloseSell(int magic)
  {
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
               if(OrderType()==OP_SELL)
                  if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE))
                    {
                     Print(OrderTicket()+" "+Symbol()+" Sell Closed @ "+OrderClosePrice());
                       }else{      Print("Sell Close Error");
                    }
     }
  }
//+------------------------------------------------------------------+

for counting orders

int totalBuys(int magic)
  {
   double total=0;
   for(int t=0; t<OrdersTotal(); t++)
     {
      if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
              {
               if(OrderType()==OP_BUY)
                  total++;
              }
        }
     }
   return(total);
  }
//+------------------------------------------------------------------+
int totalSells(int magic)
  {
   double total=0;
   for(int t=0; t<OrdersTotal(); t++)
     {
      if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderMagicNumber()==magic)
            if(OrderSymbol()==Symbol())
              {
               if(OrderType()==OP_SELL)
                  total++;
              }
        }
     }
   return(total);
  }
//+------------------------------------------------------------------+


useful functions

 
int test=OrderSend(_CROSS,OP_BUY,0.01,Ask,5,0,0,"",0,0,clrGreen);
  1. Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  2. Do not trade multiple currencies in one EA.
Reason: