Error in OrderClose #129

 
I am doing an EA of triangular arbitrage.. when profit reach 22 or above the orders should be close but when I'm trying to close the orders I got an error of 129..
void checkProfit()
{
   double profit=0, p[3];
   int ticket[3];
   int minute = TimeMinute(TimeCurrent());
   string date_time = TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);
   
   if(OrdersTotal()!=0)
   {  
      magicnumber();
      for(cnt=OrdersTotal();cnt>=0;cnt--)
      {  
         OrderSelect(cnt, SELECT_BY_POS);
         if(magic == OrderMagicNumber())
         {  
            if(set<3)
            {
               if(set == 0)
               {
                  if(OrderType() == OP_SELL)
                     type = "sell";
                  else if(OrderType() == OP_BUY)
                     type = "buy";              
               }
               ticket[set] = OrderTicket();
               profit = profit + OrderProfit();
               p[set] = OrderProfit();
               set++;
            }
           
            RefreshRates();
            if(set == 3)
            {  
               Print("Profit: ",profit);
               Print("Magic: ",magic," Ticket 0: ",ticket[0]," Ticket 1: ",ticket[1]," Ticket 2: ",ticket[2]," Set: ",set," Cnt: ",cnt);
               counter1=counter1+3;
               if(counter1<=OrdersTotal())
               {
                  FileSeek(handle1, 0, SEEK_END);
                  FileWrite(handle1,date_time,(magic_number+1)+"-"+(magic_number+3),"EURUSD: "+ticket[2],p[2],"EURJPY: "+ticket[1],p[1],"USDJPY: "+ticket[0],p[0],"Total Profit: "+profit); 
               }
               else
               counter1 = 0;
                
               if(profit >= 22.00)
               {  
                  if(type == "sell")
                  {  Print("Ticket 0: ",ticket[0]," Ticket 1: ",ticket[1]," Ticket 2: ",ticket[2]," type: ",type);
                     close_eurusd = false;
                     close_eurjpy = false;
                     close_usdjpy = false;
                     while(close_usdjpy == false)
                     {  
                        ask_usdjpy = MarketInfo("USDJPY",MODE_ASK);
                        OrderSelect(ticket[1], SELECT_BY_TICKET);
                        close_usdjpy = OrderClose(OrderTicket(), lots, ask_usdjpy, 3, Yellow);
                        Print(OrderTicket()," - ",ticket[1]," - Pasok - 1b - ",bid_usdjpy," - ",GetLastError());
                        close_usdjpy = true;
                     }
                     while(close_eurjpy == false)
                     {  
                        bid_eurjpy = MarketInfo("EURJPY",MODE_BID);
                        OrderSelect(ticket[0], SELECT_BY_TICKET);
                        close_eurjpy = OrderClose(OrderTicket(), lots, bid_eurjpy, 3, Yellow); 
                        Print(OrderTicket()," - ",ticket[0]," - Pasok - 2b - ",ask_eurjpy," - ",GetLastError());
                        close_eurjpy = true;
                     }
                     while(close_eurusd == false)
                     {  
                        ask_eurusd = MarketInfo("EURUSD",MODE_ASK);
                        OrderSelect(ticket[2], SELECT_BY_TICKET);
                        close_eurusd = OrderClose(OrderTicket(), lots, ask_eurusd, 3, Yellow);
                        Print(OrderTicket()," - ",ticket[2]," - Pasok - 3b - ",bid_eurusd," - ",GetLastError());
                        close_eurusd = true;
                     }
                     FileSeek(handle, 0, SEEK_END);
                     FileWrite(handle, ticket[2], ticket[0], ticket[1], profit);
                     profit = 0;
                     magic++;
                     if(OrdersTotal()==0)
                        previous_level = OrdersTotal();
                  }
                  else if(type == "buy")
                  {  Print("Ticket 0: ",ticket[0]," Ticket 1: ",ticket[1]," Ticket 2: ",ticket[2]," type: ",type);
                     close_eurusd = false;
                     close_eurjpy = false;
                     close_usdjpy = false;
                     while(close_usdjpy == false)
                     {  
                        bid_usdjpy = MarketInfo("USDJPY",MODE_BID);
                        OrderSelect(ticket[1], SELECT_BY_TICKET);
                        close_usdjpy = OrderClose(OrderTicket(), lots, bid_usdjpy, 3, Green);
                        Print(OrderTicket()," - ",ticket[1]," - Pasok - 1b - ",bid_usdjpy," - ",GetLastError());
                        close_usdjpy = true;
                     }
                     while(close_eurjpy == false)
                     {  
                        ask_eurjpy = MarketInfo("EURJPY",MODE_ASK);
                        OrderSelect(ticket[0], SELECT_BY_TICKET);
                        close_eurjpy = OrderClose(OrderTicket(), lots, ask_eurjpy, 3, Green);
                        Print(OrderTicket()," - ",ticket[0]," - Pasok - 2b - ",ask_eurjpy," - ",GetLastError());
                        close_eurjpy = true;
                     }
                     while(close_eurusd == false)
                     {  
                        bid_eurusd = MarketInfo("EURUSD",MODE_BID);
                        OrderSelect(ticket[2], SELECT_BY_TICKET);
                        close_eurusd = OrderClose(OrderTicket(), lots, bid_eurusd, 3, Green);
                        Print(OrderTicket()," - ",ticket[2]," - Pasok - 3b - ",bid_eurusd," - ",GetLastError());
                        close_eurusd = true;
                     }
                     FileSeek(handle, 0, SEEK_END);
                     FileWrite(handle, ticket[2], ticket[0], ticket[1], profit);
                     profit = 0;
                     magic++;
                     if(OrdersTotal()==0)
                        previous_level = OrdersTotal();
                  }
               }//if(profit >= 22.00)
               else
               {
                  profit = 0;
                  magic++;
               }              
              set = 0;   
            }//if(set == 3)
         }//if(magic == OrderMagicNumber())
         else if(magic>checkmagic)
         {
            magic = OrderSelect(OrdersTotal()-1, SELECT_BY_POS);
         }         
      }//for(cnt=OrdersTotal();cnt>=0;cnt--)  
   }//if(OrdersTotal()!=0)
}
 
You seem to be closing Sell orders on EURJPY at bid_eurjpy and Buy orders on EURJPY at ask_eurjpy
 
    for(cnt=OrdersTotal();cnt>=0;cnt--)
      {  
         OrderSelect(cnt, SELECT_BY_POS);
         if(magic == OrderMagicNumber())
  1. Position is OrdersTotal() - 1 to zero so your the first orderSelect always fails and everything thing else is bogus.
  2. Always test return codes.
  3. No need to indent 3 levels
    for(int iPos = OrdersTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)
    &&  OrderSymbol() == chart.symbol               // This pair/Any TF.
    

  4.             if(set<3){
                   :
                   set++;
                }
               
                RefreshRates();
                if(set == 3)...
    simplify code where possible
                if(set<3){
                   :
                   set++;
                   continue;
                }
               
                RefreshRates();
    //            if(set == 3)...
    No need for refreshRates there as you haven't done any server calls since start. You DO need refreshRates between each server call. IF(A){} IF(not A) could be IF(A){} else {.. but since your in a loop, just continue until you have the right count.
 
WHRoeder:
  1. Position is OrdersTotal() - 1 to zero so your the first orderSelect always fails and everything thing else is bogus.
  2. Always test return codes.
  3. No need to indent 3 levels
  4. simplify code where possible No need for refreshRates there as you haven't done any server calls since start. You DO need refreshRates between each server call. IF(A){} IF(not A) could be IF(A){} else {.. but since your in a loop, just continue until you have the right count.

thanks for your reply, but it still don't close orders.. I there anything else that I need to change.. I'm new in doing this EA
 
RaptorUK:
You seem to be closing Sell orders on EURJPY at bid_eurjpy and Buy orders on EURJPY at ask_eurjpy

thanks for your reply I think it's right coz when I open the orders for eurjpy when the set is sell I open it with bid so when I close the orders I should also close it with bid...same as buy..

 
jam04:

thanks for your reply I think it's right coz when I open the orders for eurjpy when the set is sell I open it with bid so when I close the orders I should also close it with bid...same as buy..

No . . you need to close a Sell with a Buy . . and a Buy is taken at Ask . . not Bid . . . look at "your" code for USDJPY and EURUSD, you close those Sells at Ask, and those Buys at Bid . . .
 
RaptorUK:
No . . you need to close a Sell with a Buy . . and a Buy is taken at Ask . . not Bid . . . look at "your" code for USDJPY and EURUSD, you close those Sells at Ask, and those Buys at Bid . . .

but when I put profit=22; before print profit the orders are close.. but when I wait for the profit to reach 22 base on the movement of the chart when it reach 22 it just give me an error of 129 "Invalid price"..
 
jam04:

it just give me an error of 129 "Invalid price"..
What pairs does it give you an Error 129 on ?
 
RaptorUK:
What pairs does it give you an Error 129 on ?

all pairs
 
RaptorUK:
What pairs does it give you an Error 129 on ?
Is it possible that the reason that I got this error is because the ea open too many orders per tick?
 
jam04:

all pairs
Can you copy and paste some entries from the Experts tab/Journal tab to show this.
Reason: