"while", what's wrong in my code?

 

Hi all,

I'm working on this script: I manually draw a verticle line, then there'll be a Text showing the LOCAL time of that verticle line. I move the line, text will change.

I'm using a technique of "tick simulation loop" found in this forum.

But, after I move around the verticle line, I found "t" value is not changing accordingly. What's wrong?

my code:

extern int INTERVAL_MILLIS = 100;
extern int serverzone = 0;
extern int localzone = 8;


int start(){

int Diff = localzone - serverzone;
int cc,tdate,thourminute;
string vn,var1;
datetime t,tprev,tlocal;

while( !(IsTesting() || IsStopped()) ) {
         
   for (int j=0; j<ObjectsTotal(); j++)
     {
      if( ObjectType(ObjectName(j))==OBJ_VLINE ) 
        { 
          vn=ObjectName(j);
          cc ++;
         } 
      }      

   if(cc>1 || cc == 0)
    {
    Sleep(INTERVAL_MILLIS);
    continue; 
    }

   t=ObjectGet(vn,OBJPROP_TIME1);

   if(t != tprev) 
   {
       ObjectCreate("test09msg",OBJ_TEXT,0,t,WindowPriceMax());
       tlocal = t + Diff*3600;
       //var1=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

       tdate = TimeDay(tlocal) + TimeMonth(tlocal)*100 + TimeYear(tlocal)*10000; 

       thourminute = TimeMinute(tlocal) + TimeHour(tlocal)*100;
       
       ObjectSetText("test09msg", StringConcatenate("                                 ",tdate,"___",thourminute), 10, "Times New Roman", Gray);
       ObjectSet("test09msg",OBJPROP_TIME1,t);
       WindowRedraw();
       
       tprev = t;

   }
                   
                
                 
         
   Sleep(INTERVAL_MILLIS);
   RefreshRates();
}
   return(0);
}

Thanks.

 
joshatt:

Hi all,

I'm working on this script: I manually draw a verticle line, then there'll be a Text showing the LOCAL time of that verticle line. I move the line, text will change.

I'm using a technique of "tick simulation loop" found in this forum.

But why object (text content and position) does not redraw, after I move around the verticle line?

my code:

Thanks.

This code will cause an error . . .

ObjectCreate("test09msg",OBJ_TEXT,0,t,WindowPriceMax());

. . . how can you create an object that already exists ?

 
RaptorUK:

This code will cause an error . . .

. . . how can you create an object that already exists ?


Yeah, I know, but my point is: No matter error or not, I'll let it change text content and position.
 

I just tried to move this line outside "while...", turned out the same.

extern int INTERVAL_MILLIS = 100;
extern int serverzone = 0;
extern int localzone = 8;

int start(){

int Diff = localzone - serverzone;
int cc,tdate,thourminute,j;
string vn,var1;
datetime t,tprev,tlocal;

ObjectCreate("test09msg",OBJ_TEXT,0,t,WindowPriceMax());

while( !(IsTesting() || IsStopped()) ) {
         
   for ( j=0; j<ObjectsTotal(); j++)
     {
      if( ObjectType(ObjectName(j))==OBJ_VLINE ) 
        { 
          vn=ObjectName(j);
          cc ++;
         } 
      }      

   if(cc>1 || cc == 0)     // ----- one and only one verticle line ---------------------
    {
    Sleep(INTERVAL_MILLIS);
    continue; 
    }

   t=ObjectGet(vn,OBJPROP_TIME1);

   if(t != tprev) 
   {
      
       tlocal = t + Diff*3600;
       //var1=TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS);

       tdate = TimeDay(tlocal) + TimeMonth(tlocal)*100 + TimeYear(tlocal)*10000; 

       thourminute = TimeMinute(tlocal) + TimeHour(tlocal)*100;
       
       ObjectSetText("test09msg", StringConcatenate("                                 ",tdate,"___",thourminute), 10, "Times New Roman", Gray);
       ObjectSet("test09msg",OBJPROP_TIME1,t);
       WindowRedraw();
       
       tprev = t;
       Print(t);

   }
                   
                     
         
   Sleep(INTERVAL_MILLIS);
   RefreshRates();
}
   return(0);
}
 
joshatt:

Hi all,

I'm working on this script: I manually draw a verticle line, then there'll be a Text showing the LOCAL time of that verticle line. I move the line, text will change.

I'm using a technique of "tick simulation loop" found in this forum.

But why object (text content and position) does not redraw, after I move around the verticle line?

Get rid of the continue . . . unless it's there deliberately . . . and you understand what it is doing.

   if(cc>1 || cc == 0)
    {
    Sleep(INTERVAL_MILLIS);
    continue;                 // <-----
    }
 
joshatt:

Yeah, I know, but my point is: No matter error or not, I'll let it change text content and position.
If it's wrong it's wrong . . . why wouldn't you want to fix it ?
 

Thanks. I found the reason:

while( !(IsTesting() || IsStopped()) ) {
    
   cc = 0;      // <----------------------------I add this, to prevent counter CC counts endlessly.
   for ( j=0; j<ObjectsTotal(); j++)
     {
      if( ObjectType(ObjectName(j))==OBJ_VLINE ) 
        { 
          vn=ObjectName(j);
          cc ++;
         } 
      }      
......

If it's wrong it's wrong . . . why wouldn't you want to fix it ?

But: if wrong, do A, if right, do A, so I don't see the neccessity to code "do A" twice. Anyway, sometimes my brain is just empty. Please lighten me up.

Now there's another bug: I clear the verticle line and script from chart but leave the Text object there. Then run the script again, this time the Text just does not show up any more.

Guess I could delete this obect in the begining part of the script to fix. But still want to know why this bug occurs.

 
joshatt:

Thanks. I found the reason:

But: if wrong, do A, if right, do A, so I don't see the neccessity to code "do A" twice. Anyway, sometimes my brain is just empty. Please lighten me up.

Now there's another bug: I clear the verticle line and script from chart but leave the Text object there. Then run the script again, this time the Text just does not show up any more.

Guess I could delete this obect in the begining part of the script to fix. But still want to know why this bug occurs.

So the Object exists . . and you try and create it again ? didn't I already say this will give you an error ?
 
RaptorUK:
So the Object exists . . and you try and create it again ? didn't I already say this will give you an error ?

Try this . . .


Note the changed variable names to ones that tell you what they mean . . . a little commenting so the code is easier to follow and the different way of handling the ObjectCreate to avoid the error.

Files:
 
joshatt: But, after I move around the verticle line, I found "t" value is not changing accordingly. What's wrong?
while( !(IsTesting() || IsStopped()) ) {
   for (int j=0; j<ObjectsTotal(); j++){
      if( ObjectType(ObjectName(j))==OBJ_VLINE ){ ... }

   if(cc>1 || cc == 0){    When is this condition ever going to be false?
    Sleep(INTERVAL_MILLIS);
    continue; 
    }
   NEVER REACHED
 
RaptorUK:

Try this . . .


Note the changed variable names to ones that tell you what they mean . . . a little commenting so the code is easier to follow and the different way of handling the ObjectCreate to avoid the error.


It works perfect. I can learn a lot from your code. And I have to form a good process of debugging.... random try this and that won't help. Sometimes I have to calm down to find fault. LOL.

Thanks for all the help, I appreciate it.

Reason: