MQL4| Open or close order after a certain number of hours have passed [Help]

 

I'm trying to code a function on my expert advisor that opens or closes a trade after a certain number of hours, my idea is to work similarly to sure-fire hedging but my problem is when I test my code it opens like 9 or more trades at a time and I don't understand why.


void checkCloseTimer()
{
   
   for (i=OrdersTotal()-1; i>=0 ; i--)
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

      datetime myTime = TimeLocal();
      
      MqlDateTime myTimeStruct;
      
      TimeToStruct(myTime, myTimeStruct);
      
      int currentHour = myTimeStruct.hour;
      
      string timeWithSeconds = TimeToString (TimeLocal(), TIME_DATE|TIME_SECONDS);
      
      datetime openTime = OrderOpenTime();
      
      MqlDateTime myOpenTimeStruct;
      
      TimeToStruct(OrderOpenTime(), myOpenTimeStruct);
      
      int openHour = myOpenTimeStruct.hour;
      
      string openTimeWithSeconds = TimeToString (OrderOpenTime(), TIME_DATE|TIME_SECONDS);
      
      int timeDifference = currentHour - openHour;
      
      
      if (timeDifference > 0)
      {
         if (OrderProfit() < 0)
         {
            
            if (OrderType() == OP_SELL)
               //OrderClose (OrderTicket(), OrderLots(), Ask, Slippage, NULL);
               //OrderSend (Symbol(),OP_BUY, OrderLots()*(1.5), Ask, 5, Ask-150*Point(), Ask+300*Point(), NULL, MagicNumber, 0, Green);
               OpenPosition(OP_BUY); 
               
            }
            
            if (OrderType() == OP_BUY){
               //OrderClose (OrderTicket(), OrderLots(), Bid, Slippage, NULL);
               //OrderSend (Symbol(),OP_SELL, OrderLots()*(1.5), Bid, 5, Bid+150*Point() , Bid-300*Point() , NULL, MagicNumber, 0, Red);
               OpenPosition(OP_SELL); 
               
            }
         }
      }
   }
 }
 
  1. Don't post pictures of code, they are too hard to read.

    Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. 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?
    Next time post in the correct place. The moderators will likely move this thread there soon.

 
Don't use local time. OrderOpenTime is broker time zone. You don't need TimeToStruct. Just compare TimeCurrent() - OrderOpenTime() >=  certain number of hours * 3600
 
William Roeder:
Don't use local time. OrderOpenTime is broker time zone. You don't need TimeToStruct. Just compare TimeCurrent() - OrderOpenTime() >=  certain number of hours * 3600

Thanks I'll try It right away

 
When I've experimented with a similar concept it proved to be useful to have a boolean variable indicating if it is OK or not to open a new position. Because on an H1 timescale the "hours passed" condition will still be true even after your EA opened like 5-10 positions...
 
VikMorroHun:
When I've experimented with a similar concept it proved to be useful to have a boolean variable indicating if it is OK or not to open a new position. Because on an H1 timescale the "hours passed" condition will still be true even after your EA opened like 5-10 positions...

I understand, and every time the condition is fulfilled the Boolean variable resets. Did you use as a Global or local ?