Why is my code not executed after the TP hits?

 

I am totally new here and trying to code something like a simple open and close.

However the EA is hitting the TP it is not continuing with anything else in the code.

The part of the code to check and close is not even triggered as I don't see the print messages in the journal.

Can someone explain the mechanic/logic when the trade position hits the TP?

Much appreciated

      if (PositionSelectByTicket(positionTicket))
      {
         int type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
         double tp = PositionGetDouble(POSITION_TP);
         double sl = PositionGetDouble(POSITION_SL);
         double currentPrice = (type == POSITION_TYPE_BUY) ? SymbolInfoDouble(Symbol(), SYMBOL_BID) : SymbolInfoDouble(Symbol(), SYMBOL_ASK);
      
         // Check if TP or SL is hit and close position if so
         bool shouldClose = false;
      
         if (type == POSITION_TYPE_BUY && currentPrice >= tp)
         {
            shouldClose = true;
            Print("1 XXX TAKE PROFIT Triggered BUY");
         }
         else if (type == POSITION_TYPE_SELL && currentPrice <= tp)
         {
             shouldClose = true;
             Print("2 XXX TAKE PROFIT Triggered SELL");
         }
         else if (PositionGetDouble(POSITION_SL) != 0 && ((type == POSITION_TYPE_BUY && currentPrice <= sl) || (type == POSITION_TYPE_SELL && currentPrice >= sl)))
         {    
             shouldClose = true;
             Print("2 XXX STOP LOSS Triggered BUY/SELL");
         }
         if (shouldClose)
         {
            for(int i=PositionsTotal()-1; i>=0; i--)
            {
                ulong ticket = PositionGetTicket(i);
                if(PositionSelectByTicket(ticket))
                {
                    string symbol = PositionGetString(POSITION_SYMBOL);
                    ENUM_POSITION_TYPE type = (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);
                    double volume = PositionGetDouble(POSITION_VOLUME);
                    
                    // Close the position
                    if(!trade.PositionClose(ticket))
                    {
                        Print("Failed to close position for ", symbol, ". Error: ", GetLastError());
                    }
                    else
                    {
                        Print("Position for ", symbol, " closed successfully.");
                        Print("Position closed at TP/SL: ", currentPrice);
                        active_trade = false;
               
                        // Loop through the history to find the deal         
                        for (int i = 0; i < HistoryDealsTotal(); i++)
                        {
                            ulong deal_ticket = HistoryDealGetTicket(i);
                            if (deal_ticket == positionTicket)
                            {
                                // Get the close time of the deal
                                datetime close_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
                                if(close_time)
                                {
                                    Print("Close Time",close_time);
                                    priceReEnteredRange = false;
                                    DrawOrUpdateLine(slLineName, open_time_ticket, slLevel, close_time, slLevel, clrBlueViolet, "SL_Line");
                                    DrawOrUpdateLine(tpLineName, open_time_ticket, tpLevel, close_time, tpLevel, clrYellowGreen, "TP_Line");
                                    DrawOrUpdateRectangle(tpRectName, open_time_ticket, price_start, close_time, tpLevel, clrGreen);
                                    DrawOrUpdateRectangle(slRectName, open_time_ticket, slLevel, close_time, price_start, clrRed);
                                }
                                else
                                {
                                    Print("Close Time incorrect",close_time);
                                }
                            }
                            else
                            {
                              Print("Close Ticket not Found");
                            }
                        }
                    }
                }
            }
         }
      }
 

Deal information is accessed at a different time to position (active position) information.

You have the deal scan loop inside the position scan loop, it shouldn't be like that.

This needs to be outside the position scan loop in its own scope:

                // Loop through the history to find the deal         
                        for (int i = 0; i < HistoryDealsTotal(); i++)
                        {
                            ulong deal_ticket = HistoryDealGetTicket(i);
                            if (deal_ticket == positionTicket)
                            {
                                // Get the close time of the deal
                                datetime close_time = (datetime)HistoryDealGetInteger(deal_ticket, DEAL_TIME);
                                if(close_time)
                                {
                                    Print("Close Time",close_time);
                                    priceReEnteredRange = false;
                                    DrawOrUpdateLine(slLineName, open_time_ticket, slLevel, close_time, slLevel, clrBlueViolet, "SL_Line");
                                    DrawOrUpdateLine(tpLineName, open_time_ticket, tpLevel, close_time, tpLevel, clrYellowGreen, "TP_Line");
                                    DrawOrUpdateRectangle(tpRectName, open_time_ticket, price_start, close_time, tpLevel, clrGreen);
                                    DrawOrUpdateRectangle(slRectName, open_time_ticket, slLevel, close_time, price_start, clrRed);
                                }
                                else
                                {
                                    Print("Close Time incorrect",close_time);
                                }
                            }
                            else
                            {
                              Print("Close Ticket not Found");
                            }
                        }


You also NEED to use "HistorySelect" or "HistorySelectByPosition" as it says in the documentation:

https://www.mql5.com/en/docs/trading/historydealgetticket

Documentation on MQL5: HistoryDealGetTicket / Trade Functions
Documentation on MQL5: HistoryDealGetTicket / Trade Functions
  • www.mql5.com
The function selects a deal for further processing and returns the deal ticket in history. Prior to calling HistoryDealGetTicket(), first it is...