Download MetaTrader 5
To add comments, please log in or register
Have you discovered something really important? Write an article and receive 200 USD!
Dua Yong Rew
5102
Dua Yong Rew 2013.03.04 07:46 

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?

phi nuts
2185
phi nuts 2013.03.04 07:52  
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 ?
Dua Yong Rew
5102
Dua Yong Rew 2013.03.04 07:57  
phi.nuts:
Does using CopyTime with PERIOD_D1 helps ?
Can give me an example how to extract current date from copytime?
phi nuts
2185
phi nuts 2013.03.04 08:13  
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.

 

Dua Yong Rew
5102
Dua Yong Rew 2013.03.04 08:32  
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? 

phi nuts
2185
phi nuts 2013.03.04 08:48  
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

Dua Yong Rew
5102
Dua Yong Rew 2013.03.04 08:54  
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.
Dua Yong Rew
5102
Dua Yong Rew 2013.03.18 06:41  

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;
      }
   }

 

Dua Yong Rew
5102
Dua Yong Rew 2013.03.18 06:42  
oops, i forgot to add a break to the loop if conditions is found true.
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2013.03.18 09:42  
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;
      }
   }
Dua Yong Rew
5102
Dua Yong Rew 2013.03.19 04:00  
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
/ /12
To add comments, please log in or register