Download MetaTrader 5

How can i set an external datetime variable to the current time?

To add comments, please log in or register
bunnycat
75
bunnycat  
Hi, is this possible?  Really don't want to constantly change every part of the date/time/year/ every time i need a little adjustment.  I want the default when I start the EA to be the current datetime.  Can anyone help with this?  Thanks.
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Show us your code. Use SRC (besides video icon) when posting codes.
Keith Watford
Moderator
9641
Keith Watford  
bunnycat:
Hi, is this possible?  Really don't want to constantly change every part of the date/time/year/ every time i need a little adjustment.  I want the default when I start the EA to be the current datetime.  Can anyone help with this?  Thanks.

If you want the default to be the current time, why use an external variable?

If it is an extern, you can simply change the value to TimeCurrent() in init, then it will reset every time it is initialized. If you don't want it to reset when changing chart time-frame you will have to set other conditions as well. 

whroeder1
14779
whroeder1  
If you want the extern to specify a time, but use current time if not specified, use zero and test for it.
extern datetime Next_News=0;
:
if(TimeCurrent() >= Next_News) ...
bunnycat
75
bunnycat  

//---- input parameters 

extern bool       Trade_Disabled = true;
extern datetime   Trade_Time = D'2014.06.01 00:00';
extern bool       Close_Disabled = true;
extern datetime   Close_Time = D'2014.06.01 00:00';
extern ENUM_ORDER_TYPE  Type = 0;
extern double     Lots=1.0;
extern bool       Use_Max_Lots = false;
extern double  Max_Lots_Offset = 0.01;
extern int        TP = 0;

extern int        Slippage = 50;

 

Hi, thanks guys.  Maybe I need to be clearer.  So when I run this, I almost always am setting a time very soon in the future.  I never want it to actually use the current time, but when I go in to set the variable, I don't want to have to change date/month/hour etc.  It's usually just minutes that I want to change.  Maybe GumRai provided a solution that would do this, but I don't quite understand it.  I am also not sure if it's possible, but I hope so. 

Keith Watford
Moderator
9641
Keith Watford  

An easier option may be to just add a number of minutes to the current time when the EA is initiated

//Globalscope
extern int Plus_Minutes=5;
datetime   Trade_Time;

//init
Trade_Time=TimeCurrent() + ((Plus_Minutes*60);

 Of course this will be reset if you change time-frames or re-initialize for any other reason.

bunnycat
75
bunnycat  
hey... that's a solution I never thought of...  I will implement that for now in the absence of the alternative.  Thanks GumRai!!
Nathan5
7
Nathan5  
bunnycat:
hey... that's a solution I never thought of...  I will implement that for now in the absence of the alternative.  Thanks GumRai!!

I don't like this as a solution.

My EA takes in 6 times, which is essential for its operation. Each time requires the date as well, so every time I run/test it I have to re-enter all six dates plus the specific times I want.

That's just not on.

So I thought of another solution.

I accept that the date can always be today's date. Get the times in string format (e.g. "13:25"), and then include this function in the program:

datetime TimeOnlyConvert(string usertime)
   {
   int vyy=Year();
   int vmm=Month();
   int vdd=Day();
   int vhh=Hour();
   int vmi=Minute();

   string datestring=IntegerToString(vyy)+"."+IntegerToString(vmm)+"."+IntegerToString(vdd)+" "+usertime;
   datetime datebuild=StringToTime(datestring);
   return datebuild;
   }

Now for every time input (only the time) in text format, you can get an accurate datetime variable to use in the program.

To add comments, please log in or register