How to count number of bars from a specified date?

To add comments, please log in or register
dark_side
22
dark_side  

Hi,

I want to count number of bars from a specified date to current time. For ex. on 2 Mar 2018 12:00PM I want to count how many bars would be there in a timeframe from 1 Mar 2018 00:00 AM. I want to know which function I should use and how? I think its 'Bars', but I don't know what values to put in or how to use it. Can someone kindly help me and tell me the solution? 

Thanks,

D_S_


Drazen Penic
2962
Drazen Penic  
dark_side:

Hi,

I want to count number of bars from a specified date to current time. For ex. on 2 Mar 2018 12:00PM I want to count how many bars would be there in a timeframe from 1 Mar 2018 00:00 AM. I want to know which function I should use and how? I think its 'Bars', but I don't know what values to put in or how to use it. Can someone kindly help me and tell me the solution? 

Thanks,

D_S_


Look at the iBarShift documentation.

Use the sample code from provided on that page.

iBarShift - Timeseries and Indicators Access - MQL4 Reference
iBarShift - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
Index of the bar which covers the specified time. If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on
dark_side
22
dark_side  
Drazen Penic:

Look at the iBarShift documentation.

Use the sample code from provided on that page.

Hi Drazen,

Thanks for reply. As I can understand iBarShift is used to find a bar at particular time. But, I didn't knew how to use it as I am not an expert programmer in MQL4.

I want to count number of bars from a specified date/time to current time. For ex. if specified time is 1 Mar 2018 07:00PM, then while calculating the indicator value on 2 Mar 02:12 AM the bar count should be taken 8 (in hourly chart), 9 at 03:01 AM, and so on.

So, according to you iBarShift will count number of bars from current time to a specified time and return a number, right?

Thanks very much for your help. I am really grateful.

William Roeder
20233
William Roeder  
dark_side: I think its 'Bars', but I don't know what values to put in or how to use it.
Perhaps you should read the manual. Don't think, know.
int Bars
Number of bars in the current chart.
          Bars - Predefined Variables - MQL4 Reference
It's an integer. There's nothing to put in.
Drazen Penic
2962
Drazen Penic  
dark_side:

Hi Drazen,

Thanks for reply. As I can understand iBarShift is used to find a bar at particular time. But, I didn't knew how to use it as I am not an expert programmer in MQL4.

I want to count number of bars from a specified date/time to current time. For ex. if specified time is 1 Mar 2018 07:00PM, then while calculating the indicator value on 2 Mar 02:12 AM the bar count should be taken 8 (in hourly chart), 9 at 03:01 AM, and so on.

So, according to you iBarShift will count number of bars from current time to a specified time and return a number, right?

Thanks very much for your help. I am really grateful.


It's not according to me, it's according to the documentation:

Returned value

Index of the bar which covers the specified time. If there is no bar for the specified time (history "gap"), the function will return -1 or the nearest bar index (depending on exact parameter).

Which part of the above sentence you don't understand? 


Also, did you run the sample from the article? It's only 3 lines of code.

If you can't figure out that code sample you should probably go to the freelance section and ask there for help.

Petr Nosek
3051
Petr Nosek  
dark_side:

Hi,

I want to count number of bars from a specified date to current time. For ex. on 2 Mar 2018 12:00PM I want to count how many bars would be there in a timeframe from 1 Mar 2018 00:00 AM. I want to know which function I should use and how? I think its 'Bars', but I don't know what values to put in or how to use it. Can someone kindly help me and tell me the solution? 

Thanks,

D_S_


I think the right function for you is Bars(). There are two overloadings for that function and the second for your task

int  Bars( 
   string           symbol_name,     // symbol name 
   ENUM_TIMEFRAMES  timeframe,       // period 
   datetime         start_time,      // start date and time 
   datetime         stop_time        // end date and time 
   );
https://docs.mql4.com/series/barsfunction
dark_side
22
dark_side  
Petr Nosek:

I think the right function for you is Bars(). There are two overloadings for that function and the second for your task

 https://www.mql5.com/en/docs/series/bars

Thanks Petr for answering.
William Roeder
20233
William Roeder  
Petr Nosek: I think the right function for you is Bars(). There are two overloadings for that function and the second for your task

Don't post MT5 documentation links in the MT4 forum. https://docs.mql4.com/series/barsfunction

Petr Nosek
3051
Petr Nosek  
whroeder1:

Don't post MT5 documentation links in the MT4 forum.

I didn't notice it was MQL4 forum. But the function is the same in MQL4 and MQL5. I'm so sorry. I promise I'll update my post. :D
Petr Nosek
3051
Petr Nosek  
whroeder1:

Don't post MT5 documentation links in the MT4 forum.

I've updated the link in my first post but unfortunately I can't delete the link that has been inserted automatically. Should I delete the whole post or you are already satisfied? :D
dark_side
22
dark_side  
Drazen Penic:


It's not according to me, it's according to the documentation:

Which part of the above sentence you don't understand? 


Also, did you run the sample from the article? It's only 3 lines of code.

If you can't figure out that code sample you should probably go to the freelance section and ask there for help.

Thanks again. Should have checked it as you said :)
To add comments, please log in or register