How to calculate maximum value of 10 or more variables

To add comments, please log in or register
Bhanu Sai Kishore Puvvala
169
Bhanu Sai Kishore Puvvala  

Hello,

I am saving values of a indicator in variable 10 times or more based on shift. Now I want to calculate the maximum and minimum value of those values. Whats the best way?

MathMax(v3,MathMax(v1,v2))..........?

Using arrays? I create array[] and resize and insert value from indicator? This option leaves only 1 time use of array and does not rewrite value into array.

Please help.

Konstantin Nikitin
12328
Konstantin Nikitin  
Array Functions

ArrayMaximum

Search for an element with the maximal value

ArrayMinimum

Search for an element with the minimal value

Work with arrays. It will be easier.
amrali
1502
amrali  

Try CArrayDouble 

#include <Arrays/ArrayDouble.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   CArrayDouble arr;

   arr.Add(1.0);
   arr.Add(4.0);
   arr.Add(3.0);
   arr.Add(5.0);
   arr.Add(2.0);

   double max_value=arr.At(arr.Maximum(0,WHOLE_ARRAY));
   Alert("max=",max_value);
  }
Bhanu Sai Kishore Puvvala
169
Bhanu Sai Kishore Puvvala  
Konstantin Nikitin:
Array Functions

ArrayMaximum

Search for an element with the maximal value

ArrayMinimum

Search for an element with the minimal value

Work with arrays. It will be easier.

I followed the same thing, but once the array values are saved in array, it is not being over written in the next candle. how to calculate ArrayMaximum for new candle in EA? Resize array to 0 and again back to 10?

Bhanu Sai Kishore Puvvala
169
Bhanu Sai Kishore Puvvala  
amrali:

Try CArrayDouble 

I never try this, will read and try it. will this Carraydouble overwrite values into array whenever the EA reads values from indicator?

why did you give arr.Add(1.0);

I need a one dimension array, so can I give arr.Add(1)=SI(0,1) and it will be overwritten whenever the EA runs? SI value is taken from indicator every candle as below.

SI(int buff,int shift){ return(iCustom(NULL,PERIOD_H4,"tales",buff,shift));}

lippmaje
749
lippmaje  
Bhanu Sai Kishore Puvvala:

I followed the same thing, but once the array values are saved in array, it is not being over written in the next candle. how to calculate ArrayMaximum for new candle in EA? Resize array to 0 and again back to 10?

You can use a rotating index.

static int idx=0;

arr[idx]=current_value;
idx=(idx+1) % 10;

The index will increase by 1 for each iteration and reset to 0 if it reaches 10. So that new values always overwrite the eldest entry.

Konstantin Nikitin
12328
Konstantin Nikitin  
Bhanu Sai Kishore Puvvala:

I followed the same thing, but once the array values are saved in array, it is not being over written in the next candle. how to calculate ArrayMaximum for new candle in EA? Resize array to 0 and again back to 10?

ZeroMemory  ArrayResize

Read Array Functions

Bhanu Sai Kishore Puvvala
169
Bhanu Sai Kishore Puvvala  
Konstantin Nikitin:

ZeroMemory  ArrayResize

Read Array Functions

Can you give example of a code to use zeromemory and ArrayResize for 10 indexes to reset and write values to it on each cycle/candle?

Konstantin Nikitin
12328
Konstantin Nikitin  
Bhanu Sai Kishore Puvvala:

Can you give example of a code to use zeromemory and ArrayResize for 10 indexes to reset and write values to it on each cycle/candle?

Something like this...

double test[], test_max, test_min;

bool Test(void)
{
     ZeroMemory(test);
     for(int i=0; i<10; i++)
     {
          if( ArrayResize(test, i+1) != i+1 )
               return false;
          test[i] = (iOpen(_Symbol, PERIOD_CURRENT, i) + iClose(_Symbol, PERIOD_CURRENT, i))/2;
     }
     test_max = test[ArrayMaximum(test,0)];
     test_min = test[ArrayMinimum(test,0)];
     
     return true;
}
To add comments, please log in or register