Array Index works but Array Maximum Does Not

 

Hello Everyone,

Please I need help with a code function in my EA. The Array index works correctly but the Array Maximum gives error value.

      for(int x = PositionsTotal(); x>=0; x--)
        {
         ulong ticket = PositionGetTicket(x);
         if(PositionSelectByTicket(ticket))
           {
            ArrayResize(prices, PositionsTotal(),0);
            prices[x] =PositionGetDouble(POSITION_PRICE_OPEN);
           }

        }

      int max_value = ArrayMaximum(prices,WHOLE_ARRAY,0);
      double max_price =0;

      for(int x = PositionsTotal(); x>=0; x--)
        {
         ulong ticket = PositionGetTicket(x);
         if(max_value>= 0 && PositionSelectByTicket(ticket)==true) // == true
           {
            max_price = PositionGetDouble(POSITION_PRICE_OPEN);
           }
        }

      // Comment(prices[1]); GIVES ACCURATE RESULT.
      Comment(max_price); // GIVES INCORRECT RESULT OF ZERO (0).

Kindly assist to give necessary correction, thanks.

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
CodeFx: ArrayMaximum

Please read the documentation. The ArrayMaximum returns an index, not a value.

Return Value

The function returns an index of a found element taking into account the array serial. In case of failure it returns -1.

Documentation on MQL5: Array Functions / ArrayMaximum
Documentation on MQL5: Array Functions / ArrayMaximum
  • www.mql5.com
ArrayMaximum - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Hi
PositionsTotal() returns the number of open positions, but their index starts from 0
So if PositionsTotal() returns 3 that means there is 3 open positions and their index numbers are 0,1 and 2
So in for loop try this:

 for(int x = PositionsTotal()-1; x>=0; x--)
 
CodeFx: Please I need help with a code function in my EA. The Array index works correctly but the Array Maximum gives error value.

Besides the issue with ArrayMaximum, your code is also illogical.

If you have already collected all the open prices and know where the maximum value is at in the array, why are you scanning all the positions again.

 
And the Array Maximum returns the index of the max cell of the array
So it is needed to read that cell value to read the max price 

   int max_index = ArrayMaximum(prices,WHOLE_ARRAY,0);
      double max_price = prices[max_index];
 
Mohammadmahmood Pirayeh #:
   int max_index = ArrayMaximum(prices,WHOLE_ARRAY,0);       double max_price = prices[max_index];

Many thanks for your response. I will try it out.

 
Fernando Carreiro #:

Besides the issue with ArrayMaximum, your code is also illogical.

If you have already collected all the open prices and know where the maximum value is at in the array, why are you scanning all the positions again.

Thanks a lot, your response did help, I have figured out the position (index) of the maximum value. 

 
Mohammadmahmood Pirayeh #:
Hi
PositionsTotal() returns the number of open positions, but their index starts from 0
So if PositionsTotal() returns 3 that means there is 3 open positions and their index numbers are 0,1 and 2
So in for loop try this:

Thanks for spotting that error, what an oversight on my part.

Reason: