How to convert bars in the future to time ? - page 2

 

Hi,

i try this code below for testing to know the right future bar time over weekends, but  there is bugs in my code .  i have tried these some days to look for where the problem is, but still can not find the cause of error . Please help if you have time ...

Thank you very much in advance.

//+------------------------------------------------------------------+
//|                                                                  |
//| trying to check for future bar time that over weekends !!!       |
//+------------------------------------------------------------------+

#property strict

#property indicator_chart_window

//-----------------------------------------------------------------------
input    string         info                          = "better to test this on big TF (ex: H1,H4,D1)";
input    string         info2                         = "please see 'Experts' tab ...";
input    int            lookBackBars                  = 200;
input    int            futureBarsCount               = 40;
         datetime       curTime,futureTime,calcFutureTime;
         int            i,j,k,limit,cnt;
         int            ctf,curBar,futureBar;
         string         symb;
         string         sFutureTime,sCalcFutureTime;
         
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
   symb     = Symbol();
   ctf      = Period();

   //-----------------------------------
   if (lookBackBars>Bars) {
           Print(__FUNCTION__, "   ERROR : lookBackBars(",lookBackBars,") must be smaller than : ",Bars,".");
           Alert("ERROR : lookBackBars(",lookBackBars,") must be smaller than : ",Bars,".");
           return(INIT_FAILED);
        } // if

   //---
   return(INIT_SUCCEEDED);
}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) {
                
   limit    = (int) MathMin(lookBackBars,Bars);
   
   for (i=limit; i>0; i--) {             
      curTime           = iTime(symb,ctf,i);
      futureBar         = i-futureBarsCount;
   
      if (futureBar>=0) {  // still history bar, for testing only ...
         futureTime        = iTime(symb,ctf,futureBar);   
         calcFutureTime    = FuncValidateFutureTime(curTime,ctf,futureBarsCount);
         
         //--- show error
         if (futureTime != calcFutureTime) {
            sFutureTime       = TimeToStr(futureTime);
            sCalcFutureTime   = TimeToStr(calcFutureTime);
            
            Print("Error at pair: ",symb," TF:",FuncPeriodToStr(ctf),", futureBarsCount:",futureBarsCount," ,barNr:",i," ,time:",curTime," ,futureTime=",sFutureTime," ,calcFutureTime=",sCalcFutureTime);
         } // if
      } // if
   } // for
   //---
   return(rates_total);
}

//+--------------------------------------------------------------------------------------------------------+
datetime FuncValidateFutureTime(datetime theBeginTime,int theTF,int futureBarAmounts) {
   datetime retValue    = 0;
   double   weeks       = MathFloor((futureBarAmounts*theTF*60)/(5*24*3600));
   datetime targetTime  = theBeginTime+(futureBarAmounts*theTF*60);
   int      weekAmount  = (int) weeks;
   int      targetDay   = TimeDayOfWeek(targetTime);
    
   //---
   if (weekAmount == 0) {
      if (targetDay==0)   { // sunday
         retValue = targetTime + 24*60*60;
      } // if
      else if (targetDay==6) { // saturday
         retValue = targetTime + 2*24*60*60;
      } // else if
      else { // workdays
         retValue = targetTime;
      } // else   
   } // if \
   else { // over weekends
      if (targetDay==0)   { // sunday
         retValue = targetTime + (24*60*60) + (weekAmount*2*24*3600);
      } // if
      else if (targetDay==6) { // saturday
         retValue = targetTime + (2*24*60*60) + (weekAmount*2*24*3600);
      } // else if
      else { // workdays
         retValue = targetTime + (weekAmount*2*24*3600);
      } // else  
   } // else
   //---   
   Print("weeks=",weeks," ,weekAmounts = ",weekAmount," ,targetDay=",targetDay," ,retVal=",retValue);
   //---             
   return(retValue);            
}

//+-----------------------------------------------------------------------------------------------------+ 
string FuncPeriodToStr(int thePeriod) {  
   string   retVal="";
   switch(thePeriod)  {
      case 1: retVal = "M1";        break;
      case 5: retVal = "M5";        break;
      case 15: retVal = "M15";      break;
      case 30: retVal = "M30";      break;
      case 60: retVal = "H1";       break;
      case 240: retVal = "H4";      break;
      case 1440: retVal = "D1";     break;
      case 10080: retVal = "W1";    break;
      case 43200: retVal = "MN1";   break;
   }
   return(retVal);
}


//+------------------------------------------------------------------+
 
jackprobe: i try this code below for testing to know the right future bar time over weekends,
Can't generally predict future bar times: How to convert bars in the future to time ? - Trends - General - MQL5 programming forum