Trying to get the period separator on h4 chart but the code isnt working properly. (SOLVED)

 

i am trying to get the period separator on h4 , but there seems  to be something wrong with my logic , i keep getting Array out of range error.


This is   my code   :

#property strict
#property indicator_chart_window

int timefr ;  int bari=-1;


int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

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[])
  

{
             int curday=TimeDay(TimeCurrent());
 
                  if(curday>=15 && curday<=21)
                  {
                    for(int i=0;i<50;i++)
                     {
                       
                      if(TimeDay(Time[i]) <=21 && TimeDay(Time[i])>=15)
                         {
                          bari= bari+1;
                            Print(3);
                            Print(bari); 
                         }
                        else
                          {
                           break;
                          }
                     }
                  }
    ObjectCreate(0,"zxc",OBJ_VLINE,0,Time[bari],0);
    ObjectSetInteger(0,"zxc",OBJPROP_COLOR,clrRed);
    ObjectSetInteger(0,"zxc",OBJPROP_STYLE,STYLE_SOLID);
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Any suggestions , what i am doing wrong ?


   

 
Arad_1:

i am trying to get the period separator on h4 , but there seems  to be something wrong with my logic , i keep getting Array out of range error.


This is   my code   :

Any suggestions , what i am doing wrong ?

I have no idea what you are trying to achieve, but try

 if(bari>-1)
    {
    ObjectCreate(0,"zxc",OBJ_VLINE,0,Time[bari],0);
    ObjectSetInteger(0,"zxc",OBJPROP_COLOR,clrRed);
    ObjectSetInteger(0,"zxc",OBJPROP_STYLE,STYLE_SOLID);
    }
 
  1. Arad_1: i am trying to get the period separator on h4

    The separator on the H4 is the beginning of the week.

    int OnCalculate(…){
      if( !download_history(PERIOD_W1) ) return prev_calculated;
      datetime  BOW = iTime(_Symbol, PERIOD_W1, 0); // Beginning of the Week
      ObjectCreate(0,"zxc",OBJ_VLINE,0,BOW,0);
  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26.4 2019.05.20

  3. Why are you creating a VLine instead of just turning on the separator?

 
Keith Watford:

I have no idea what you are trying to achieve, but try

you were spot on .... it seems like the loop was running 4000 times instead of running only 50 times , because it wrapped into another for loop (my mistake) making the variable value more then there were candles on the chart 
thanks 

 
William Roeder:
  1. The separator on the H4 is the beginning of the week.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming  - Page 3 #26.4 2019.05.20

  3. Why are you creating a VLine instead of just turning on the separator?

Thanks William Roeder , This line of code saved me a lot of headache :  

datetime  BOW = iTime(_Symbol, PERIOD_W1, 0); // Beginning of the Week

i dont know what i was smoking that i didnt think of this , i was making it needlessly longer then it needed to be , i wrote 5 conditions to first calculate which week it is by getting the day() and then runnig one for loop per condition  making it needlessly long when all of that could be replaced by one line above 



as for " Why are you creating a VLine instead of just turning on the separator?" the vline is only for the visuals , and its just the tip of the iceberg , my main motive is to calculate keep indicator calculations and indicator's signals with in one period, so i am using this "bari" variable as the limit in another for loop where the indicator is calculating some conditions , i had got it to work on other timeframes , but weekly period for h4 was giving me a bit of a hard time since there isnt any function like TimeMonth(Time[i])     for the week



Reason: