Close all open orders when first order TP hit

 

Hi, I need some help please... I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...

The code below is closing all trades when any SL / TP hit.

   bool checked;
   if(ATR_TP1_CloseTrades)
   {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS))
         {
            if(OrderMagicNumber() != Magic) continue;
            string symbol = OrderSymbol();
            if(GlobalVariableGet(OrderSymbol() + "_stoptrading" + (string)Magic) == TimeDayOfYear(TimeCurrent()))
            {
               if(OrderType() == OP_BUY)
               {
                  checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), 0);
               }
               else if(OrderType() == OP_SELL)
               {
                  checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), 0);
               }
               else
               {
                  checked = OrderDelete(OrderTicket());
               }
            }
         }
      }
   }
 
katiz22: The code below is closing all trades when any SL / TP hit.

Where are you testing for “when the first open order TP is hit?”

 
William Roeder #:

Where are you testing for “when the first open order TP is hit?”

In live trade... the SL of an open order is hit but this should not close the symbol orders... only want this when first order tp hit..

 
katiz22 #: In live trade..

“In live trade” is not a place in your code. What part of «Where are you testing for “when the first open order TP is hit?”» was unclear?

 
William Roeder #:

“In live trade” is not a place in your code. What part of «Where are you testing for “when the first open order TP is hit?”» was unclear?

Sorry maybe my english not good I dont understand what you mean by where? It within - void OnTimer() function.

 
katiz22 #: I dont understand what you mean by where? It within - void OnTimer() function.

Which you never showed.

 
William Roeder #:

Which you never showed.

//+------------------------------------------------------------------+
void OnTimer()
{
   if(_StopFlag || !IsConnected()) return;
   UpdatePanel();
   if(!NewBarD)
   {
      if(BarTimeD < iTime(NULL, PERIOD_D1, 0))
      {
         NewBarD = true;
         BarTimeD = iTime(NULL, PERIOD_D1, 0);
      }
   }
   datetime dayStart = int(TimeCurrent() / 86400) * 86400;
   for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)
   {
      if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) return;
      if(OrderOpenTime() < dayStart) break;
      if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
      if(OrderMagicNumber() != Magic) continue;
      GlobalVariableSet(OrderSymbol() + "_stoptrading" + (string)Magic, TimeDayOfYear(TimeCurrent()));
   }
   bool checked;
   if(ATR_TP1_CloseTrades)
   {
      for(int i = OrdersTotal() - 1; i >= 0; i--)
      {
         if(OrderSelect(i, SELECT_BY_POS))
         {
            if(OrderMagicNumber() != Magic) continue;
            string symbol = OrderSymbol();
            if(GlobalVariableGet(OrderSymbol() + "_stoptrading" + (string)Magic) == TimeDayOfYear(TimeCurrent()))
            {
               if(OrderType() == OP_BUY)
               {
                  checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), 0);
               }
               else if(OrderType() == OP_SELL)
               {
                  checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), 0);
               }
               else
               {
                  checked = OrderDelete(OrderTicket());
               }
            }
         }
      }
   }

Please see now...

 
katiz22:

Hi, I need some help please... I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...

The code below is closing all trades when any SL / TP hit.

It's pretty simple. Assign a custom code to the comment field or a different magicnumber to this first order. After that, if you don't have this magic number, you can close all orders. Or if there is no special code in the comment section, it's like close all orders.

 

1. Loop through OrderHistory and see if any MagicNumber trade closed because of TP.

2. If so, close all other orders with the MagicNumber?

 
  1.    if(!NewBarD)
       {
          if(BarTimeD < iTime(NULL, PERIOD_D1, 0))
          {
             NewBarD = true;
             BarTimeD = iTime(NULL, PERIOD_D1, 0);
          }
       }
    
    Once set true, will always be true.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3
  2. Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!

  3. Where do you use it?


  4. Prefer positive logic over negative
       for(int i = OrdersHistoryTotal() - 1; i >= 0; i--)
       {
          if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) return;
          if(OrderOpenTime() < dayStart) break;
          if(OrderType() != OP_BUY && OrderType() != OP_SELL) continue;
          if(OrderMagicNumber() != Magic) continue;
    
    Easier to understand
       for(int i = OrdersHistoryTotal() - 1; i >= 0; i--) if(
          OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
       && OrderMagicNumber() == Magic
       && OrderOpenTime()    >= dayStart
       && OrderType()        <= OP_SELL
       ){
    
  5. MT4:

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum (2017)

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum (2012)
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 (2020)

  6.                if(OrderType() == OP_BUY)
                   {
                      checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), 0);
                   }
                   else if(OrderType() == OP_SELL)
                   {
                      checked = OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), 0);
    

    MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  7. katiz22: I would like my EA to close all open orders when the first open order TP is hit - for magic number, symbol...

    The code below is closing all trades when any SL / TP hit.

    You set the GV when you find a closed order, opened today. Let me ask the question a third time:
    William Roeder #: Where are you testing for “when the first open order TP is hit?”
    Even andrew implied the same question:
    andrew #: Loop through OrderHistory and see if any MagicNumber trade closed because of TP.
 
William Roeder #:
  1. Once set true, will always be true.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3
  2. Use not equal. There was a post around 2002, where a server clock was mis-set and then corrected — stopped the EA for days!

Thanks for this - I was having this issue at times with EA not working but couldnt understand I will review.