stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Need help Calculating total Bars between 2 Time points!
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4377
Page 1 of 1
Author:  denialyg [ Sat Aug 08, 2015 5:41 am ]
Post subject:  Need help Calculating total Bars between 2 Time points!

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!
Author:  SteveHopwood [ Sat Aug 08, 2015 2:06 pm ]
Post subject:  Need help Calculating total Bars between 2 Time points!

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:
Author:  denialyg [ Mon Aug 10, 2015 1:50 am ]
Post subject:  Need help Calculating total Bars between 2 Time points!

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!
All times are UTC Page 1 of 1