Get Bar Shift From Entry Date/Time To Current Date/Time


I would like to get the number of candles from a specific Date/Time to now.

The idea is to get the lowest and highest price from candles since my Date/Time of entry.

When I try to subtract my Date/Time from now (TimeCurrent) only the time seems to work:

CandleRange  = (Now)2020.04.24 15:33.45 - (Entry Date/Time)2020.04.24 15:10.52 which returns 1970.01.01 00:22:53

As you can see, This gives me trading time of 22 minutes and 53 seconds (which is correct) and I would therefore Like to get the Highest and Lowest prices within 22 minutes but the candle shift returned is incorrect (10851), I am expecting a shift of 1 for a 15 minutes chart and after 30 minutes a shift of 2 etc.. So in other words if I trade for an hour, this should give me a shift of 4 and within those four candles, I need to get the Highest and Lowest candle prices.

My code below:

datetime Now = TimeCurrent();
if (EntryTime == 0) EntryTime = Now;
datetime CandleRange = Now - EntryTime;
int CandleShift = iBarShift(NULL, TimeFrame, CandleRange);
double CandleHighFromEntry = iHigh(NULL, TimeFrame, CandleShift);
double CandleLowFromEntry = iLow(NULL, TimeFrame, CandleShift);
William Roeder  
twostarii: datetime CandleRange = Now - EntryTime; int CandleShift = iBarShift(NULL, TimeFrame, CandleRange);
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. Perhaps you should read the manual. iBarShift give you the candle index of a datetime. Not elapsed seconds. Pass the correct value. The shift of now is always zero.

Code formatting noted.

I used the following logic which seems to be working as required:

//Outside of OnTick)
double CandleHighFromEntry = 0;
double CandleLowFromEntry = 1000000;

//Inside of OnTick)
if (PriceBid > CandleHighFromEntry) CandleHighFromEntry = NormalizeDouble(PriceBid,2);
if (PriceBid < CandleLowFromEntry) CandleLowFromEntry = NormalizeDouble(PriceBid,2);