How to check if there is orders which closed within 2hours.

 

Hi, I need your idea.

I want to check if there is orders which closed withing 2hours from current time.


If there is closed order within 2hours = true.

If there is no closed order within 2hours = false.


My code

bool OrderClosedwithin2hours()
  {
   datetime closetime = CurrentTime()-2*???  //How can I code here?

   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol()      != _Symbol)  continue;
      if(OrderMagicNumber() != magic_no) continue;
      if(OrderCloseTime()    >= closetime){return(true);}
       else{return(false);}
     }
   
}


  Thank you for your tips!

 
  1. There is no function called “CurrentTime” in MT4/5. Do not post code that will not even compile!
  2. Time is in seconds; so your “???” should be 3600 (hour).
  3. You return false if the latest history entry is older than your “closetime.” Finish the loop, then return false.

    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.06.08)

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

  5. Avoid using negative logic (if(x)continue. Simplify

       for(int i=OrdersHistoryTotal()-1; i>=0; i--) if(
          OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)
       && OrderMagicNumber() == magic_no
       && OrderType()        <= OP_SELL   // № 4
       && OrderCloseTime()   >= closetime
       && OrderSymbol()      == _Symbol   // Last: string comparisons are slowest.
       ){ 
          return true;
       }

Reason: