Finding maximum value in a loop

 

Hello,

I'm currently trying to write a script that prints the maximum price within the last 100 days.

I know that ArrayMaximum exists but I want to avoid using it for now.


There is some error in the code that I cannot find.

Currently I am only getting a " 'for' - semicolon expected " error on

  for (int x = 0; x <= 99; x++)

I am also not sure if

rates[x-1]

is a proper way to indicate the previous index value, if it is not I would appreciate guidance on the correct way to do it, however that is not what is preventing it from compiling.

If you could inform me of my mistakes I would appreciate it :)


void OnStart()
  {
//---
   MqlRates rates[];
   
   ArraySetAsSeries (rates, true);
   
   int PriceDataPoints = CopyRates (_Symbol, 0, 0, 99, rates);  
 
   
  double high = 0, low = 0
    
  for (int x = 0; x <= 99; x++)
      {if (x > 0 && rates[x].high > rates[x-1].high)
        
      {high = rates[x].high;
      }
       
      }
         
   //Print ((string) NormalizeDouble (high, _Digits));
   //Print ((string) NormalizeDouble (low, _Digits));
  }
//+------------------------------------------------------------------+
 
double high = 0, low = 0;
                        ^

You need a semicolon after the low declaration. You can double click the error messages and the editor will point the line with the error.

 

Oh I see. I'm an idiot.

Now it compiles, however it is not sending any information to the terminal.

The print/comment/alert functions do not work until I take out the rest of the code.

Is there something else I'm doing wrong?


void OnStart()
  {
//---
   MqlRates rates[];
   
   ArraySetAsSeries (rates, true);
   
   int PriceDataPoints = CopyRates (_Symbol, 0, 0, 100, rates);  
 
   
  double high = 0, low = 0;
    
  for (int x = 0; x <= 100; x++)
      {if (x > 0 && rates[x].high > rates[x-1].high)
        
      {high = rates[x].high;
      }
       
      }
         
   Alert ((string) NormalizeDouble (high, _Digits));
   Alert ("hi");
   
   //Print ((string) NormalizeDouble (low, _Digits));
  }
 
Colin Leamy:

Oh I see. I'm an idiot.

Now it compiles, however it is not sending any information to the terminal.

The print/comment/alert functions do not work until I take out the rest of the code.

Is there something else I'm doing wrong?


Use ArraxMax

 
void OnStart()
  {
//---
   MqlRates rates[100];

   ArraySetAsSeries(rates, true);

   int PriceDataPoints = CopyRates(_Symbol, 0, 0, 100, rates);


   double high = 0, low = 0;

   for(int x = 0; x < 100; x++)
     {

      if(high == 0 || rates[x].high > high )

        {
         high = rates[x].high;
        }

     }

   Alert((string) NormalizeDouble(high, _Digits));
   Alert("hi");

//Print ((string) NormalizeDouble (low, _Digits));
  }
 
iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, 100, 0));

combination of: https://docs.mql4.com/series/ihighest and https://docs.mql4.com/series/ihigh

iHighest - Timeseries and Indicators Access - MQL4 Reference
iHighest - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iHighest - Timeseries and Indicators Access - MQL4 Reference
 
Trinh Dat:

Interesting, yours prints properly but mine doesn't. Why is this?

 
Colin Leamy:

Interesting, yours prints properly but mine doesn't. Why is this?

cause it is not same.

 
Trinh Dat:

cause it is not same.

Mine should still print.....

Reason: