ArrayMaximum() has generic error ?

Back to topics list To post a new topic, please log in or register
avatar
4
hopedslee123 2016.02.08 04:33
 

Hello,

I wish to get Max and Min value in arrays.

But I failed to get maximum value using ArrayMaximum using below source code.

Does anybody help me?

 

int start()

{

    double b_oprice[3], s_oprice[3], Bprofit=0, Sprofit=0;

    int Bcount=0, Scount=0;

    int Distance = 10;

    ArrayInitialize(b_oprice,0);    

    ArrayInitialize(s_oprice,0);

    int minValueIdx = 0, maxValueIdx = 0;    

    b_oprice[0] = 111.1; // minimum

    b_oprice[1] = 111.3; 

    b_oprice[2] = 111.2;

    s_oprice[0] = 121.2;

    s_oprice[1] = 121.1;

    s_oprice[2] = 121.3; // maximum

  

    for(int i=0; i<3; i++) 

    {

        printf("b_oprice = [%d], %.3f", i, b_oprice[i]);

    }      

    minValueIdx=ArrayMaximum(b_oprice,WHOLE_ARRAY,0);

    Print("Max value = ",b_oprice[maxValueIdx]," at index=",maxValueIdx);

      

    for(int i=0;i<3; i++)

    {

        printf("s_oprice = [%d], %.3f", i, s_oprice[i]);

    }

    minValueIdx=ArrayMinimum(s_oprice,WHOLE_ARRAY,0);

    Print("Min value = ",s_oprice[minValueIdx]," at index=",minValueIdx);

    return(0);

}


Experts results are as below. 

Max value = 111.1 at index=0

b_oprice = [2], 111.200

b_oprice = [1], 111.300

b_oprice = [0], 111.100 <---- wrong


Min value = 121.1 at index=1

s_oprice = [2], 121.300

s_oprice = [1], 121.100 <---- correct

s_oprice = [0], 121.200


avatar
Moderator
3404
GumRai 2016.02.08 06:38 #
 
     

    minValueIdx=ArrayMaximum(b_oprice,WHOLE_ARRAY,0);

    Print("Max value = ",b_oprice[maxValueIdx]," at index=",maxValueIdx);
you don't assign a value other than 0 to maxValueIdx
avatar
4
hopedslee123 2016.02.08 09:01 #
 
GumRai:
you don't assign a value other than 0 to maxValueIdx

Ooops ! The value was incorrct. 

Thanks GumRai ! 

avatar
4
hopedslee123 2016.02.09 04:44 #
 
hopedslee123:

Ooops ! The value was incorrct. 

Thanks GumRai ! 

I correct the code as GumRai told.

But, the result is not as expected. It varies by ArrayInitializd(array, EMPTY_VALUE / 0

int start()
{

    double b_oprice[100], s_oprice[100];
    
    ArrayInitialize(b_oprice,0);    
    ArrayInitialize(s_oprice,0);
    
  
    int minIdx = 0, MaxIdx = 0, buycnt=0, sellcnt=0;

       
    b_oprice[0] = 111.1; 
    b_oprice[1] = 111.3; //maximum
    b_oprice[2] = 111.2;
        
    s_oprice[0] = 121.2;
    s_oprice[1] = 121.1; // miniimum
    s_oprice[2] = 121.3; 
   
    buycnt=3; sellcnt=3;
    
    for(int i=0; i<buycnt; i++) 
    {
        printf("b_oprice = [%d], %.3f", i, b_oprice[i]);
    }    
    
    MaxIdx=ArrayMaximum(b_oprice,WHOLE_ARRAY,0);
    Print("Max value = ",b_oprice[MaxIdx]," at index=",MaxIdx);
   
    for(int i=0;i<sellcnt; i++)
    {
        printf("s_oprice = [%d], %.3f", i, s_oprice[i]);
    }
    
    minIdx=ArrayMinimum(s_oprice,WHOLE_ARRAY,0);
    Print("Min value = ",s_oprice[minIdx]," at index=",minIdx);
   
        
   ExpertRemove();

   return(0);
}

 

when use EMPTY_VALUE 

    ArrayInitialize(b_oprice,EMPTY_VALUE);    

    ArrayInitialize(s_oprice,EMPTY_VALUE);


2016.02.09 12:34:35.480 Expert Array GBPJPY,M1: removed

2016.02.09 12:34:35.477 Array GBPJPY,M1: uninit reason 0

2016.02.09 12:34:35.477 Array GBPJPY,M1: ExpertRemove function called


2016.02.09 12:34:35.477 Array GBPJPY,M1: Min value = 121.1 at index=1

2016.02.09 12:34:35.477 Array GBPJPY,M1: s_oprice = [2], 121.300

2016.02.09 12:34:35.477 Array GBPJPY,M1: s_oprice = [1], 121.100

2016.02.09 12:34:35.477 Array GBPJPY,M1: s_oprice = [0], 121.200


2016.02.09 12:34:35.477 Array GBPJPY,M1: Max value = 2147483647.0 at index=3

2016.02.09 12:34:35.477 Array GBPJPY,M1: b_oprice = [2], 111.200

2016.02.09 12:34:35.477 Array GBPJPY,M1: b_oprice = [1], 111.300

2016.02.09 12:34:35.477 Array GBPJPY,M1: b_oprice = [0], 111.100

2016.02.09 12:34:35.341 Array GBPJPY,M1: initialized

2016.02.09 12:34:34.319 Expert Array GBPJPY,M1: loaded successfully


When use 0 value. 

    ArrayInitialize(b_oprice,0);    

    ArrayInitialize(s_oprice,0);


2016.02.09 12:36:18.990 Array GBPJPY,M1: Min value = 0.0 at index=3

2016.02.09 12:36:18.990 Array GBPJPY,M1: s_oprice = [2], 121.300

2016.02.09 12:36:18.990 Array GBPJPY,M1: s_oprice = [1], 121.100

2016.02.09 12:36:18.990 Array GBPJPY,M1: s_oprice = [0], 121.200


2016.02.09 12:36:18.990 Array GBPJPY,M1: Max value = 111.3 at index=1

2016.02.09 12:36:18.990 Array GBPJPY,M1: b_oprice = [2], 111.200

2016.02.09 12:36:18.990 Array GBPJPY,M1: b_oprice = [1], 111.300

2016.02.09 12:36:18.990 Array GBPJPY,M1: b_oprice = [0], 111.100

2016.02.09 12:36:18.083 Array GBPJPY,M1: initialized

2016.02.09 12:36:16.329 Expert Array GBPJPY,M1: loaded successfully



avatar
Moderator
3404
GumRai 2016.02.09 07:48 #
 

It does as I expect

EMPTY_VALUE is 2147483647.0 , so is larger than any of the 3 numbers

0 is smaller than any of the 3 numbers 

avatar
12985
WHRoeder 2016.02.09 15:02 #
 
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. GumRai is correct. The smallest of {121.200, 121.100, 121.300, 0, 0, ...} is zero. The smallest of {121.200, 121.100, 121.300, 2147483647, 2147483647, ...} is 121.100. Why would you expect anything else?
avatar
4
hopedslee123 2016.02.10 05:17 #
 
GumRai:

It does as I expect

EMPTY_VALUE is 2147483647.0 , so is larger than any of the 3 numbers

0 is smaller than any of the 3 numbers 

Dear GumRai,

I found the resolution.

Inspite of using WHOLE_ARRAY

MaxIdx=ArrayMaximum(b_oprice,WHOLE_ARRAY,0);

minIdx=ArrayMinimum(s_oprice,WHOLE_ARRAY,0);

use real array size  

MaxIdx=ArrayMaximum(b_oprice,buycnt,0);

minIdx=ArrayMinimum(s_oprice,sellcnt,0);

Then, it returned real max and min value as below.

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: Min value = 121.1 at index=1

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: s_oprice = [2], 121.300

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: s_oprice = [1], 121.100

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: s_oprice = [0], 121.200

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: Max value = 111.3 at index=1

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: b_oprice = [2], 111.200

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: b_oprice = [1], 111.300

2016.02.10 13:15:30.193 FunctionTest GBPJPY,M1: b_oprice = [0], 111.100


GumRai, Thank your reponse. 

Back to topics list  

To add comments, please log in or register