Order Modify

 

Hello Everyone,

I have been experiencing intermittent issues with my OrderModify code and I have not been able to figure out what is causing it to fail. Typically the EA opens and order and then modifies it. But lately, the EA opens and order and never modifies the SL and TP. Hopefully the issue is blatantly obvious, but I have yet to find the issue. It is important to note that the function does succeed during a session but not always. If you have any input please feel free to comment:

// Trade Context Handling (https://www.mql5.com/en/articles/1412)
// Wait until the trade context is free and then occupy it globally (if an error occurs, leave it)
            if(_IsTradeAllowed() >= 0)
            {    // Trading Occupied Globally, check the terminal for activity
                 if(TradeIsBusy() < 0) 
                     return(-1); 
                 // Refresh the market info
                 RefreshRates();
                    
                      // Open orders using BuySell as criterion for OrderType
                      int ticket=OrderSend(Symbol(),intBuySell,LotSize(),BidAsk,3,0,0,"EA Order",Magic.Number,0,ColorCode);  //Open execution cannot include TP and SL, modify after execution
                        if(ticket<0)
                        {
                           Print("OrderSend failed with error #",GetLastError());
                        }else // Order was successful, Add SL and TP
                        {
                        if(ModifyOpenOrder(ticket,BuySell) < 0)
                           Print("OrderModify failed with error #",GetLastError());
                        }
                         
                 // Set the trade context back to free
                 TradeIsNotBusy();
                 return(0);
            }

int ModifyOpenOrder(int ticket,string BuySell)
{  
   // Function Returns 1 for success, -1 for failure
   OrderSelect(ticket,SELECT_BY_TICKET);
      // Set SL and TP according to Buy or Sell signs
      if(BuySell == "Buy") { SL = OrderOpenPrice()-SL*Point; TP = OrderOpenPrice()+TP*Point; }
      if(BuySell == "Sell"){ SL = OrderOpenPrice()+SL*Point; TP = OrderOpenPrice()-TP*Point; }  
      
      if(OrderModify(ticket, OrderOpenPrice(),SL,TP,0,Blue) == true) 
         return(1);
      else 
         return(-1);
}
 
thekaptain:

Hello Everyone,

I have been experiencing intermittent issues with my OrderModify code and I have not been able to figure out what is causing it to fail. Typically the EA opens and order and then modifies it. But lately, the EA opens and order and never modifies the SL and TP. Hopefully the issue is blatantly obvious, but I have yet to find the issue. It is important to note that the function does succeed during a session but not always. If you have any input please feel free to comment:

It might be helpful to know what error you get in your log when the OrderModify() fails.
 
if(BuySell == "Buy") { SL = OrderOpenPrice()-SL*Point; TP = OrderOpenPrice()+TP*Point; }
if(BuySell == "Sell"){ SL = OrderOpenPrice()+SL*Point; TP = OrderOpenPrice()-TP*Point; }  
  1. Always test your return codes and report the error from GetLastError so you find out why it is failing
  2. EA's must adjust for 4/5 digit brokers. tp, sl, AND slippage.
 
RaptorUK:
It might be helpful to know what error you get in your log when the OrderModify() fails.

There is no errors displayed in the log. Thus my confusion.

WHRoeder:
  1. Always test your return codes and report the error from GetLastError so you find out why it is failing
  2. EA's must adjust for 4/5 digit brokers. tp, sl, AND slippage.

I am testing the return codes and am reporting the errors. As far as TP, SL and Slippage are concerned, I am currently addressing that issue, but that doesn't change the fact that the OrderModfiy() is successful intermittently. Thus I can conclude that is not the issue at hand.

 
FreezeLevel ?
 
Ickyrus:
FreezeLevel ?

Pardon my ignorance, but what is a freezelevel?
 
thekaptain:

Pardon my ignorance, but what is a freezelevel?


Nevermind, I just read into the freezelevel information (https://book.mql4.com/appendix/limits).

My TP is static at 150 pips and SL at 30 pips. So I can safe with confidence that I should be well beyond the freezelevel.

 
if(ModifyOpenOrder(ticket,BuySell) < 0)
//
//
//
int ModifyOpenOrder(int ticket,string BuySell)
{  
   // Function Returns 1 for success, -1 for failure
   OrderSelect(ticket,SELECT_BY_TICKET);
      // Set SL and TP according to Buy or Sell signs
      if(BuySell == "Buy") { SL = OrderOpenPrice()-SL*Point; TP = OrderOpenPrice()+TP*Point; }
      if(BuySell == "Sell"){ SL = OrderOpenPrice()+SL*Point; TP = OrderOpenPrice()-TP*Point; }  
      
      if(OrderModify(ticket, OrderOpenPrice(),SL,TP,0,Blue) == true) 
         return(1);
      else 
         return(-1);
}

What is the stringvalue you are giving BuySell

I don't see you set it to "Buy" or to "Sell" but you call the function with

ModifyOpenOrder(ticket,BuySell)
Then it is needed BuySell is a string with some kind of value....
 
deVries:

What is the stringvalue you are giving BuySell

I don't see you set it to "Buy" or to "Sell" but you call the function with

Then it is needed BuySell is a string with some kind of value....


"Buy" and "Sell" is being passed into the parent operator. As stated before, the OrderSend() is opening the order successfully (thus "Buy" and "Sell" are being passed in correctly), but the modify does not always commit.
 
thekaptain:

There is no errors displayed in the log. Thus my confusion.

Well in that case the OrderModify didn't fail, and if it didn't fail and it didn't happen then it wasn't performed. Add some debugging Print() statements to find out if the OrderModify() is being attempted or not . . .
 
thekaptain:

"Buy" and "Sell" is being passed into the parent operator. As stated before, the OrderSend() is opening the order successfully (thus "Buy" and "Sell" are being passed in correctly), but the modify does not always commit.

                      // Open orders using BuySell as criterion for OrderType
                      int ticket=OrderSend(Symbol(),intBuySell,LotSize(),BidAsk,3,0,0,"EA Order",Magic.Number,0,ColorCode);  //Open execution cannot include TP and SL, modify after execution
                        if(ticket<0)
                        {
                           Print("OrderSend failed with error #",GetLastError());
                        }else // Order was successful, Add SL and TP
                        {
                        if(ModifyOpenOrder(ticket,BuySell) < 0)
                           Print("OrderModify failed with error #",GetLastError());
                        }
explain where did you do that after orderopening ??
Reason: