Open trade, open pending order, reach TP, close all

 
Hi everyone,
 I'm trying to write mql4 code that....
 as soon as the last open trade in chronological order closes due to reaching TP, all the other open trades and all pending orders also close.

 Obviously I'm not an expert, however I tried different solutions but I was stuck.
 Can anyone give me suggestions?
 Thank you
 
Paul9 Can anyone give me suggestions?

You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your difficulty.
          No free help (2017)

Or pay someone. Top of every page is the link Freelance.
          Hiring to write script - General - MQL5 programming forum (2018)

We're not going to code it for you (although it could happen if you are lucky or the issue is interesting).
          No free help (2017)

 

William thanks,

I never use this forum so I didn't know the rules. This is what I tried to do, but it doesn't do what I intended.


// Funzione per cancellare tutti gli ordini pendenti
void CancelPendingOrders()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            if (OrderType() == OP_BUYLIMIT || OrderType() == OP_SELLLIMIT)
            {
                OrderDelete(OrderTicket());
            }
        }
    }
}

// Funzione OnTick
void OnTick()
{
    // Verifica se non ci sono altri trade normali aperti
    if (!AreThereOpenNormalTrades())
    {
        double openPrice = Ask; // Prezzo Ask come prezzo di apertura
        
        // Apri il trade normale
        OpenNormalTrade(openPrice);
        
        // Apri il trade buy limit
        OpenBuyLimitTrade(openPrice);
    }
    
    // Verifica se un trade ha chiuso per TP e cancella tutti gli ordini pendenti
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
        {
            if (OrderType() == OP_BUY || OrderType() == OP_SELL)
            {
                if (OrderClosePrice() != 0.0)
                {
                    CancelPendingOrders();
                    break;
                }
            }
        }
    }
}
 
  1. On a buy/sell, OrderClosePrice() is always non-zero (just Bid/Ask).
  2. Why are you deleting pending orders, when you have any open order? That isn't what you originally wanted.
  3. Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy.
         Trade current timeframe, one strategy, and filter by symbol requires one MN.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

 
William Roeder #:
OrderClosePrice

Ok William,

It seems impossible for me to proceed, I thought it would be much simpler.

I clearly have no skills to write code. However, I learned something new.

I thank you for the answer

Reason: