Value assigned to int variable incorrect

 

Hi, i'm new to mq5 (but i'm familiar with code, because i'm software engineer)

i encountered a problem as stated on the title, here's my code

      MqlRates priceInformation[];
      ArraySetAsSeries(priceInformation,true);
      int data = CopyRates(Symbol(),Period(),0,Bars(Symbol(),Period()),priceInformation);
      int ctr = 25;
      
      int topIndex;
      double topValue;
      
      int bottomIndex;
      double bottomValue;
      
      double valueDifference;
      if(data<=0){
         return;
      }
      for(int x=1; x<=ctr; x++){
         if(topValue == NULL || topValue < priceInformation[x].high){
            topValue = priceInformation[x].high;
            topIndex = x;
            if(x>100){
               Print("1 LOG " + x + " " + topIndex);
            }
            if(topIndex>100){
               Print("2 LOG " + x + " " + topIndex);
            }
         }
         if(bottomValue == NULL || bottomValue > priceInformation[x].low){
            bottomValue = priceInformation[x].low;
            bottomIndex = x;
            if(x>100){
               Print("3 LOG " + x + " " + bottomIndex);
            }
            if(bottomIndex>100){
               Print("4 LOG " + x + " " + bottomIndex);
            }
         }
         if(topIndex>100){
         Print("5 LOG " + topIndex);
         }
         if(bottomIndex>100){
            Print("6 LOG " + bottomIndex);
         }
      }
      
      if(topIndex>100){
         Print("7 LOG " + topIndex);
      }
      if(bottomIndex>100){
         Print("8 LOG " + bottomIndex);
      }

as you can seee the value topIndex and bottomIndex should never be above 25

but when i run the code, it sometimes print 5,6,7,8 LOG 

not sure why 1,2,3,4 not printed while 5, 6, 7, 8 got printed

the values varies, but it always above tens of millions

 
Alexander Chandra: as you can seee the value topIndex and bottomIndex should never be above 25
      double topValue;

A random value can always be above 25.

 
William Roeder:

A random value can always be above 25.

what do you mean? i dont put random value on topIndex & bottomIndex

and what you are highlighting is topValue which is not related to the question

 
Alexander Chandra: what do you mean? i dont put random value on topIndex & bottomIndex and what you are highlighting is topValue which is not related to the question

Be it "topValue" or "topIndex" or any of the others, you declared the variables but never assigned an initial value to them. Hence, they have random initial values.

When you compiled, did you not get the warning "possible use of uninitialized variable"?

Be it a warning or error, you should always fix them!

 
Fernando Carreiro:

Be it "topValue" or "topIndex" or any of the others, you declared the variables but never assigned an initial value to them. Hence, they have random initial values.

When you compiled, did you not get the warning "possible use of uninitialized variable"?

Be it a warning or error, you should always fix them!

yeah, i got the warning

i see, so there are no null value in MQ5? it will assign random value?

 
Alexander Chandra: as you can seee the value topIndex and bottomIndex should never be above 25 but when i run the code, it sometimes print 5,6,7,8 LOG  not sure why 1,2,3,4 not printed while 5, 6, 7, 8 got printed the values varies, but it always above tens of millions

I  have compiled your code as a Script and tested it against EURUSD H1, and I do not get any of the strange behavior you claim.

Maybe you can provide a print out of what you are getting on your end as well as the test conditions you are using!

Reason: