One "physical" bar befor date time.

 
Hello again,
I need your help (always...)
I am trying to draw VLINEs one bar (what ever the time frame is switched) before a date time (in my case, begining of each week).

datetime t = iTime(NULL,PERIOD_W1,i);
      
      int Bars_Ahead =1;
      
      if (t>0)
       {
        ObjectCreate("Line "+DoubleToStr(i,0),OBJ_VLINE,0,t,0);
        //ObjectSet("Line "+DoubleToStr(i,0),OBJPROP_TIME,t);
        ObjectSet("Line "+DoubleToStr(i,0),OBJPROP_STYLE,STYLE_SOLID);
        ObjectSet("Line "+DoubleToStr(i,0),OBJPROP_COLOR,colir);
        ObjectSet("Line "+DoubleToStr(i,0),OBJPROP_WIDTH,1);
        ObjectSet("Line "+DoubleToStr(i,0),OBJPROP_BACK,true);
        
        ObjectCreate("Line2 "+DoubleToStr(i,0),OBJ_VLINE,0, t - (Bars_Ahead * Period()*60),0);

I thought i have found the answer by

t - (Bars_Ahead * Period()*60)

But it didn't work.

Can you help me please?

Regards.

 
i is nowhere to be found.
 
Marco vd Heijden:
i is nowhere to be found.

I don't want to calculate "i".
I want the 2nd VLINE place ONE BAR before the datetime "t".

If I am on D1 PERIOD, I want the 2nd VLINE one bar (one day) before,
If I am on H4 PERIOD, I want the 2nd VLINE one bar (4 hours) before,
If I am on H1 PERIOD, I want the 2nd VLINE one bar (1 hour) before   ... etc


 
Thierry Ramaniraka:
Hello again,
I need your help (always...)
I am trying to draw VLINEs one bar (what ever the time frame is switched) before a date time (in my case, begining of each week).

I thought i have found the answer by

But it didn't work.

Can you help me please?

Regards.

ObjectCreate("Line "+DoubleToStr(i,0),OBJ_VLINE,0,Time[iBarShift(NULL,0,t)+1],0);
 
Mladen Rakic:

Perfect (!).
Thank you very much.

EDIT :
note : in my case, it will be "Line2" instead "Line".
 
t - (Bars_Ahead * Period()*60)
This assumes every bar every exists. What if there are no ticks during a specific candle period? There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific.) requires knowledge of when your broker stops and starts (not necessary the same as the market.)
          "Free-of-Holes" Charts - MQL4 Articles
          No candle if open = close ? - MQL4 and MetaTrader 4 - MQL4 programming forum

Use iBarShift to find the bar that started at your W1 time, as Mladen Rakic posted in #3.
 
Mladen Rakic:

Hello again,
I am converting my indicator to the "new" standard mt4 way

//from
int start()

//to
int OnCalculate // and so on..

and

int Bars_Ahead=5;

doesn't work anymore.
It always give me a result like Bars_Ahead=1...So only one bar shifted.
Why ? It's the same code. What is wrong in this case ?

 
PS : Here is my code :
datetime Months_Start=iTime(NULL,PERIOD_MN1,im+1);
      
datetime Months_Start_BarShift=Time[iBarShift(NULL,0,Months_Start)+Bars_Ahead];
Reason: