Custom indicator - compute average value

To add comments, please log in or register
caesarSK
29
caesarSK  

Hi all,

I try to create my own indicator. Part of my indicator has to calculate the average value (example for last 5 bars) from CCI.


int start() {

   int counted_bars = IndicatorCounted();

   if(counted_bars > 0) counted_bars--;

   int limit = Bars - counted_bars;

   for(int i = limit - 1; i >= 0; i--) { 

      CCI_buffer[i] = iCCI(NULL,0,CCI_period,PRICE_WEIGHTED,i);

   }

   return(0);

}


This code gives me the CCI. This is ok. But how can I get the indicator to return the average value for example to the last three columns? 

A practical example of what I want to achieve (last 3 bars):

CCI_buffer[0] = 100

CCI_buffer[1] = 50

CCI_buffer[2] = 50


CCI_average[0] = (CCI_buffer[0] + CCI_buffer[1] + CCI_buffer[2]) / 3 = 50 <- this is my value

How can I do that? In this case, my logic fails (I'm probably a fool) and I need to push forward.

Should I use the "FOR" function twice?

for{

   for{

   }

}


Or do I have the "FOR" function inside the formula to calculate?

for{

   CCI_average[i] = ....

}

Musngi
480
Musngi  
caesarSK:

Hi all,

I try to create my own indicator. Part of my indicator has to calculate the average value (example for last 5 bars) from CCI.


int start() {

   int counted_bars = IndicatorCounted();

   if(counted_bars > 0) counted_bars--;

   int limit = Bars - counted_bars;

   for(int i = limit - 1; i >= 0; i--) { 

      CCI_buffer[i] = iCCI(NULL,0,CCI_period,PRICE_WEIGHTED,i);

   }

   return(0);

}


This code gives me the CCI. This is ok. But how can I get the indicator to return the average value for example to the last three columns? 

A practical example of what I want to achieve (last 3 bars):

CCI_buffer[0] = 100

CCI_buffer[1] = 50

CCI_buffer[2] = 50


CCI_average[0] = (CCI_buffer[0] + CCI_buffer[1] + CCI_buffer[2]) / 3 = 50 <- this is my value

How can I do that? In this case, my logic fails (I'm probably a fool) and I need to push forward.

Should I use the "FOR" function twice?

for{

   for{

   }

}


Or do I have the "FOR" function inside the formula to calculate?

for{

   CCI_average[i] = ....

}

Use IMaOnArray() function
caesarSK
29
caesarSK  
Musngi:
Use IMaOnArray() function
I do not understand. IMaOnArray() function use moving avarege, but I need use CCI. Can you tell me a little more about this?
Musngi
480
Musngi  
caesarSK:
I do not understand. IMaOnArray() function use moving avarege, but I need use CCI. Can you tell me a little more about this?

You said: "But how can I get the indicator to return the average value for example to the last three columns?"

https://docs.mql4.com/indicators/imaonarray

iMAOnArray

Calculates the Moving Average indicator on data, stored in array, and returns its value.

Note

Unlike iMA(...), the iMAOnArray() function does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries() function.

double  iMAOnArray(
   double       array[],          // array with data
   int          total,            // number of elements
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // MA averaging method
   int          shift             // shift
   );


int start() {

   int counted_bars = IndicatorCounted();

   if(counted_bars > 0) counted_bars--;

   int limit = Bars - counted_bars;

   for(int i = limit - 1; i >= 0; i--) { 

      CCI_buffer[i] = iCCI(NULL,0,CCI_period,PRICE_WEIGHTED,i);

   }
   
   for(int x = limit - 1; x >= 0; x--) { 

      CCI_Average[x] = iMAOnArray(CCI_buffer, 0, inp_period, 0, MODE_SMA, x-4); 
      // inp_period is user defined variable, and MODE_SMA is Average value or Arithmetic Mean, and "x-4" is last 5 bars


   }
   

   return(0);

}

The above code is not tested. I'm not sure if this code correct.
To add comments, please log in or register