TimeDayOfWeek() problem?!

 

Hi everybody!

I wrote an simple script, that can not work.

It should find the first candle of  Wednesday from right to left on the used time frame etc: PERIOD_H1, but it can not work and i dont know why.

This code is so simply and am i so idiot i can not write it?? :)

Please somebody help!

 

Thank you very much! 

#property copyright "Copyright 2014, Kajos"
#property link      "http://www.eaeffect.com"
#property version   "1.00"
#property strict

string gsa_commentArr[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   for(int i=0;i<25;i++)
     {
      ResetLastError();
      comment("day[i]: "+(string)iTime(Symbol(),Period(),i)+
              " | day : "+(string)TimeDayOfWeek(iTime(Symbol(),Period(),i))+
              " error : "+(string)GetLastError());
      Sleep(300);
      if(TimeDayOfWeek(iTime(Symbol(),Period(),i)!=3))
        {
         continue;
        }
      else
        {
         comment("found date: "+(string)iTime(Symbol(),Period(),i));
         break;
        }
     }

  }
//+------------------------------------------------------------------+
void comment(string str)
  {
   const int cli_rowCnt=26;
   int li_aCnt=ArrayRange(gsa_commentArr,0);
   string ls_tmp="";
   if(li_aCnt<cli_rowCnt)
     {
      ArrayResize(gsa_commentArr,li_aCnt+1);
      gsa_commentArr[li_aCnt]=str;
      for(int i=0;i<li_aCnt+1;i++)
        {
         ls_tmp+=gsa_commentArr[i]+"\n";
        }
      Comment(ls_tmp);
      return;
     }
   else
     {
      for(int i=0;i<cli_rowCnt-1;i++)
        {
         gsa_commentArr[i]=gsa_commentArr[i+1];
         ls_tmp+=gsa_commentArr[i]+"\n";
        }
      gsa_commentArr[cli_rowCnt-1]=str;
      ls_tmp+=str+"\n";
      Comment(ls_tmp);
      return;
     }
   return;
  }
Files:
test.mq4  3 kb
 

Hello,

you check with your loop only the latest 25 Bars, if you try it on the H1-chart, then you check only the latest 25 hours.


for(int i=0;i<25;i++)


I hope it helps you?

 
user_123:

Hello,

you check with your loop only the latest 25 Bars, if you try it on the H1-chart, then you check only the latest 25 hours.



I hope it helps you?

Unfortunetly no! :( in my original code 25=Bars-1...

The loop is running and the comment writes the days 3 and in the if-else part the day!=3 is always true... ??? 

I think a got a bug :( 

 

Check this line:


if(TimeDayOfWeek(iTime(Symbol(),Period(),i))!=3)


in your code is the bracket not at the right position

 
user_123:

Check this line:



in your code is the bracket not at the right position

Me, noob :) i belive in mql4 lol, sorry!

Thank you very much!

 
Kajos2250: It should find the first candle of  Wednesday from right to left on the used time frame
if(TimeDayOfWeek(iTime(Symbol(),Period(),i)!=3)) ...
else {... break; }
  1. If you're looking at the current chart, current period, why use a function call? Just use
    if(TimeDayOfWeek(Time[i]!=3))
  2. Time[i]!=3 is always true and true==1 so you have
    if(TimeDayOfWeek(Time[i]!=3))
    if(TimeDayOfWeek(true))
    if(TimeDayOfWeek(1))        // Thu, 01 Jan 1970 00:00:01
    if(TimeDayOfWeek(D'1970.1.1 0:0:1))
    if(4)
    if(true)
  3. Even fixing it like user_123 said
    // if(TimeDayOfWeek(iTime(Symbol(),Period(),i) !=3)) ...
       if(TimeDayOfWeek(iTime(Symbol(),Period(),i))!=3 ) ...
       if(TimeDayOfWeek(Time[i]                   )!=3 )
    won't find the first (earliest) Wednesday candle, it finds the last (latest) either
    int i=0; while(TimeDayOfWeek(Time[i  ]) != 3) ++i; // Find latest Wednesday
             while(TimeDayOfWeek(Time[i+1]) == 3) ++i; // Find earliest Wednesday
    or
    int iDOW(int dow=3){
       for(int iD1=0; true; ++iD1){
          datetime D1time = iTime(Symbol(),PERIOD_D1);
          int      D1dow  = TimeDayOfWeek(D1time);
          if(D1dow == dow)
             return iBarShift(NULL,0, D1time);
    }  }
    :
    comment("found date: "+(string)Time[iDow(3)];

Reason: