How to count Today Open Order in MQL5

 

Hello, i want to count Today Open Order, i made a code, is always return the value 0. Can anyone get a help ? please.


int CheckTodayOpenOrder(int MagicNr)
{  
   int TodaysOrders = 0;
   
   uint TotalNumberOfDeals=HistoryDealsTotal();
   ulong Ticket;
   string OrderSymbol;
   int Magic = 0;
   datetime OrderOpentime = "";

   HistorySelect(0,TimeCurrent());
   
   for(int i=0; i<TotalNumberOfDeals; i++)
   {
      Ticket = HistoryDealGetTicket(i);
      OrderSymbol = HistoryDealGetString(Ticket,DEAL_SYMBOL);
      Magic = HistoryDealGetInteger(Ticket,DEAL_MAGIC);
      OrderOpentime = HistoryDealGetInteger(Ticket,DEAL_TIME);
      if(OrderSymbol == Symbol())
      if(Magic == MagicNr)
      if(TimeDayOfYearMQL4(OrderOpentime) == TimeDayOfYearMQL4(TimeCurrent()) )
      {
         TodaysOrders += 1;
      }
   }

   return(TodaysOrders);
}


int TimeDayOfYearMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_year);
  }
 
To Pui Kuen:

Hello, i want to count Today Open Order, i made a code, is always return the value 0. Can anyone get a help ? please.


 if(TimeDayOfYearMQL4(OrderOpentime) == TimeDayOfYearMQL4(TimeCurrent()) )

what i know is that the order open time will not be equal the current time when the function is called...the deal already took place before the script is called

 
roshjardine:

what i know is that the order open time will not be equal the current time when the function is called...the deal already took place before the script is called

int TimeDayOfYearMQL4(datetime date)
  {
   MqlDateTime tm;
   TimeToStruct(date,tm);
   return(tm.day_of_year);
  }

Thanks ! But i use this code to turn the datetime to TimeDayofYear ( day 1..2...3..4..etc )

 
To Pui Kuen:

Thanks ! But i use this code to turn the datetime to TimeDayofYear ( day 1..2...3..4..etc )

then you can try "greater than"  today 00:00 and "less than equal" current time..that's the correct way to get deals that happened today until current time
 
roshjardine:
then you can try "greater than"  today 00:00 and "less than equal" current time..that's the correct way to get deals that happened today until current time

Thank for advice.  But if i am delete this code 

  if(TimeDayOfYearMQL4(OrderOpentime) == TimeDayOfYearMQL4(TimeCurrent()) )

the above code also return me "0". 

Can you see what is the problem about this code

 
To Pui Kuen:

Thanks ! But i use this code to turn the datetime to TimeDayofYear ( day 1..2...3..4..etc )

or TimeToStruct the order open time to MqlDateTime and get the day and compare with the returned day of
TimeDayOfYearMQL4
 
To Pui Kuen :

Hello, i want to count Today Open Order, i made a code, is always return the value 0. Can anyone get a help ? please.


Example:

//+------------------------------------------------------------------+
//|                                           Number Deals Today.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
//--- input parameters
input group             "Additional features"
input ulong    InpMagic             = 200;         // Magic number
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   MqlDateTime STimeToday;
   TimeToStruct(TimeCurrent(),STimeToday);
   STimeToday.hour            = 0;
   STimeToday.min             = 0;
   STimeToday.sec             = 0;
   datetime from_date         = StructToTime(STimeToday);    // From date
   datetime to_date           = from_date+24*60*60*3;          // To date
//--- request trade history
   HistorySelect(from_date,to_date);
   uint total_deals           = HistoryDealsTotal();
   ulong ticket_history_deal  = 0;
//---
   int count_buys_in=0,count_buys_out=0,count_sells_in=0,count_sells_out=0;
//--- for all deals
   for(uint i=0; i<total_deals; i++)
     {
      //--- try to get deals ticket_history_deal
      if((ticket_history_deal=HistoryDealGetTicket(i))>0)
        {
         //--- get deals properties
         long     deal_type         = HistoryDealGetInteger(ticket_history_deal,DEAL_TYPE);
         long     deal_entry        = HistoryDealGetInteger(ticket_history_deal,DEAL_ENTRY);
         long     deal_magic        = HistoryDealGetInteger(ticket_history_deal,DEAL_MAGIC);
         string   deal_symbol       = HistoryDealGetString(ticket_history_deal,DEAL_SYMBOL);
         //--- only for current symbol
         if(deal_symbol==Symbol() && deal_magic==InpMagic)
           {
            //---
            if(deal_type==DEAL_TYPE_BUY)
              {
               if(deal_entry==DEAL_ENTRY_IN)
                  count_buys_in++;
               if(deal_entry==DEAL_ENTRY_OUT)
                  count_buys_out++;
              }
            if(deal_type==DEAL_TYPE_SELL)
              {
               if(deal_entry==DEAL_ENTRY_IN)
                  count_sells_in++;
               if(deal_entry==DEAL_ENTRY_OUT)
                  count_sells_out++;
              }
           }
        }
     }
   Comment("Today: ",
           "| Buys IN ",IntegerToString(count_buys_in),
           "| Buys OUT ",IntegerToString(count_buys_out),
           "| Sells IN ",IntegerToString(count_sells_in),
           "| Sells OUT ",IntegerToString(count_sells_out));
  }
//+------------------------------------------------------------------+


Result:


Files:
 
roshjardine:
or TimeToStruct the order open time to MqlDateTime and get the day and compare with the returned day of
int CheckTodayOpenOrder(int MagicNr)
{  
   int TodaysOrders = 0;
   
   uint TotalNumberOfDeals=HistoryDealsTotal();
   ulong Ticket;
   string OrderSymbol;
   int Magic = 0;
   datetime OrderOpentime = "";

   HistorySelect(0,TimeCurrent());
   
   for(int i=0; i<TotalNumberOfDeals; i++)
   {
      Ticket = HistoryDealGetTicket(i);
      OrderSymbol = HistoryDealGetString(Ticket,DEAL_SYMBOL);
      Magic = HistoryDealGetInteger(Ticket,DEAL_MAGIC);
      OrderOpentime = HistoryDealGetInteger(Ticket,DEAL_TIME);
      if(OrderSymbol == Symbol())
      if(Magic == MagicNr)
      {
         TodaysOrders += 1;
      }
   }

   return(TodaysOrders);
}

above code i also return the value "0". I dont know why.... Can you see any error of this?

 
To Pui Kuen:

above code i also return the value "0". I dont know why.... Can you see any error of this?


      MqlDateTime OrderTime;
      TimeToStruct(OrderOpentime,OrderTime);
      if(OrderSymbol == Symbol())
      if(Magic == MagicNr)
      if(OrderTime.day_of_year == TimeDayOfYearMQL4(TimeCurrent()) )
 
To Pui Kuen:

above code i also return the value "0". I dont know why.... Can you see any error of this?

you can just use the code provided by Vladimir, it's really clear
 
Vladimir Karputov:

Example:


Result:


Thanks! Let me study your code and try...

Reason: