2 last date concentrate Ibarshift

 

this Is part of my code I want to change it and mt4 automatically calculate last 2 day:

extern datetime GOBACK = D'2015.08.30 00:00';
extern datetime GOBACKtill = D'2013.08.29 00:00';
int start()

{

   int countedBars = IndicatorCounted();
   if (countedBars < 0)
       countedBars = 0;
   CalculateIndicator(countedBars);   
   return(0);
}



void CalculateIndicator(int countedBars)
{

    
    int k=iBarShift(0,0,GOBACKtill);
    for(  int i=iBarShift(0,0,GOBACK);i <= k; i++)

whats the solution???

 
ahmadrezaahmad:

this Is part of my code I want to change it and mt4 automatically calculate last 2 day:

whats the solution???


Midnight - ( PERIOD_D1 * 60 )

and

Midnight - ( PERIOD_D1 * 2 * 60 )

 
RaptorUK:


Midnight - ( PERIOD_D1 * 60 )

and

Midnight - ( PERIOD_D1 * 2 * 60 )


midnight is variable not deifined for mt4 editor!!!!
 
ahmadrezaahmad:

midnight is variable not deifined for mt4 editor!!!!
Correct . . . define it yourself or find my definition on this forum . . .
 
ahmadrezaahmad:

this Is part of my code I want to change it and mt4 automatically calculate last 2 day:

whats the solution???

extern datetime GOBACK = D'2015.08.30 00:00';
extern datetime GOBACKtill = D'2013.08.29 00:00';
Did you really mean to use the year 2015, or is that just a typo?
 
ahmadrezaahmad: this Is part of my code I want to change it and mt4 automatically calculate last 2 day:
#define HR2400 86400       // 24 * 3600
int      TimeOfDay(datetime when){  return( when % HR2400          );         }
datetime DateOfDay(datetime when){  return( when - TimeOfDay(when) );         }
datetime Today(){                   return(DateOfDay( TimeCurrent() ));       }
datetime Tomorrow(){                return(Today() + HR2400);                 }
datetime Yesterday(){               return( iTime(NULL, PERIOD_D1, 1) );      }
 

Or

datetime xDayDate(int xDay) {
   // Negative xDay returns date for xDay in the past; Positive xDay returns date for xDay in the future
   datetime Today = TimeCurrent() - (TimeCurrent()%(PERIOD_D1 * 60));
   return (Today + (xDay * 86400));
}

int start() {
   Print ("Today's Date: ", TimeToStr(xDayDate(0)));                                // Output: Today's Date: 2013.09.02 00:00
   Print ("Yesterday's Date: ", TimeToStr(xDayDate(-1)));                           // Output: Yesterday's Date: 2013.09.01 00:00
   Print ("Tomorrow's Date: ", TimeToStr(xDayDate(1)));                             // Output: Tomorrow's Date: 2013.09.03 00:00
   Print ("Date for 100 days in the futrue from now: ", TimeToStr(xDayDate(100)));  // Output: Date for 100 days in the futrue from now: 2013.12.11 00:00
   Print ("Date for 3000 days ago from Today: ", TimeToStr(xDayDate(-3000)));       // Output: Date for 3000 days ago from Today: 2005.06.16 00:00
   return (0);
}
 
xDayDate returns the date of N days ago, not N trading days ago. If you want trading days, you need to use a D1 version (like my Yesterday() function)
 
WHRoeder:
xDayDate returns the date of N days ago, not N trading days ago. If you want trading days, you need to use a D1 version (like my Yesterday() function)

You are right. :) See expanded code below.

datetime xDayDate(int xDay, bool OnlyTradingDays = false) {
   // Negative xDay returns date for xDay in the past; Positive xDay returns date for xDay in the future (only when OnlyTradingDays = false)
   if (!OnlyTradingDays) {
      datetime Today = TimeCurrent() - (TimeCurrent()%(PERIOD_D1 * 60));
      return (Today + (xDay * 86400));
   }
   else if (xDay <= 0) {
      GetLastError();   // clear error flag
      for (int MaxIterations = 10; MaxIterations > 0; MaxIterations--) { 
         datetime xTradingDayDT = iTime(NULL, PERIOD_D1, MathAbs(xDay));
         if (GetLastError() == 4066 || xTradingDayDT <= 0)
            Sleep(2000); 
         else {
            if (iBarShift(NULL, PERIOD_D1, xTradingDayDT, true) == MathAbs(xDay))   // verify
               return (xTradingDayDT);
            else
               continue;
         }
      }
   }
   return (-1);
}

One potentially undesirable (or at least less desirable) aspect of the above code is my use of Sleep(). It may be more desirable to simply return -1 or 0 upon error and let the calling function decide how to respond.

 

tnx alot

finaly i establish this code:

        datetime Midnight = iTime(0, PERIOD_D1, 0);    
        datetime  a=Midnight+( PERIOD_D1 * 60 );
        
        datetime b=Midnight - ( PERIOD_D1 * 60 );
         if(DayOfWeek()==1 ) 
         {  b=Midnight - ( PERIOD_D1 *3* 60 );}
        
        int k=iBarShift(0,0,b);
        for(  int i=iBarShift(0,0,a);i <= k; i++)

in this case indicator check only today and yesterday bar!so draw only last 2 day line!

if I want to tell mt check all bar off chart with 2day bar before that bar in order to draw all chart line of indicator,what should I do?

Reason: