Help to set time point

 

Please help to set time points:


I want to set a time point that's sometime before a given time, for example:


TodayDatetime=2009.07.09 0:00, to calculate a time point that's two weeks earlier:


TwoWeeksDatetime=TodayDatetime - 2*7*24, time unit in hour, the problem is how to make this work?


Thanks,


Jean

 
MontS:

Please help to set time points:


I want to set a time point that's sometime before a given time, for example:


TodayDatetime=2009.07.09 0:00, to calculate a time point that's two weeks earlier:


TwoWeeksDatetime=TodayDatetime - 2*7*24, time unit in hour, the problem is how to make this work?


Thanks,


Jean

TwoWeeksDatetime=TodayDatetime-2*7*24*3600 ; it's simple integer operation

Then display on chart using a VLINE object.

 
MontS wrote >>

Please help to set time points:

I want to set a time point that's sometime before a given time, for example:

TodayDatetime=2009.07.09 0:00, to calculate a time point that's two weeks earlier:

TwoWeeksDatetime=TodayDatetime - 2*7*24, time unit in hour, the problem is how to make this work?

Thanks,

Jean

See Date & Time functions

 
TheEconomist:

TwoWeeksDatetime=TodayDatetime-2*7*24*3600 ; it's simple integer operation

Then display on chart using a VLINE object.

Yes? I doubt that'll work, I just want to set that exact time point in hour. I want to count hourly bar from that point

 
MontS:

Yes? I doubt that'll work, I just want to set that exact time point in hour. I want to count hourly bar from that point



https://docs.mql4.com/series/iBarShift

 

Thanks, I'll try 1st to set the time point then the bars.

 
TheEconomist:

TwoWeeksDatetime=TodayDatetime-2*7*24*3600 ; it's simple integer operation

Then display on chart using a VLINE object.

No, it dosen't work this way, one is string, the other is numeric, cannot put them in an operation,


Who has the solution? Help please!

 

No no no, you got it all wrong.  A datetime is integer. Even if you define it string-like, it's integer. 

For example, if you have this:

int init()

   {

     datetime test=D'2004.01.01 00:00';

     Print(test);

   }

Will display an integer value; because the compiler knows that the D'...' notation refers a datetime constant and not a string. If it would be string, it would be

string datetime="2004.01.01 00:00"; 

And this is a pure string. That's why strings are quote-separated and datetimes are apostrophe-separated.

If you want to pinpoint specific hours in your charts, that's a bit different from what you do. Paste this bunch of functions in your project and use them wisely:

 bool Between(double v,double a,double b)
    {
    double aa;
    double bb;
    bool res;
    aa=MathMin(a,b);
    bb=MathMax(a,b);
    if (v>=a&&v<=b)
       res=True;
    else 
       res=False;
    return(res);
    }
 

 //***************************************************************
 // Date & Time functions
 //***************************************************************    
 
 bool LeapYear(int year)
   {
   double d,d2;
   d=(year-1988)/4.0;
   d2=Round(d)*1.0;
   if (d==d2)
     return(true);
   else
     return(false);
   }

 int MaxDay(int m,int y)
   {
   switch(m)
     {
     case  1: return(31);
     case  2: 
       if (LeapYear(y)==True) 
         return(29);
       else
         return(28);
     case  3: return(31);
     case  4: return(30);
     case  5: return(31);
     case  6: return(30);
     case  7: return(31);
     case  8: return(31);
     case  9: return(30);
     case 10: return(31);
     case 11: return(30);
     case 12: return(31);    
     default: return(0);
     }
   }

 bool DateExists(int d,int m,int y)
   {
    if (Between(m,1,12)==false)
      return(false);
    if (Between(m,1,MaxDay(m,y))==false)
      return(false);
    else
      return(true);
   }
   
 int DayFrom1Jan1970(int d,int m,int y)
   {
    int counter=0;
    int cntyears=0;
    int cntmths=0;
    int j;
    if (y>=1970) //going up
      {
       cntyears=y-1970;
       if (cntyears>0)
         {
         for (j=0;j<cntyears;j++)
            {
             if (LeapYear(1970+j)==false)
               counter=counter+365;
             else
               counter=counter+366;
            }
         }       
       cntmths=m-1;
       if (cntmths>0)
         {
         for (j=0;j<cntmths;j++)
            counter=counter+MaxDay(j+1,y);
         }
       counter=counter+d-1;
      }
    else //if (y>=1970)  //going down
      {
       cntyears=1970-y;
       if (cntyears>0)
         {
         for (j=0;j<cntyears;j++)
            {
             if (LeapYear(1970-j)==false)
               counter=counter-365;
             else
               counter=counter-366;
            }
         }
       cntmths=12-m;
       if (cntmths>0)
         {
         for (j=0;j<cntmths;j++)
            counter=counter-MaxDay(12-j,y);
         }
       counter=counter - (MaxDay(m,y)-m+1);
      }
   return(counter);                                                   
   }
   
datetime TimeAtNoon(int d,int m,int y)
   {
    int p=DayFrom1Jan1970(d,m,y);
    return( 12*3600 + p*24*3600 );    
   }
       
int DateDiff(int d1,int m1,int y1,int d2,int m2,int y2)
   {
    int p1=DayFrom1Jan1970(d1,m1,y1);
    int p2=DayFrom1Jan1970(d2,m2,y2);
    return(p1-p2);
   }
   
datetime MoveDate(int d1,int m1,int y1,int daystomove,int &d2,int &m2, int &y2)
   {    
    datetime timeafter=TimeAtNoon(d1,m1,y1)+daystomove*24*3600;
    d2=TimeDay(timeafter);
    m2=TimeMonth(timeafter);
    y2=TimeYear(timeafter);
    return(timeafter);
   }   
 
TheEconomist:

No no no, you got it all wrong. A datetime is integer. Even if you define it string-like, it's integer.

For example, if you have this:

int init()

{

datetime test=D'2004.01.01 00:00';

Print(test);

}

Will display an integer value; because the compiler knows that the D'...' notation refers a datetime constant and not a string. If it would be string, it would be

string datetime="2004.01.01 00:00";

And this is a pure string. That's why strings are quote-separated and datetimes are apostrophe-separated.

If you want to pinpoint specific hours in your charts, that's a bit different from what you do. Paste this bunch of functions in your project and use them wisely:


That's great, thanks a lot, I'll try to understand and follow.

Reason: