Download MetaTrader 5
To add comments, please log in or register
Have you published a program in the Market? Place it in your blog as well - the widget will help!
lord_hiro
50
lord_hiro 2014.04.10 14:14 

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:


int counter=0, MaxCount = 10000; 

void start()
  {
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?

qjol
3243
qjol 2014.04.10 14:24  

maybe the counter gets another value somewhere else inside the EA try :

void start()
  {
  int counter=0, MaxCount = 10000;
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }
Ian Venner
2583
Ian Venner 2014.04.10 14:56  
lord_hiro:

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?

You declared the counter globaly that means it is static.

Your code in OnStart(), counter++ pushes the counter value all the way up to 10,000

So then because it is static, at the next tick your counter is at 10,000 to begin with.

qjol
3243
qjol 2014.04.10 15:00  
SDC:

You declared the counter globaly that means it is static.

Your code in OnStart(), counter++ pushes the counter value all the way up to 10,000

So then because it is static, at the next tick your counter is at 10,000 to begin with.


he wrote:

lord_hiro:

Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?


so, this is my answer


qjol:

maybe the counter gets another value somewhere else inside the EA try :

void start()
  {
  int counter=0, MaxCount = 10000;
  while( counter <= MaxCount )
   {Print("Counter ", counter);
   counter++;
   }
   return;
  }
Keith Watford
Moderator
9419
Keith Watford 2014.04.10 15:12  
lord_hiro:

Hello all!

I've an EA (never tested it on pre 600 builds but the code is 2 years old) that gets stuck during a WHILE on build 625.

The fact is that when the WHILE starts it seems to be unable to check the expressions: the result is that it never comes out . It is placed in the start function.

I've tried a very simple EA in order to understand what goes on:


Well, the first counter value usually starts from a random value above 9500 and not from 0.

Why is it? Any suggestion?



Most likely you are looking in the Experts tab, which cannot keep up with fast loops like this.

Open the actual log file.

Ian Venner
2583
Ian Venner 2014.04.10 15:26  

I can tell you one thing, that while loop would never execute a second time unless somewhere else in the code you call that counter variable again and change its value back to less than 10,001. You should be careful about declaring variables globably.

whroeder1
14199
whroeder1 2014.04.10 16:02  
lord_hiro: Well, the first counter value usually starts from a random value above 9500 and not from 0. Why is it? Any suggestion?
Prior to build 600 uninitialized variables were initialized to zero. Now they contain random values, unless you initialize them..
Ian Venner
2583
Ian Venner 2014.04.10 16:18  
He did initialize them, it must be something else changing the value of that counter variable...
JDeel
239
JDeel 2014.04.10 18:42  
As GumRai said, the log can't handle that speed, it skips most of the actual data and shows you only some fragments.
lord_hiro
50
lord_hiro 2014.04.11 08:59  

GumRai, you are right: the log reports all the outputs starting from 1.

The difference between putting the variable declaration in the global space instead of inside OnStart() is that in the first case the loop executes one time whilst in the second one it repeats indefinitely.

But... It's evident that I choose the wrong debugging example because the count cycle executes well.

It all started with the following EA.

Here is the code:

 extern int SwingBarCount = 100;
int start()



{

int SwingHighShift = 0;
string StringHighStatus = "False";
int SwingHigh = 0;


while (StringHighStatus == "False" || SwingHighShift <= SwingBarCount)
   {
   
   if(iFractals(NULL, 0, MODE_UPPER, SwingHighShift) == iHigh(NULL, 0, SwingHighShift) && iFractals(NULL, 0, MODE_UPPER, SwingHighShift) > Close[0])
      {
      StringHighStatus = "True";
      SwingHigh = SwingHighShift;
      ObjectDelete("SwingHigh");
      ObjectCreate("SwingHigh", OBJ_VLINE, 0, Time[SwingHigh], 0);
      ObjectSet("SwingHigh", OBJPROP_COLOR, Red);
      }
      else
      {
      SwingHighShift++;
      }

   }

}}

I've removed the other non relevant code for clarity and it executes till the WHILE because I put some breakpoints before.

It should start with the WHILE condition being true and cycling the IF ELSE until StringHighStatus becomes true or StringHighShift reaches SwingBarCount.

What I see instead is that it never ends because after the WHILE there is a series of COMMENT and PRINT commands that return no output.

Whilst StringHighStatus can remain false, the counter has to reach SwingBarCount.

I had to modify like this in order for it to work :

while (!EndCycle)
   {
   
   if(iFractals(NULL, 0, MODE_UPPER, SwingHighShift) == iHigh(NULL, 0, SwingHighShift) && iFractals(NULL, 0, MODE_UPPER, SwingHighShift) > Close[0])
      {
      StringHighStatus = "True";
      SwingHigh = SwingHighShift;
      ObjectDelete("SwingHigh");
      ObjectCreate("SwingHigh", OBJ_VLINE, 0, Time[SwingHigh], 0);
      ObjectSet("SwingHigh", OBJPROP_COLOR, Red);
      }
      else
      {
      SwingHighShift++;
      }
      if( StringHighStatus == "True" ) EndCycle = TRUE;
      if( SwingHighShift > SwingBarCount ) EndCycle = TRUE;
   }

I don't understant why it processes EndCycle and not the other condition.

Thnk you all for your replies!

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2014.04.11 09:57  
if( StringHighStatus == "True" ) EndCycle = TRUE;
else if( SwingHighShift > SwingBarCount ) EndCycle = TRUE;
try include else.
/ /1234
To add comments, please log in or register