Determining the Date (or Year and Month seperately) of a Bar in a Chart

 

Hi,

I'm sorry to be asking this but believe me I've looked everywhere before asking and cannot find the answer that I need.

Essentially I need to SOMEHOW be able to do this (my description is in laymans terms for clarity):

In an indicator I need to somehow accomplish the following:

IF (YEAR OF BAR) IS GREATER THAN OR EQUAL TO 2009 AND (MONTH OF BAR) IS GREATER THAN OR EQUAL TO 5

DO THE CALCULATIONS IN THE INDICATOR FROM THE POINT ONWARD AND RETURN THE VALUE

ELSE

DO NOT DO ANY OF THE CALCULATIONS IN THE INDICATOR AND LET A CERTAIN VARIABLE REMAIN EQUAL TO ZERO

In the above example it would be nice if the '2009' (YEAR OF BAR) and the '5' (MONTH OF BAR) could be inputs.

An alternative (possibly easier, even preferable, method) would be to do it like this:

IF (DATE OF BAR) IS GREATER THAN OR EQUAL TO A CERTAIN (INPUT) DATE

DO THE CALCULATIONS IN THE INDICATOR FROM THAT POINT ONWARD AND RETURN THE VALUE

ELSE

DO NOT DO ANY OF THE CALCULATIONS IN THE INDICATOR AND LET A CERTAIN VARIABLE REMAIN EQUAL TO ZERO

In the above example the (DATE OF BAR) could be input as, say, 2009.10.01

As I said: sorry to ask. All the Date and Time Functions that I've tried either return ZERO or the CURRENT YEAR or 1970!!! LOL!!!

It may be important to mention that the indicator itself is working just fine. I just want to stop it from starting it's calculations from the very first bar of the chart i.e. it must start doing it's calculations only from a specified date onward.

Regards,

Dale.

 


Hello,

Thanks for you prompt response but it's EXACTLY those functions that you have referred me to that I cannot get to work for me for some or the other reason. And believe me when I say I've tried most of them.

As I said: thanks for your prompt response but I'm really not one of those people that will ask BEFORE reading available documentation or doing a search for what I'm looking for.

The closest I've come to anything that even resembles what I'm trying to do is an article that somebody wrote about STOPPING an indicator working when a specified date has been reached (a sort of 'demo' version protection system) but that relies on the current SERVER time which is useless to me.

Regards,

Dale.

 
   int    counted_bars=IndicatorCounted();
   for(int i = Bars-counted_bars-1; i>0; i--)
   {
    if(TimeYear(Time[i])>=2009) if(TimeMonth(Time[i])>=5)
     {
     //DO THE CALCULATIONS IN THE INDICATOR FROM THE POINT ONWARD AND RETURN THE VALUE
     }
     else
     {
      //DO NOT DO ANY OF THE CALCULATIONS IN THE INDICATOR AND LET A CERTAIN VARIABLE REMAIN EQUAL TO ZERO
     }
    }
 

Hi,

I cannot thank you enough. It works perfectly (but then I guess you already knew that it would).

I SWEAR to you I must have tried every variation of those Date and Time Functions and either got NO results i.e. NO indicator display OR no difference to the indicator. As a matter of fact: this over a period of two weeks would you believe and today I just couldn't take it anymore so decided to register and ask an expert!!! LOL!!!

That being said: I'm almost SURE I tried your version but I (only NOW) have a sneaky suspicion I had it in the wrong 'loop' of the code at the time.

EASY when you know how??? LOL!!!

Thank you once again.

Regards,

Dale.

 
You are welcome.
 

Hello again.

I also wanted to let you know that you've taught me something else that I didn't know was possible in MQL i.e. having multiple if() functions on a single line relating to the code block that follows!!! You have NO idea how much easier it is going to be for me to maintain and tidy up my code!!! I've been using nested if() functions up until now!!! LOL!!! Once again: thank you!!!

(Believe it or not: I code in C# for a proprietary trading platform and I'm now having to 'duplicate' all of my systems and indicators for use with MetaTrader 4 and THAT is the problem so I'm relatively new at coding for MetaTrader 4. Hell: I was 'lucky' to get the systems and indicators to WORK for MetaTrader 4 LET ALONE anything else)!!! LOL!!!

It's quite an interesting problem (and not something that somebody would, under normal circumstances, think of) (and I'm just detailing it here because it may be of some benefit to someone in the future).

Let's say that you have an indicator that performs certain calculations for each bar on the chart and then SUMS the results of those calculations to give you an end, current, mathematical result. If your charting package can only 'hold' or display a certain number of bars e.g. 999 bars then the moment the FIRST (OLDEST) bars start 'dropping off' the beginning of the chart then your end, current, mathematical result will change every time a bar 'drops off' of the chart. Or in the case of MetaTrader 4: if the number of bars in the chart exceeds the maximum number of bars that you have allowed for in your default chart settings then the same thing will happen. So in other words: if you draw a horizontal line (for example) at a certain level TODAY and TONIGHT (NEW DAY) the oldest bar 'drops off' of the chart your horizontal line is no longer at the same (correct) level in relation to your indicator because your end, current, mathematical result will have now changed after being recalculated.

The above being said though: what I DID find strange with MetaTrader 4 (and what prompted me to ask for input today) was that EVEN ALTHOUGH the number of bars on the chart HAD NOT exceeded the maximum number of bars allowed for in your default chart settings the same thing was happening. This I don't quite understand. Your fix has solved the problem though. It's just interesting as to WHY it should have been necessary to worry about this with MetaTrader 4 (the proprietary platform to which I was referring above displays a maximum, 'not negotiable', number of bars which happens to be 999 bars, although the historical data is stored 'ad infinitum', so THAT I could understand and of course had compensated for this but why the same problem was evident with MetaTrader 4 I don't quite know or understand).

But thanks again. There's nothing worse than drawing a line at a certain level TODAY and waking up tomorrow morning and the line is nowhere NEAR where it was yesterday in relation to the indicator and you start wondering if you made a mistake YESTERDAY (today)!!! LOL!!!

Regards,

Dale.

 
//if(TimeYear(Time[i])>=2009) if(TimeMonth(Time[i])>=5)
if(Time[i]) >= D'01.05.2009')
 

dpaterso:But thanks again. There's nothing worse than drawing a line at a certain level TODAY and waking up tomorrow morning and the line is nowhere NEAR where it was yesterday in relation to the indicator and you start wondering if you made a mistake YESTERDAY (today)!!! LOL!!!\

Lines don't move by themselves. The indicator must be redrawing.
 

Hi,

Thanks for your input as well.

That's my point: the horizontal lines were remaining at the levels where I'd put them the day before but the indicator value ITSELF was changing on a daily basis at the close (actually I watched it changing on an hourly basis too as a test and that stopped after I FINALLY was able to 'filter' or 'specify' its calculation start position by date i.e. by year and ny month and THAT I don't understand. I mean: there's really only two ways for that to happen i.e. either the scenario I described above or historical prices are being 'changed' (and even the latter, if this WAS happening, would only affect things if you cleared your chart cache and reopened the chart).

I must say that your solution is also very elegant. My only question (although I guess I should experiment instead of asking): would the user have to input the date with the 'D' prefix or is there a way of specifying that you're ACTUALLY inputting a date???

Regards,

Dale.

 

Hi Dale, you could have the user enter it as a string in the properties box

extern string date = "yyyy.mm.dd hh:mi"; (string must be in this format for StrToTime() to work)

then in init() use StrToTime to convert that to a datetime format

or if you wanted the input to look more user friendly and pretty you could do something like

extern string date = "dd/mm/yyyy";

but then you would have to write a function to break down that string to get the date numbers out of it and reassemble it in the correct yyyy.mm.dd hh:mi format so StrToTime() can convert it to datetime

Reason: