Check for specific time

 

Hey there, i am trying to check if in last two hours there was an ordertype:

   if( OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY) == true)
   {
      if (OrderType()==OP_BUY)
      {
         Print("Last order type: ",OrderType());
      }
      if (OrderType()==OP_SELL)
      {
         Print("Last order type: ",OrderType());
      }
   }

But in documentation i found only how to check the historyTotal, but not how to check "on time", are there any possibilities in mql4?

anybody knows about such a function/method?


Greetings!

 
//+------------------------------------------------------------------+
//| Trade Count                                                      |
//+------------------------------------------------------------------+
int TradeCount(ENUM_TIMEFRAMES TimeFrame)
  {
//---

   int      Cnt;
   ulong    Ticket;
   long     EntryType;
   datetime DT[1];

   Cnt = 0;

   if(CopyTime(_Symbol, TimeFrame, 0, 1, DT) <= 0)
   {
      Cnt = -1;
   }
   else
   {
      HistorySelect(DT[0], TimeCurrent());

      for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
      {
         Ticket    = HistoryDealGetTicket(i);
         EntryType = HistoryDealGetInteger(Ticket, DEAL_ENTRY);

         if(EntryType == DEAL_ENTRY_IN)
         {
            if(_Symbol == HistoryDealGetString(Ticket, DEAL_SYMBOL))
            {
               Cnt++;
            }
         }
      }
   }

//---
   return(Cnt);
  }
 

very interesting code, what i inserted:

   int TradeCount(ENUM_TIMEFRAMES PERIOD_H1)
   {
      int      Cnt;
      ulong    Ticket;
      long     EntryType;
      datetime DT[1];

      Cnt = 0;

      if(CopyTime(_Symbol, PERIOD_H1, 0, 1, DT) <= 0)
      {
         Cnt = -1;
      }
      else
      {
         HistorySelect(DT[0], TimeCurrent());

         for(int i = HistoryDealsTotal() - 1; i >= 0; i--)
         {
            Ticket    = HistoryDealGetTicket(i);
            EntryType = HistoryDealGetInteger(Ticket, DEAL_ENTRY);

            if(EntryType == DEAL_ENTRY_IN)
            {
               if(_Symbol == HistoryDealGetString(Ticket, DEAL_SYMBOL))
               {
                  Cnt++;
               }
            }
         }
      }
      return(Cnt);
   }

But there is nothing returned, there is an error somewhere in code, but i dont know where?

You sure about this?

Greetings


EDIT:

When i just "insert" your code, the script doesnt work, error somewhere :/

 
nikoscher:

very interesting code, what i inserted:

But there is nothing returned, there is an error somewhere in code, but i dont know where?

You sure about this?

Greetings


EDIT:

When i just "insert" your code, the script doesnt work, error somewhere :/

Of course it doesn't work, the code provided by doshur is mql5, you need mql4.

 

ah ok, so there is no possibilty in mql4 to check from the current tick if there was an order in last hour?


Greetings

 
nikoscher:

ah ok, so there is no possibilty in mql4 to check from the current tick if there was an order in last hour?


Greetings

Yes it's possible, compare the OrderOpenTime() or maybe the OrderCloseTime(), with time one hour ago.
 

you sure?

i am asking because this is a sample return value for orderCloseTime() :

1406839062

What does it mean?

Do i have to split this string??

 
nikoscher:

you sure?

i am asking because this is a sample return value for orderCloseTime() :

What does it mean?

Do i have to split this string??

This is a datetime type (the number of seconds elapsed since January 01, 1970). Compare it to TimeCurrent() .
 
nikoscher:

you sure?

i am asking because this is a sample return value for orderCloseTime() :

What does it mean?

Do i have to split this string??

Do you know what documentation is ?

OrderOpenTime().

datetime

Please check it, read it, understand it before asking on the forum.

OrderOpenTime - MQL4 Documentation
  • docs.mql4.com
OrderOpenTime - MQL4 Documentation
 

Intention: Maximal one trade per hour.

i already thought about this and created this snippet(mql4):

// Get the last order
if (OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS, MODE_HISTORY) == true)
{
    // Get it´s closing time
    string orderclosetime=OrderCloseTime();
    
    // Get current time
    string timecurrent=TimeCurrent();

    // Substract them
    string minimum = StrToInteger(timecurrent) - StrToInteger(orderclosetime);
}

if(minimum > 3600)
{
    // Last trade has been closed minimum 1 hour (3600 seconds) ago.
    // Buy or Sell
}

But this snippet doesnt work :/

I checked in backtest and i saw e.g. eight trades within an hour.

But this shouldn´t be.

Anybody could refer to it?

Reason: