Need help Calculating total Bars between 2 Time points!

Post Reply
denialyg
Posts: 3
Joined: Thu Apr 12, 2012 1:44 pm

Need help Calculating total Bars between 2 Time points!

Post by denialyg »

Hi all,

Ive been stuck trying to figure out how to solve this. Could some kind soul help me out or point in me the right direction please.
Basically Ive got 2 time points, a starting and ending time. I want to get the total number of bars shown on chart between these 2 points. Ive tried using (EndTimeRange -StartTimeRange * 60 * Period() ) but this included weekends into the total number.

Is the below code correct or is that a much simplier way to handle this?

Code: Select all

 
string STR ="2015.08.06 15:30" ; //Starting time example
string ETR ="2015.08.07 15:30" ; //Ending time example
 
 datetime StartTimeRange = StrToTime(STR) ; 
 datetime EndTimeRange = StrToTime(ETR);
  
int XDBars1=iBarShift(Symbol(),0,StartTimeRange,true);
int XDBars2=iBarShift(Symbol(),0,EndTimeRange,true);
int XDBars= XDBars1-XDBars2;
Also how do I countback bars based on a time point? eg. Based on EndTimeRange, i want to count backwards 1000 bars and get the data from that bar?

Much appreciated!
User avatar
SteveHopwood
Owner
Posts: 9904
Joined: Tue Nov 15, 2011 8:43 am
Location: Misterton - an insignificant village in England. Very pleasant to live in.

Need help Calculating total Bars between 2 Time points!

Post by SteveHopwood »

Code: Select all

      datetime STR = D'2015.08.06 15:30'; 
      datetime ETR = D'2015.08.07 15:30'; 
        
      int XDBars1=iBarShift(Symbol(),0,STR,true);
      int XDBars2=iBarShift(Symbol(),0,ETR,true);
      int XDBars= XDBars1-XDBars2;
         
      Alert(XDBars);//Remove this when you are confident the code is correct
      
      //Find the shift 1000 bars ago
      int shift = XDBars2 + 1000;
      Alert(shift);//Remove this when you are confident the code is correct
Well done for trying to sort this stuff out for yourself. You fell foul of a particularly difficult problem. Empty4 lack-of-useful-help is as useful as a chocolate fire guard. I learned the solution by studying the code of other coders, so don't feel useless.

:xm:
Read the effing manual, ok?

Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.

I still suffer from OCCD. Good thing, really.

Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.

To see The Weekly Roundup of stuff you guys might have missed Click here

My special thanks to Thomas (tomele) for all the incredible work he does here.
denialyg
Posts: 3
Joined: Thu Apr 12, 2012 1:44 pm

Need help Calculating total Bars between 2 Time points!

Post by denialyg »

Thanks Steve,
Glad to know I was on the right track.
I've been learning through referencing other coders codes and lots of trial and error, was the easiest way to pick up and learn mql4 for a person with 0 background in programming.

Thanks again!
Post Reply

Return to “Coders Hangout”