Code Help .. Modify 2 opened orders

 

Hello,

please can someone help me? Am just new in coding ..

I tried to Modify 2 open orders at the same time,(I have 2 tickets with different Magic, one is for buy and one for sell), but order select dont work for me

Errors like: invalid ticket for OrderModify function

Modify of Buy cant be succees, Error: 4051

what i doin wrong ? thx for answers

 if(Total>0)
  {
   if(OrderSelect(BuyTicket,SELECT_BY_TICKET))
    {
     RefreshRates();
     if(OrderOpenPrice()+20<=Bid)
      {
       BModify=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(HL15/2),OrderTakeProfit(),0,clrBlue);
       if (!BModify)
        Print ("Modify of Buy cant be succees, Error: ",GetLastError());
       else
        Print("Modify of Buy was succeed");
       }
      }
   if(OrderSelect(SellTicket,SELECT_BY_TICKET))
    {
     RefreshRates();
     if(OrderOpenPrice()-20>=Ask)
      {
       SModify=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(HL15/2),OrderTakeProfit(),0,clrBlue);
       if (!SModify)
        Print ("Modify of Sell cant be succees, Error: ",GetLastError());
       else
        Print("Modify of Sell was succeed");
       }
      }
   }
 
Bisonx: but order select dont work for me Errors like: invalid ticket for OrderModify
  1. How can you know? 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. The select is working fine, or you couldn't get to the OrderModify.

  3. You are selecting by ticket. Where do you test to see if the ticket has closed?
 
whroeder1:
  1. How can you know? 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. The select is working fine, or you couldn't get to the OrderModify.

  3. You are selecting by ticket. Where do you test to see if the ticket has closed?

Right, i couldn´t get Modify.

All looks like it working fin,  but Order StopLoss are not modified. ... why? I dont know...

Here is my all code (just tryin open and modify 2 opened orders at the same time)

Please tell me where I did mistake

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
 if(!IsTradeAllowed())
   {Alert("Autotrade is NOT allowed.");
    return(0);}
 return(INIT_SUCCEEDED);
 }
//+---------------------------------------------------------------------+
//|Checkin for Opened orders
//+-------------------------
bool IsBuyOpen(int BuyTicket)
 {
  if(OrderSelect(BuyTicket, SELECT_BY_TICKET))
   {
    return(OrderCloseTime() == 0);
    }
  else
   {
    return(false);
    }  
  }
bool IsSellOpen(int SellTicket)
 {
  if(OrderSelect(SellTicket, SELECT_BY_TICKET))
   {
    return(OrderCloseTime() == 0);
    }
  else
   {
    return(false);
    }  
  }
//+---------------------------------------------------------------------+
//|Start
//+-----
int start()
{
 int
 Total = OrdersTotal(),                                                  // All Opened Orders
 BuyTicket = -1,
 SellTicket = -1;
 double
 HL15 = NormalizeDouble(iHigh(NULL,15,1)-iLow(NULL,15,1),Digits),      // High and Low Diff on prev M15
 Spread = NormalizeDouble(MarketInfo(Symbol(),MODE_SPREAD)*Point,Digits),                        // Spread
 TrailStep = NormalizeDouble(2*Point,Digits);
 bool
 BModify,
 SModify,
 BSelect,
 SSelect;
//+---------------------------------------------------------------------+
//|Opening Orders Buy & Sell
//+-------------------------
 RefreshRates();
 if(Total<1)
  {
   RefreshRates();
   BuyTicket = OrderSend (Symbol(),OP_BUY,0.01,Ask,2,Bid-HL15,0,NULL,111,0,clrGreen);
   if(BuyTicket<0) 
     { 
      Print("Buy Order failed with error #",GetLastError()); 
      }
   else
     {
      Print("Buy was placed successfully");
      } 
   SellTicket = OrderSend (Symbol(),OP_SELL,0.01,Bid,2,Ask+HL15,0,NULL,222,0,clrRed);
   if(SellTicket<0) 
     { 
      Print("Sell Order failed with error #",GetLastError()); 
      }
   else
     {
      Print("Sell was placed successfully");
      } 
   }
//+---------------------------------------------------------------------+
//|StepTrail StopLoss
//+----------------
 if(Total>0)
  {
   do
    {
     if(IsBuyOpen(BuyTicket))
      {
       BSelect=OrderSelect(BuyTicket,SELECT_BY_TICKET);
       RefreshRates();
       if(Bid-(TrailStep-HL15)>OrderStopLoss())
        {
         RefreshRates();
         BModify=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-HL15,OrderTakeProfit(),0,clrBlue);
         if(!BModify)
          {
           Print ("Modify of Buy cant be succees, Error: ",GetLastError());
           }
         else
          {
           Print("Modify of Buy was succeed");
           }
          }
         }
     if(IsSellOpen(SellTicket))
      {
       SSelect=OrderSelect(SellTicket,SELECT_BY_TICKET);
       RefreshRates();
       if(Ask+(TrailStep+HL15)<OrderStopLoss())
        {
         RefreshRates();
         SModify=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+HL15,OrderTakeProfit(),0,clrBlue);
         if(!SModify)
          {
           Print ("Modify of Sell cant be succees, Error: ",GetLastError());
           }
         else
          {
           Print("Modify of Sell was succeed");
           }
          }
         }
      }
     while(IsBuyOpen(BuyTicket)||IsSellOpen(SellTicket));
    }
return(0);
}
 
Use the debugger or print out your variables, including _LastError and find out why.
Reason: