Canceling a pending order while in OnTradeTransaction event

 

Hello. I'm new to using the OnTradeTransaction Event.

I'm using it to detect pending orders that I place manually from a different terminal.

The pending orders should trigger the EA to start a trading session based on the pending order parameters.

That's working very well.

But now I want to delete the pending order as soon as I extract what I need. I used Ctrade OrderDelete to cancel the order but I'm getting invalid request error.

void OnTradeTransaction(const MqlTradeTransaction& trans,
                        const MqlTradeRequest& request,
                        const MqlTradeResult& result)
  {
   if(trans.type == TRADE_TRANSACTION_ORDER_ADD)
     {
      if(trans.order_type == ORDER_TYPE_BUY_STOP || trans.order_type == ORDER_TYPE_SELL_STOP)
        {
         string symbol = trans.symbol;
         ENUM_ORDER_TYPE orderType = trans.order_type;
         double xvalid = trans.price_sl;
         if(xvalid == 0.0)
            return;
         Print("New STOP ORDER Registered in the Chase");
         PrintFormat("%s: Type:- %s", symbol, EnumToString(orderType));

         Chaser *newChaser = new Chaser(symbol, orderType, TF, xvalid);
         marray.Add(newChaser);
         // Try to cancel the pending order.
         Print(trans.order);
         if(trade.OrderDelete(trans.order))
           {
            Print("Order Deleted in OnTradeTransaction");
           }
         else
           {
            Print("OnTradeTransaction Error: ", GetLastError());
           }
        }
     }
  }


Sometimes it goes through.

Most Times, it is an invalid request error. Of the 20+ plus times I tried, only a couple went through

I printed the logs in the screenshots.

Is there something I'm missing? Thanks

Files:
sucess.png  20 kb
fail.png  29 kb
 
When you delete the order onTradeTransaction is invoked once again. This time the order is already deleted and cannot be deleted again. I guess that is the problem.
 
Kevin Onsongo:

Hello. I'm new to using the OnTradeTransaction Event.

I'm using it to detect pending orders that I place manually from a different terminal.

The pending orders should trigger the EA to start a trading session based on the pending order parameters.

That's working very well.

But now I want to delete the pending order as soon as I extract what I need. I used Ctrade OrderDelete to cancel the order but I'm getting invalid request error.


Sometimes it goes through.

Most Times, it is an invalid request error. Of the 20+ plus times I tried, only a couple went through

I printed the logs in the screenshots.

Is there something I'm missing? Thanks

Print the trading error code instead of the generic last error which is useless here.

            Print("OnTradeTransaction Error: ", Trade.ResultCode());
 
Yashar Seyyedin #:
When you delete the order onTradeTransaction is invoked once again. This time the order is already deleted and cannot be deleted again. I guess that is the problem.

Hello

I'm only looking for a new active order. That is what I'm checking first.

if(trans.type == TRADE_TRANSACTION_ORDER_ADD)

The block won't run again unless another pending order is added. I send only one order at a time, for now.

When I get the error, the order isn't deleted.

 
Alain Verleyen #:

Print the trading error code instead of the generic last error which is useless here.

Thank you for the tip.

The error code changed to 10013 invalid request

 
Kevin Onsongo #:

Thank you for the tip.

The error code changed to 10013 invalid request

Can you post BOTH the experts log AND the journal log ? (including the opening of the order).
 
Alain Verleyen #:
Can you post BOTH the experts log AND the journal log ? (including the opening of the order).

Here are both the journal and expert logs

 
Kevin Onsongo #:

Here are both the journal and expert logs

You are trying to delete an order BEFORE it's fully placed.

See this article for detailed explanation.

https://www.mql5.com/en/articles/1111

MQL5 Cookbook: Processing of the TradeTransaction Event
MQL5 Cookbook: Processing of the TradeTransaction Event
  • www.mql5.com
This article considers capabilities of the MQL5 language from the point of view of the event-driven programming. The greatest advantage of this approach is that the program can receive information about phased implementation of a trade operation. The article also contains an example of receiving and processing information about ongoing trade operation using the TradeTransaction event handler. In my opinion, such an approach can be used for copying deals from one terminal to another.
 
Alain Verleyen #:

You are trying to delete an order BEFORE it's fully placed.

See this article for detailed explanation.

https://www.mql5.com/en/articles/1111

Thank you very much.

I realized that the order wasn't placed state until after some moments, sometimes even a whole second. Behavior is different in different brokers and symbols.

Since I need to grab the information very early, I modified the Chaser class to handle the cancelation of the order on its method that runs OnTick.

So, I will pass the ticket and try to delete it on the next tick, or when it is available.