OrderClose sometimes randomly close my trades when changing timeframe

 
hi as stated on my title, I put a button that closes all my open trade at the same time when I press it, but sometimes when I change my timeframe in mt4 it randomly activate without me pressing the button, here is the code of my button


if(id==CHARTEVENT_OBJECT_CLICK)
              {
       if(sparam =="Close Trades And Orders")
              {  
                  
                CloseBuyOrder();
                ObjectSetInteger(0,"Close Trades And Orders",OBJPROP_STATE,false);   
                                 
              }       
              }
and here is the code i used for CLoseBuyOrder()

void CloseBuyOrder()
{
  for(int i=OrdersTotal()-1;i>= 0 ;i--)
{
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
       {
         if(OrderSymbol()==Symbol() && OrderType() == OP_BUY)
         {
           if ( OrderClose(OrderTicket(),OrderLots(),Bid,0,NULL) )   continue;
                Print("OrderClose error ",GetLastError()); 
                return;      
         } 
       }
}  
}  


thanks guys 

 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2.    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
           {
             if(OrderSymbol()==Symbol() && OrderType() == OP_BUY)
             {
               if ( OrderClose(OrderTicket(),OrderLots(),Bid,0,NULL) )   continue;

    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)

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

 
William Roeder #:

Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
          General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
Next time, post in the correct place. The moderators will likely move this thread there soon.

oops sorry i got confused. thanks

Btw thank you for your answer on my question i will try it :)