Download MetaTrader 5

Check for specific time

To add comments, please log in or register
Do you often publish source codes? Find out how to do that correctly!
nikoscher
105
nikoscher 2014.09.07 13:54 

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!

Dua Yong Rew
5422
Dua Yong Rew 2014.09.07 18:07  
//+------------------------------------------------------------------+
//| 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);
  }
nikoscher
105
nikoscher 2014.09.07 19:14  

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 :/

Alain Verleyen
Moderator
30707
Alain Verleyen 2014.09.07 21:54  
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.

nikoscher
105
nikoscher 2014.09.07 22:21  

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


Greetings

Alain Verleyen
Moderator
30707
Alain Verleyen 2014.09.07 22:40  
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.
nikoscher
105
nikoscher 2014.09.07 23:13  

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??

Ahmed Soliman
15713
Ahmed Soliman 2014.09.08 02:12  
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() .
Alain Verleyen
Moderator
30707
Alain Verleyen 2014.09.08 12:34  
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
nikoscher
105
nikoscher 2014.09.08 23:55  

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?

To add comments, please log in or register