what wrong with my While Loop - page 2

 
Demos:

hello,

 The problem bug lies in the Print function; i replaced print with Alert and it is working as expected. By the way, global variables are static by themselves

(in MQL that is) so there is no need to do

static datetime newbar ;

 

best regards 

I will Try

 Thank you 

 
Demos:
Well, i was hurry on that one; Alert looks like it is working, but it does not actually

Yes, I have try and still hang

//+------------------------------------------------------------------+
int start()
    {
     newbar = Time[0];
          
     while(newbar == Time[0])
          {
           Alert("Still Old candle");
           Sleep(1000);
          }
     Alert("Now new candle occurs");
     return(0);
    }
//+------------------------------------------------------------------+
 

basicly, I want to loop the program until new candle occurs (do not go to the bigining start program for every tick until the current candle finish)

Do you have suggestion beside "While(still old candle)"

 

Thank you 

 

 Demos:

Well, i was hurry on that one; Alert looks like it is working, but it does not actually

Yes, I have try and still hang

 

 

I looked at a little bit more, and here are some remarks. You can try the code

     while (newbar<150)
     {
         Alert("Still Old candle"); Sleep(1000); newbar++;
     }

     Alert("Now new candle occurs");

 If you right click and remove the script before it finishes the counting, we can see that the terminal disregards Sleep() and proceeds to complete the script as if Sleep was not there. So, in case we have while (newbar==Time[0] ) it goes in an infinite loop. Looks like a bug to me 

Also, you should add RefresRates() inside while, or else Time[0] is not updated (so above code is wrong as well :) )

 

 basicly, I want to loop the program until new candle occurs (do not go to the bigining start program for every tick until the current candle finish)

Do you have suggestion beside "While(still old candle)"

 

 

I can see your code is based presumably in the MQL book which is ok but somehow outdated. You can find more stuff in the reference manual and up to date of course.

Now, it looks like you are going to be happy with an indicator. In that case you can use OnTimer() with a period you want and put your code inside there instead of Start() ; you should not be worried if your program needs more time than the period you set, as MQL has a message queue

In any case, Start(), Init() are obsolete and you may use OnStart(), OnInit() etc. You can look at "MQL4 programs" in the reference manual, as a starting point for more info

 

Well, if you still want to use a script (presence of OnStart() implies a script), you have to enter the following line to protect your code

void OnStart() /* i replaced int Start() with void OnStart(), so I removed return 0; from the body */
{
    newbar = Time[0];
          
    while (newbar == Time[0])
    {
        Alert("Still Old candle"); Sleep(1000); RefreshRates();
           
        if (IsStopped() ) newbar=0; /* I added this line to handle the removal of the script */
    }
    
    Alert("Now new candle occurs");
}
 

best regards 

 
 
     while(newbar == Time[0])
          {
           Alert("Still Old candle");
           Sleep(1000);
          }
Predefined variables (Time[0]) will not change; you must RefreshRates after Sleep.
 

I have add RefreshRates(), but the problem still exist

what do i don't understand why there isn't any  "Still Old candle" printed in the expert tab, not even once

datetime newbar;
//+------------------------------------------------------------------+
int start()
    {
     newbar = Time[0];
  
     while(newbar == Time[0])
          {
           Print("Still Old candle");
           Sleep(1000);
           RefreshRates();             // add RefreshRates()
          }
     Print("Now new candle occurs-------------");
     return(0);
    }
//+------------------------------------------------------------------+

 thank you

 

I think the structure of my program is wrong, because even I use this still the problem occurs

int newbar;
//+------------------------------------------------------------------+
int start()
    {
     newbar = Bars;
     while(newbar == Bars)
          {
           Print("Still Old candle");
           Sleep(1000);
           RefreshRates();
          } 
     Print("Now new candle occurs");
     return(0);
    }
//+------------------------------------------------------------------+

 anyway thank you for to all of you for your effort to help me

 

 best regards 

 
didatsd:

I have add RefreshRates(), but the problem still exist

what do i don't understand why there isn't any  "Still Old candle" printed in the expert tab, not even once

 thank you

I put your code into a script and it worked as expected. Printed every second until a new bar.

Are you sure that you are looking in the Experts Tab and not the journal for the prints? 

 
GumRai:

I put your code into a script and it worked as expected. Printed every second until a new bar.

Are you sure that you are looking in the Experts Tab and not the journal for the prints? 

According to the code he issued it was used in an indicator.
Reason: