Doing calculations with Buffer from "ExponentialMAOnBuffer"

 

Hey! Thanks for taking some time out of your day to read my question. 

So in my script I am calculating a few EMA's. With these EMA values I have to do some further calculations, but how would I do calculations with these values that are stored inside this array (buffer)? 

input int UMAPeriod  = 9;     // UMA period

double  UMAX_Buffer[],                   UMA_Buffer[];

   ArrayResize(UMA_Buffer,UMAPeriod);

   ArrayFill(UMA_Buffer,0,UMAPeriod,UMA);

   ExponentialMAOnBuffer(rates_total,prev_calculated,0,UMAPeriod,UMA_Buffer,UMAX_Buffer);  

These are the lines code for the UMA -> UMAX_Buffer. 

UMA is defined in a different script, but it does have variables. So the Array is Filled it just doesn't show that here.

But how would I do calculations with the latest value from this buffer? Could I do something like this?:

   ArrayResize(UMAX_Buffer,1);

But I think it still isn't possible to do something like this for example: 

double  Example         =       UMAX_Buffer + 10;

These might be dumb questions, but I can't figure them out. 

Thanks in advance! 

Have a great rest of your day

 
#include  <MovingAverages.mqh>
//--- input parameters
input int      UMAPeriod  = 9;     // UMA period
//--- indicator buffers
double         UMAX_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   SetIndexBuffer(0, UMAX_Buffer, INDICATOR_DATA);

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
        //---
        ExponentialMAOnBuffer(rates_total, prev_calculated, UMAPeriod, UMAPeriod, UMA_Buffer, UMAX_Buffer);  
        //--- return value of prev_calculated for next call
        return(rates_total);
}
//+------------------------------------------------------------------+

I don't know how to get data on UMA_Buffer. Add it by yourself.

 
Nagisa Unada #:

I don't know how to get data on UMA_Buffer. Add it by yourself.

Hey, I have only just seen your reply. 

Thanks allot! 

But the question I have is: 

The result of the “ExponentialMAOnBuffer” is placed in "

UMAX_Buffer

But how would I do calculations with the most recent value in this buffer. 

So for example: 

the most recent calculations of  ExponentialMAOnBuffer” is 20. 

How could I then do this,

A       =       20 /*UMAX_Buffer*/      +
10

Could I do this?:

input int UMAPeriod  = 9;     // UMA period

double  UMAX_Buffer[],                   UMA_Buffer[];



ExponentialMAOnBuffer(rates_total,prev_calculated,0,UMAPeriod,UMA_Buffer,UMAX_Bufer);  
A       =       UMAX_Buffer[UMAPeriod-1]       +10;

Thanks in advance,

 
It is very difficult to understand what information you are still trying to get since it seems you have understood how ExponentialMAOnBuffer works. There is a source array and a destination array.

Normally you process a buffer with a loop and that is also what ExponentialMAOnBuffer does if you study the function body. 

It takes the values from a source buffer (UMABuffer) one by one and does something with them, then puts them in the destination buffer.

Maybe it would clear up if you would say what particular operation do you intend to do after this?

Edit: but normally you can further use that buffer like every normal buffer if that helps
 
Tobias Johannes Zimmer #:
It is very difficult to understand what information you are still trying to get since it seems you have understood how ExponentialMAOnBuffer works. There is a source array and a destination array.

Normally you process a buffer with a loop and that is also what ExponentialMAOnBuffer does if you study the function body. 

It takes the values from a source buffer (UMABuffer) one by one and does something with them, then puts them in the destination buffer.

Maybe it would clear up if you would say what particular operation do you intend to do after this?

Edit: but normally you can further use that buffer like every normal buffer if that helps

Hey thanks for your reply! 

I kind of fell into coding not so long ago, so some parts I understand and some simple parts I still don't know allot about, buffers/ arrays for example. 

I think my main question would be: 

How can I do calculations with the newest value that is put into "UMAX_Buffer" in this case? 


The particular operation I want to do is this: 


input int RCRBopenP  = 100;
ExponentialMAOnBuffer(rates_total, prev_calculated, 0, RCRBopenP, _1Buffer, EMA_1); 

ExponentialMAOnBuffer(rates_total, prev_calculated, 0, RCRBopenP, _2Buffer, EMA_2); 
double
   dif_EMA        =  EMA_1[(RCRBopenP-1)]     - EMA_2[(RCRBopenP-1)]; double   RCwd           =  dif_EMA      + UMAX_Buffer[(UMAPeriod-1)];

And EMA_1 / EMA_2 are also buffer outcomes from  "ExponentialMAOnBuffer". 

But I don't know if my logic is adding up. 


But let's say the most recent calculations from EMA_1 are 10, EMA_2 are 5 and UMAX_Buffer is 5 

I want it to do:

double   dif_EMA        =  10     - 5;

double   RCwd           =  dif_EMA /*5*/     +5;

RCwd   -> 10

This is oversimplified, but I hope you get the idea. 

Thanks in advance!

 
ExponentialMAOnBuffer(rates_total, prev_calculated, RCRBopenP, RCRBopenP, _1Buffer, EMA_1); 

"Begin" is the bar number at which the calculation begins.

double   dif_EMA = EMA_1[rates_total - 1] - EMA_2[rates_total - 1];

The bar number of the latest value is "rates_total - 1"

 
Nagisa Unada #:

"Begin" is the bar number at which the calculation begins.

The bar number of the latest value is "rates_total - 1"

Thanks incredibly much, this was such a simple thing I was looking to get an answer to! 

Another question, why can't Begin be 0? Wouldn't this mean it would start immediately instead of at bar 100? Or is this done because period should be "= begin" to make sure there are enough bars to begin with? 


Again thanks allot, 

 
For example, if RCRBopenP=100, then 100 data are needed to calculate. There are no data prior when it calculates Bar0. So you should start the calculation from the 100th.
 
Alright, you can subtract two buffer values I think MACD is based on that any many more indicators. But they are automated with loops.

double c[];
int total=1000;
//Dynamic array c is Resized to total which the //number 1000 has been assigned to.
ArrayResize(c,total);

for(int i=0; i<total; i++)  //  (runs from 0 to 999)
{
//This loop adds two values which depend on the //running index i and stores them in c[] array.
c[i]=(i * 2) + (i * 3);
}

for(int i=0; i<total; i++)
{
//This loop prints the results of c[] behind a "Hello world" message
Print("Hello world #"+string(c[i]));
}

The same way as we added two numbers, we can subtract two buffer values (UMAXBuffer[i] and another one) which are depending on the running index i.
 
Tobias Johannes Zimmer #:
Alright, you can subtract two buffer values I think MACD is based on that any many more indicators. But they are automated with loops.

double c[];
int total=1000;
//Dynamic array c is Resized to total which the //number 1000 has been assigned to.
ArrayResize(c,total);

for(int i=0; i<total; i++)  //  (runs from 0 to 999)
{
//This loop adds two values which depend on the //running index i and stores them in c[] array.
c[i]=(i * 2) + (i * 3);
}

for(int i=0; i<total; i++)
{
//This loop prints the results of c[] behind a "Hello world" message
Print("Hello world #"+string(c[i]));
}

The same way as we added two numbers, we can subtract two buffer values (UMAXBuffer[i] and another one) which are depending on the running index i.

Thanks again for your response! 

Does the above mentioned response also work?:

[rates_total -1]


Also when looking at:

for(int i=0; i<total; i++)      //  (runs from 0 to 999)
{
c[i]=(i * 2) + (i * 3);         //This loop adds two values which depend on the running index i and stores them in c[] array.
}

Could you please elaborate on why exactly it adds two values? The for() is one of the operators I have been meaning to look into further.


I also have a completely different question I hope you can answer.. 

We talked about the "ExponentialMAOnBuffer" and the EMA values I am calculating to eventually calculate the values with which will be plotted. 

The problem I have now, is that "ExponentialMAOnBuffer" uses rates_total and prev_calculated. But I am calculating these values in an EA which means I don't have OnCalculate() and thus don't have

rates_total and prev_calculated.


I thought about using iCustom() and creating indicators for all the "ExponentialMAOnBuffer" above, but I don't actually have to plot these values I just need their EMA value. 

What can I do to get around this problem? I simply need to calculate their EMA's in the EA source code, but this could also be by including a file an calculating them here. 


Do you have any workarounds for my problem?

Again thanks allot for all of your help so far, I appreciate it a lot. 

 
Okay... normally you would have an Indicator do the indicator work which is exactly this kind of calculations. The EA usually only gets the values it needs and often these are signals like buy, sell or no action.

Rates_total is the name for the integer number(whole number) of all the values in a row. For the indicator it is the whole of all bars that exist in the current symbol and timeframe.

But if you play around with the ExponentialMAOn Buffer, it can calculate exponential moving average of any series of values. Like if you invent 100 values, you can say 100 is rates_total.
previously calculated is the integer number which shows you what has been calculated.

For instance you have a series of 100 values in a double array. So 100 is rates_total and prev_calculated is 0(because it is the first run and nothing has been calculated before). So the loop has to run from 0 to 99. 

Normally in OnCalculate in the first run, prev_calculated is 0 and rates_total is like 12000 or something (all bars available in this symbol and tf). Then the loop goes over all the values and the number of processed values is given back, which is now a hundred. Then prev_calculated equals rates_total.
Then becaus the most recent bar is not yet closed and still changes, prev_calculated is set to prev_calculated-1 so that the loop doesn't run a hundred times again, just once.

The two values in the code above are added together because I wanted to show you a harmless little example example. 🤓😎

Edit: The buffer fields will be accessed by UMAXBuffer[i] etc.
Reason: