Close order before Candle Close MQL5

 

Hi al!!, quick question, how can I set my EA in mql5 to close a pending order but before candle close? I mean, this is my situation:

I have  open trades (one long and one short, with TP @  tpLongLevel  and  tpShortLevel) , and I also have a pending stop order, so one of my first trades hits TP, and I want automatically to cancel all pending orders then, at the same instant. What I have achieved is the system to close pending orders, after the closing of the open bar where my open trade had TP´ed, x instance, my EA trades on the 1h timeframe, so if I hit TP in the first 10mins of the 1H candle, then it takes 50 mins to cancel all pending orders, instead of cancelling instantly all pending orders.

Thanks!

this is how I coded it:

inside ontick:

   if ((currentTick.bid >= tpLongLevel) || (currentTick.ask <= tpShortLevel)){

      CancelOrder();





function CancelOrder:


void CancelOrder(){



   //Check all orders

   for(int i=OrdersTotal()-1; i>=0; i--){

      //Get ticket number

      ulong OrderTicket = OrderGetTicket(i);

      

      //Delete pending orders

      trade.OrderDelete(OrderTicket);

   }
 
void OnTick()
{
    // Get the current bid and ask prices
    double currentBid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    double currentAsk = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

    // Get the time remaining till the end of the current candle
    datetime nextCandleTime = (TimeCurrent() / PeriodSeconds(_Period)) * PeriodSeconds(_Period) + PeriodSeconds(_Period);
    int secondsToNextCandle = nextCandleTime - TimeCurrent();

    // Iterate through all open positions
    for (int i = PositionsTotal() - 1; i >= 0; i--)
    {
        // Get the position details
        ulong positionTicket = PositionGetTicket(i);
        double tpLongLevel = PositionGetDouble(POSITION_TP);
        double tpShortLevel = PositionGetDouble(POSITION_TP);

        // Check if the position has reached the take profit level
        if ((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && currentBid >= tpLongLevel) ||
            (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL && currentAsk <= tpShortLevel))
        {
            // Check if there is enough time remaining till the end of the current candle
            if (secondsToNextCandle > 10) // Replace 10 with the desired number of seconds
            {
                // Cancel all pending orders
                CancelPendingOrders();

                // Add any additional actions you want to perform when the take profit level is reached
                // ...
            }
        }
    }
}

void CancelPendingOrders()
{
    // Same as before
}

10 sec before bar close.
 
If you want to do it before candle close there is no workaround. Because you never know which tick is the last tick of bar.
But if you want to do it at the moment the position's hit TP you can use onTradeTransaction event handler to do what you want at the right moment.
 

OnTradeTransaction is surely the most accurate way of achieving what you want.

But anyway, also a simple check for each tick can be enough. Based on your explaination the logic should be very easy: if EA can't find a buy and sell trade at market, it delete all pendings. You don't need to check timing or remaining time of the bar.

Reason: