Getting current date without time

 

Hi

I'm trying to get the current date without the time using this code

MqlDateTime Time;
TimeCurrent(Time);

string CurrDate = IntegerToString(Time.year) + "." + IntegerToString(Time.mon) + "." + IntegerToString(Time.day);

HistorySelect(StringToTime(CurrDate), TimeCurrent());

 are there any simpler ways of getting the current date only?

 
doshur:

Hi

I'm trying to get the current date without the time using this code

 are there any simpler ways of getting the current date only?

Does using CopyTime with PERIOD_D1 helps ?
 
phi.nuts:
Does using CopyTime with PERIOD_D1 helps ?
Can give me an example how to extract current date from copytime?
 
doshur:
Can give me an example how to extract current date from copytime?

Sample ? I thought you can do this yourself ;D

  datetime dt1[1];
  
  if (CopyTime (_Symbol, PERIOD_D1, 0, 1, dt1) == 1)
     {
     Print(TimeToString (dt1 [0], TIME_DATE| TIME_SECONDS));
     }

The problem with your first code is this : when using today date March 04, 2013, your code returned string 2013.3.4, while it should be string 2013.03.04.

 

 
phi.nuts:

Sample ? I thought you can do this yourself ;D

The problem with your first code is this : when using today date March 04, 2013, your code returned string 2013.3.4, while it should be string 2013.03.04.

 

Thanks for the comment.

Other than copytime is there any other methods? 

 
doshur:

Thanks for the comment.

Other than copytime is there any other methods? 

Yes, it's called LOL ...

string LOL = TimeToString (TimeCurrent(), TIME_DATE);
Print (StringToTime(LOL));

being lazy is no good, doshur ;D

 
phi.nuts:

Yes, it's called LOL ...

being lazy is no good, doshur ;D

This seems to be much better. Appreciate the keywords and sample.
 

this is the reason why i asked this question. to develop one trade a day code. i can easily use new bar tactics but if EA got restarted, it will trade again.

here u go...

   static bool OneTrade;

   OneTrade = true;

   string CurrDate = TimeToString(TimeCurrent(), TIME_DATE);

   HistorySelect(StringToTime(CurrDate), TimeCurrent());

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

      if(Entry == DEAL_ENTRY_IN)
      {
         OneTrade = false;
      }
   }

 

 
oops, i forgot to add a break to the loop if conditions is found true.
 
doshur:

this is the reason why i asked this question. to develop one trade a day code. i can easily use new bar tactics but if EA got restarted, it will trade again.

here u go...

You can also remove the messing about with strings stuff . . .

 

#define SECONDSINADAY  86400


   static bool OneTrade;
   datetime Midnight, StartOfNewYear;
   

   Midnight = TimeCurrent() - ( TimeCurrent()%SECONDSINADAY );  // midnight today as a datetime

   OneTrade = true;

   HistorySelect(Midnight, TimeCurrent());            // just history between Midnight and now

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

      if(Entry == DEAL_ENTRY_IN)
      {
         OneTrade = false;
      }
   }
 
RaptorUK:

You can also remove the messing about with strings stuff . . .

 

let me compress the code a little

   static bool OneTrade;

   OneTrade = true;

   HistorySelect(TimeCurrent() - (TimeCurrent()%86400), TimeCurrent());   // just history between Midnight and now

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

      if(Entry == DEAL_ENTRY_IN)
      {
         OneTrade = false;
      }
   }

https://www.mql5.com/en/docs/basis/operations/mathoperation

smart use of % 

Documentation on MQL5: Language Basics / Operations and Expressions / Arithmetical Operations
Documentation on MQL5: Language Basics / Operations and Expressions / Arithmetical Operations
  • www.mql5.com
Language Basics / Operations and Expressions / Arithmetical Operations - Documentation on MQL5
Reason: