Draw a rectangle to the right of the most recent bar...

 
Hi, can anyone tell me how to draw a rectangle to the right of the most recent bar?

Ideally, I want something like the code below, where -5 and -25 refer to coordinates of time just ahead of the current bar.

ObjectCreate("SR"+i,OBJ_RECTANGLE,0,Time[-5],price1,Time[-25],price2);



Thanks for your help.

 
Calculate the time of bar -5 and -25 and use that in the ObjectCreate().

I don't know why this is not built-in to MT4 as Time[-1] and so on
 
Wow that was so easy.

I like answers like that.

Thanks.
 

Anyone looking at this answer 15 years later, may be a little mystified as to why you cannot do

...
datetime time2=iTime(symbol, timeframe, -1)
ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2)

However this does not work and, rather than throwing a compile time error, simply draws the object back to 01/01/1970 00:00:00... useful!

so instead we calculate the time using the required number of extended bars and multiplying it by the number of seconds per bar, then adding that to the current time:

...

int extendbars = 4;//number of bars to extend right
time2 = TimeCurrent()+(PeriodSeconds(_Period)*extendBars)
ObjectCreate(chart_ID,name,OBJ_RECTANGLE,sub_window,time1,price1,time2,price2)

Hope this saves someone some time

 
Andrew Thompson #: Anyone looking at this answer 15 years later, may be a little mystified as to why you cannot do
Because it can not be done. You must assume every bar every exists — they don't. 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 (2006)
          No candle if open = close ? - MQL4 programming forum (2010)
 
William Roeder #:
Because it can not be done. You must assume every bar every exists — they don't. 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 (2006)
          No candle if open = close ? - MQL4 programming forum (2010)

Yes but my work round works... calculating using barlength in seconds