Date error in MT4? - page 2

 
FMIC:

Since you are obviously afraid of showing your code, use my code example (see the script above) as the test code and then give use the results of that code.

Save it as "YearTest.mq4" and place it in the "Scripts" folder and run it against a EUR/USD H1 chart just as I have. It will then serve as the comparison between our two respective systems.

I've already done it, before you did. The script worked fine for me too.

Dear FMIC, please forget my question, I will do it myself later.

Thank you for your time! Cheers

 
ggekko:

Dear FMIC, I'm not a beginner, please consider this.

I am not trying to treat you as a beginner, but since you supplied no other information about yourself or the test environment, I could not possible know that, and that is why I had to ask many questions.

Please recreate the test conditions using my Test script and provide us with the results and log entries please!

 
ggekko:
I've already done it, before you did. The script worked fine for me too.

Then, why did you not tell us that? Why hide the information if you want use to help you?

OK! Then, my next point is a suspicion that I have about it being an Indicator!

Where is it being executed? Is it in the OnInit() or the OnCalculate() functions?

What is the earliest date of the chart data to which you are attaching the indicator code?

 
FMIC:

Then, why did you not tell us that? Why hide the information if you want use to help you?

OK! Then, my next point is a suspicion that I have about it being an Indicator!

Where is it being executed? Is it in the OnInit() or the OnCalculate() functions?

What is the earliest date of the chart data to which you are attaching the indicator code?

You missed my edited earlier post:

"Dear FMIC, please forget my question, I will do it myself later.

Thank you for your time! Cheers

"

Again, thank you! Your help intention is really appreciated!

 

@ggekko: You may no longer be online, but here is the information for anyone else who is interested!

I have recreated the problem. It seems that the Year() function (and possibly other time/date functions) when first used in the Indicator when first attached to a chart, it returns a value of 1900. Only with subsequent use or when you change the time frame, it then always returns the correct value.

In essence this most probably is a bug that needs to be reported to MetaQuotes. I leave that up to @ggekko to do!

This is the test code I used, and you will notice when first used that the Buffer Label and first data points are set to 1900, but when you change the chart time-frame (effectively recalculating) you then get the correct year:

#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

double         YearBuffer[];

int OnInit()
{
   SetIndexBuffer(0,YearBuffer);
   
   string strYear = StringConcatenate( "Year: ", Year());
   IndicatorShortName( strYear );
   SetIndexLabel( 0, strYear );
      
   return(INIT_SUCCEEDED);
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries( YearBuffer, false );
   
   int
      intYear = Year(),
      intLimit = ( prev_calculated == 0 ) ? 0 : prev_calculated - 1;
   
   for( int i = intLimit; i < rates_total; i++ )
      YearBuffer[ i ] = (double) intYear;

   return(rates_total);
}
Reason: