4105 OrderModify Error - page 2

 
WHRoeder:

I am great at copying, but not at scritting...   It was part of another EA which was applying the  case.  here is a copy:

 

   int l_Try = 0;
   int l_MaxTry = 10;
   
   switch (aCmd) {
   case OP_BUY:
   case OP_BUYLIMIT:
   case OP_BUYSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) 
      {
         if(aCmd==OP_BUY) { RefreshRates(); aPrice=Ask; aPrice2=Bid; }
            order_ticket = OrderSend(Symbol(), aCmd, aLots, aPrice, aSlipPage, 0, 0, aComment, aMagic, aExpiration, aColor);
         if (order_ticket > 0) break;
         err = GetLastError();
         if (err == 0) break;
         if (err == 4 || err == 137 || err == 146 || err == 136) Sleep(5000);
         else {
            Print("Error Code= ", err);
            break;
         }
      }
      break;
   case OP_SELL:
   case OP_SELLLIMIT:
   case OP_SELLSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) {
         if(aCmd==OP_SELL) { RefreshRates(); aPrice=Bid; aPrice2=Ask; }
           order_ticket = OrderSend(Symbol(), aCmd, aLots, aPrice, aSlipPage, 0, 0, aComment, aMagic, aExpiration, aColor);            
         if (order_ticket > 0) break;
         err = GetLastError();
         if (err == 0) break;
         if (err == 4 || err == 137 || err == 146 || err == 136) Sleep(5000);
         else {
            Print("Error Code= ", err);
            break;
         }

          }
}

 

Also my appology, my code was missing the OrderSymbol & OrderMagicNumber.

here is what I am working with. 

void ChangeStopsAndTPs() 
{
  int total = OrdersTotal();
   int l_Try = 0;
   int l_MaxTry = 10;

  for(int i=0;i<total;i++)
  {
    OrderSelect(i, SELECT_BY_POS,MODE_TRADES)
    if (OrderSymbol() == Symbol() && OrderMagicNumber()==MagicNumber)
     {
    int type   = OrderType();
    double sl = StopLoss*Point;
    double tp = TakeProfit*Point;

    bool result = false;
    
    switch(type)
    {
   case OP_BUY:
   case OP_BUYLIMIT:
   case OP_BUYSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) 
      {
         if(type==OP_BUY) 
      //modify opened long positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sl,OrderOpenPrice()+tp,0,NULL);
         if (result>0) break;
      }
    break;
    
      case OP_SELL:
      case OP_SELLLIMIT:
      case OP_SELLSTOP:
      for (l_Try = 0; l_Try < l_MaxTry; l_Try++) {
         if(type==OP_SELL)
      //modify opened short positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);
         if (result>0) break;
    }
    
    if(result == false)
    {
      ReportError();
      Sleep(3000);
    }  
   }
  }
}
  return(0);
}
 
I_Need_Money:
I grabed the idea from another EA, where it was opening orders based on the (switch(type) & CASE) and was trying to apply the same principle here.  Yes, I could out line each type of order, but was thrying to get the same result with the (switch & case).
OK,  carry on then . . .  are you saying you have no idea of how to do it and you need someone to do it for you ?
 

RaptorUK:
OK,  carry on then . . .  are you saying you have no idea of how to do it and you need someone to do it for you ?

 

I don't know how, but this code is working now.

 

void ChangeStopsAndTPs() 
{
  RefreshRates();
  int total = OrdersTotal();
  for(int i=0;i<total;i++)
  {
  OrderSelect (i, SELECT_BY_POS, MODE_TRADES);
    if ( OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
    {
    if(OrderStopLoss()==0 || OrderTakeProfit ()==0)
     {
    int type   = OrderType();
    double sl = StopLoss*Point;
    double tp = TakeProfit*Point;
    bool result = false;

    switch(type)
    {
      //Close opened long positions
      case OP_BUY: 
      case OP_BUYLIMIT: 
      case OP_BUYSTOP: 

         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-sl,OrderOpenPrice()+tp,0,NULL);
         //break;
      
      //Close opened short positions
      case OP_SELL: 
      case OP_SELLLIMIT: 
      case OP_SELLSTOP: 

         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);
         //break;                          
    }
    
    if(result == false)
    {
      ReportError();
      Sleep(3000);
    }
   }  
  }
}
  return(0);
}

 

Thank you. 

 
I_Need_Money:

I don't know how, but this code is working now.

Yes it would do . . . you removed the line of code I mentioned.

 

RaptorUK:

If I understand correctly your OP_BUY and OP_SELL Order are being modified but not your limit orders,  isn't this causing the issue ?

 and similar for the code that modifies the Buy orders.

if(type==OP_SELL)   // <-- only executes the following code (the OrderModify() )  for a OP_SELL
      //modify opened short positions
         result = OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+sl,OrderOpenPrice()-tp,0,NULL);

         if (result>0) break;

For a Sell Limit  (OP_SELLLIMIT) or Sell Stop (OP_SELLSTOP)  the OrderModify()  will not be called but the lines that follow will, result will be false even though no error has been generated.

 
RaptorUK:

Yes it would do . . . you removed the line of code I mentioned.

 


 

Thank you,

 

Now I understand. 

Reason: