OrderClose and OrderSend 4051 error

 

hello everyone, i need a little help on finding the error on this code, namely: the orderclose error 4051 and ordersend error 4051

this is a hedging EA that takes 5 levels of lots: 0.01, 0.02, 0.04, 0.07, 0.1

if the previous order takes a -80 point loss, the robot hedges and takes the opposite position.

if the total profit makes it up to $1, then it closes all the orders...

int ticket[7]; // an array of tickets for ticket selection and manuevering
int level = 0; // level = 1 for 0.01 lot,
               // level = 2 for 0.02 lot,
               // level = 3 for 0.04 lot,
               // level = 4 for 0.07 lot,
               // level = 5 for 0.10 lot
int total;     // total is the OrdersTotal
int pips;      // profit / loss in pips

//+------------------------------------------------------------------+
//| for CloseAll() function variables                                |
//+------------------------------------------------------------------+
int option = 0;
int magic_number = 0;
string comment_text = "";
int before_day = 0;
//+------------------------------------------------------------------+
// Set this prameter to the type of clsoing you want:
// 0- Close all (instant and pending orders) (Default)
// 1- Close all instant orders
// 2- Close all pending orders
// 3- Close by the magic number
// 4- Close by comment
// 5- Close orders in profit
// 6- Close orders in loss
// 7- Close not today orders
// 8- Close before day orders
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   if (Bars < 100) { Print ("Bars less than 100."); return (0); }
   
   total = OrdersTotal();
   
   if ((total == 0) && (level == 0))
   {
      int trend = iMA(NULL,1,25,0,MODE_LWMA,PRICE_CLOSE,0);
      RefreshRates();
      if ((trend - Ask < 0) && (trend - Bid < 0))
      {
         ticket[level] = Buy(level);
         level = 1;
         return (0);
      }
      if ((trend - Ask > 0) && (trend - Bid > 0))
      {
         ticket[level] = Sell(level);
         level = 1;
         return (0);
      }
      else {return (0);}
      
      // this is the initial buy or sell position
   }
   
   if ((total>=1) && (level >= 1) && (total <=5))
   {
      double profit = AccountProfit();
      // if the order gives a $1 profit, close it/them... 
      // or if the orderS give a $25 loss, close them...
      if ((profit > 1.05) || ((total == 5) && (profit <= -25.0)))
      {
         CloseAll();
         level = 0;
         return (0);
      }
      
      // while the total orders are still below level 5, go on with the hedge... 
      else if (level != 5)
      {
         OrderSelect (total-1,SELECT_BY_POS,MODE_TRADES); //select previous order
         if(OrderType() == OP_BUY) pips = (MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);
         if(OrderType() == OP_SELL) pips= (OrderOpenPrice() - MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT);
         if (pips <= -80) 
         {
            if(OrderType() == OP_BUY)
            {
               ticket[level] = Sell(level);
               level = level + 1;
               return (0);
            }
            if(OrderType() == OP_SELL)
            {
               ticket[level] = Buy(level);
               level = level + 1;
               return (0);
            }
            
         }
      }  
   }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 
Show Buy() and Sell() functions.
 
void CloseAll ()
{
   int totalorders = OrdersTotal();
   int cnt = 0;
   for (cnt = totalorders ; cnt >=0 ; cnt--)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if(Symbol()!=OrderSymbol()) RefreshRates();
         if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
         if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
         if(OrderType()>OP_SELL) OrderDelete(OrderTicket());
      }
   //return (0);   
}

int Buy (int nth_level)
{
   double Lots [5] = {0.01 , 0.02 , 0.04 , 0.07 , 0.10};
   int ticket_num = OrderSend (Symbol(),OP_BUY,Lots[nth_level],Ask,3,0,0,"",12345,0,Green);
   return (ticket_num);
}

int Sell (int nth_level)
{
   double Lots [5] = {0.01 , 0.02 , 0.04 , 0.07 , 0.10};
   int ticket_num = OrderSend (Symbol(),OP_SELL,Lots[nth_level],Bid,3,0,0,"",12345,0,Red);
   return (ticket_num);
}
 

Replace

for (cnt = totalorders ; cnt >=0 ; cnt--)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if(Symbol()!=OrderSymbol()) RefreshRates();
         if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
         if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
         if(OrderType()>OP_SELL) OrderDelete(OrderTicket());
      }

to

for (cnt = totalorders-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
         if(Symbol()!=OrderSymbol()) RefreshRates();
         if(OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Violet);
         if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Violet);
         if(OrderType()>OP_SELL) OrderDelete(OrderTicket());
      }
 
Wow!!! thanks! i'll improve this EA...
 
Always test return codes, make the EA compatible with other EA's (including itself on other charts)
for (cnt = totalorders-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
to
    for(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.
Reason: