Bar position over multiple time frames

 

Hi guys, I've been searching the forum and trying to pull apart scripts on the web. Documentation has only gotten me so far. I'm trying to get the current and previous position of M15, then zooming out to see what bar it resides inside of H1 and H4.

Live trading I believe this will either be 0 or 1, but for back testing the result will vary. 

Could someone help me understand a couple of things:

//+----------------------------------------------------------------------------------------------------------------------------+
//| 1.  The following script does a close job, but it's a little janky. M1 and M5 don't seem to be correct 

//+----------------------------------------------------------------------------------------------------------------------------+  

datetime some_time=D'2017.02.17 16:00';     // Hardcoded datetime
datetime server_time=TimeCurrent();            // Server Time
datetime local_time=TimeLocal();                 // Local Machine Time
datetime NewTime = server_time - 960;        // Server Time Minus 16 Minutes

// The following seems to be nearly correct
  int      shift_m1=iBarShift(Symbol(),PERIOD_M1, NewTime);
  int      shift_m15=iBarShift(Symbol(),PERIOD_M15, NewTime);
  int      shift_h1=iBarShift(Symbol(),PERIOD_H1, NewTime);
  int      shift_h4=iBarShift(Symbol(),PERIOD_H4, NewTime);

// Output: 

AUDUSD,M15: H4 Current Candle Position = 0 Candle 1 = 1 Candle 2 = 2 @ 2017.02.23 10:34:23
AUDUSD,M15: H1 Current Candle Position = 0 Candle 1 = 1 Candle 2 = 2 @ 2017.02.23 10:34:23
AUDUSD,M15: M15 Current Candle Position = 1 Candle 1 = 2 Candle 2 = 3 @ 2017.02.23 10:34:23
AUDUSD,M15: M5 Current Candle Position = 2 Candle 1 = 3 Candle 2 = 4 @ 2017.02.23 10:34:23
AUDUSD,M15: M1 Current Candle Position = 10 Candle 1 = 11 Candle 2 = 12 @ 2017.02.23 10:34:23

//+---------------------------------------------------------------------------------------------------------------+
//| 2. Would it be better (possible) to try and build an array that blocked the day into brackets? 
//+---------------------------------------------------------------------------------------------------------------+ 

ie.

H4   x 6   = 24H
H1   x 24 = 24H
M15 x 96 = 24H

Then so how checking if the candle resided in the previous bracket. I'm just ad lib-ing here. It's just an idea and I haven't quite got the construct down, but it's the only other way I can think of testing it.

 Any suggestions would be appreciated.

 Thank you, 

 
aqz:

Hi guys, I've been searching the forum and trying to pull apart scripts on the web. Documentation has only gotten me so far. I'm trying to get the current and previous position of M15, then zooming out to see what bar it resides inside of H1 and H4.

Live trading I believe this will either be 0 or 1, but for back testing the result will vary. 

The results will be the same live or backtesting:

  • The current M15 bar will always be within the current H1 / H4 bars.
  • The previous M15 bar may be within the current or the previous H1 / H4 bar.

datetime some_time=D'2017.02.17 16:00';     // Hardcoded datetime
datetime server_time=TimeCurrent();            // Server Time
datetime local_time=TimeLocal();                 // Local Machine Time
datetime NewTime = server_time - 960;        // Server Time Minus 16 Minutes

// The following seems to be nearly correct
  int      shift_m1=iBarShift(Symbol(),PERIOD_M1, NewTime);
  int      shift_m15=iBarShift(Symbol(),PERIOD_M15, NewTime);
  int      shift_h1=iBarShift(Symbol(),PERIOD_H1, NewTime);
  int      shift_h4=iBarShift(Symbol(),PERIOD_H4, NewTime);

Subtracting 16 minutes from TimeCurrent() isn't a good option.

To get the current M15 bar's opening time, use:

datetime M15_time = iTime(_Symbol, PERIOD_M15, 0);

To get the previous M15 bar's opening time, use:

datetime M15_time = iTime(_Symbol, PERIOD_M15, 1);

-------

Please use the SRC button when you post your code - I've modified your post. 

 

Thanks so much for your help. That makes a lot more sense to set the time before trying to find the bar.

 Will be sure to use the SRC button next time, sorry, first post.

 Thanks again. 

 
aqz:

Thanks so much for your help. That makes a lot more sense to set the time before trying to find the bar.

 Will be sure to use the SRC button next time, sorry, first post.

 Thanks again.