Find average value over number of bars? - page 5

 
Dominik Christian Egert #:

Ahm... No.

Apples and Peaches... Haha

OK, let me try to explain the issue.

Lets say you have an array, close prices, given to you by a function, like OnCalculate(). 

Lets say you want to have an object that calculates the mean over the last 15 values. When instantiating this object, you cannot give reference to the array and make it store this reference. Because you cannot create reference-variables, except as paramter in functions, which loose focus on return.

This means, you cannot have a memory address pointer to point to an external array within the given object, and you are forced to pass the array every time to the object.

Therefore you cannot write such code as this:


Thats why it is neccessary to keep track of data internally, within the object itself.  

Yes I see - my solution with this is simply to have a receiver array embedded in the object and use ArrayCopy() to select the values you need.

You then use the embedded array copy to process the data in the way you want - not difficult to do and quite common really

Alternatively pass the source array into a method of the object - it is call by ref anyway so you will be transacting directly on the source array

 
R4tna C #:

If you need a code example of what I am suggesting... 

Output sample (random data):

2022.08.27 18:21:06.408 //--- Sum = 184190.00 Count=9.00 Mean = 20465.56 Max = 32233.00 Min = 1851.00

2022.08.27 18:21:06.408 Check ArrayMax[2]  true  = 32233.00

2022.08.27 18:21:06.408 Check ArrayMin[3]  true  = 1851.00

2022.08.27 18:21:06.408 29554.00000 17037.00000 32233.00000  1851.00000 22728.00000 31879.00000 25929.00000  6003.00000 16976.00000


And please note - this is just an example, not a finished version. But using this approach one could build a function which accepts a stream or set of numbers - the sum, count, mean, max, min could be retained so each time the same number of operations are executed.


each time

I am tired of explaining to you what's not possible, and you keeping claim it were possible.

Keep your state of mind and be happy with your opinion.

It's simply wrong. No matter how you twist it, it does not work.

Reason for this is, it's mathematically, or better said chronologically impossible.


 
R4tna C #:

Yes I see - my solution with this is simply to have a receiver array embedded in the object and use ArrayCopy() to select the values you need.

You then use the embedded array copy to process the data in the way you want - not difficult to do and quite common really

This approach is more expensive than having a fixed allocated buffer and use it as ring buffer.

Doesn't make sense.


 
Dominik Christian Egert #:
each time

I am tired of explaining to you what's not possible, and you keeping claim it were possible.

Keep your state of mind and be happy with your opinion.

It's simply wrong. No matter how you twist it, it does not work.

Reason for this is, it's mathematically, or better said chronologically impossible.


You are not the only who is tired... I gave you an explanation, it is pretty simple stuff but if you don't get it, so be it.

 
Dominik Christian Egert #:
This approach is more expensive than having a fixed allocated buffer and use it as ring buffer.

Doesn't make sense.


Alternatively pass the source array into a method of the object - it is call by ref anyway so you will be transacting directly on the source array


Anyway, if you say its not possible so be it, I'll keep finding ways to do the needful...

 
R4tna C #:

You are not the only who is tired... I gave you an explanation, it is pretty simple stuff but if you don't get it, so be it.

Your answer is wrong. And anyways, your whole approach to this matter is simply wrong as well.

My code posted on this thread is the technically most efficient method of calculating the mean value of a set of values taken from a Datastream.

It is also the most efficient way to get the max and min values from that subset of data.

There is no other way to do it more efficient in MQL4 or 5.

Simply accept it and stop arguing with false code samples and the claim you'd knew better. You don't.

So in some future, when you gain access to some quantum computer, you might (although I highly doubt that) figure out a method on how you can solve this more efficient.


 

People! You are both going off on a "rabbit hole" tangent and loosing sight of the original question and the points the OP refers to. I have outlined them here:

  • "I'm trying to write a custom indicator ..."
  • "... finding the average value over a past number of bars ..."
  • "... I don't see an "average" function prebuilt into mql4 ..."
  • "... don't think I would need to save each value into it's own variable and divide by the number of variables manually ..."

The answer to all these points are just one (or two) ... a simple built-in function, as the OP requested ... "iMA()" or "iMAOnArray()".

Forum on trading, automated trading systems and testing trading strategies

Find average value over number of bars?

Fernando Carreiro, 2022.08.06 03:06

It's called a Simple Moving Average and is the most basic of indicators build into every platform I know (and that includes MetaTrader).

The MQL4 function for it (and several other types of moving averages) is "iMA()". If you need to apply it to custom data, then use "iMAOnArray()".

 
Fernando Carreiro #:

People! You are both going off on a "rabbit hole" tangent and loosing sight of the original question and the points the OP refers to. I have outlined them here:

  • "I'm trying to write a custom indicator ..."
  • "... finding the average value over a past number of bars ..."
  • "... I don't see an "average" function prebuilt into mql4 ..."
  • "... don't think I would need to save each value into it's own variable and divide by the number of variables manually ..."

The answer to all these points are just one (or two) ... a simple built-in function, as the OP requested ... "iMA()" or "iMAOnArray()".

Amen to that...

 
Dominik Christian Egert #:
Your answer is wrong. And anyways, your whole approach to this matter is simply wrong as well.

My code posted on this thread is the technically most efficient method of calculating the mean value of a set of values taken from a Datastream.

It is also the most efficient way to get the max and min values from that subset of data.

There is no other way to do it more efficient in MQL4 or 5.

Simply accept it and stop arguing with false code samples and the claim you'd knew better. You don't.

So in some future, when you gain access to some quantum computer, you might (although I highly doubt that) figure out a method on how you can solve this more efficient.


I don't know better - I just find it puzzling why someone would write so much code for such simple tasks. 

This is easy stuff, but as Fernando has pointed out, time to terminate this discussion

 
Fernando Carreiro #:

People! You are both going off on a "rabbit hole" tangent and loosing sight of the original question and the points the OP refers to. I have outlined them here:

  • "I'm trying to write a custom indicator ..."
  • "... finding the average value over a past number of bars ..."
  • "... I don't see an "average" function prebuilt into mql4 ..."
  • "... don't think I would need to save each value into it's own variable and divide by the number of variables manually ..."

The answer to all these points are just one (or two) ... a simple built-in function, as the OP requested ... "iMA()" or "iMAOnArray()".

Yes. Thannk you for getting this back on track.

In fact, my original post was to give a code example on what I have described as method to calculate the mean of a series. As by the given answeres, it seemed to me, it was not obvious to everyone what exactly I meant. 

Maybe I should not have asked for feedback on it, but simply post the code and close the issue (for me). 

I was motivated to write that code because it was asked in another thread as well, and given the fact, an example was asked for "find the average value of the Close price minus the Open prices over the past x number of bars", which is not easily possible with the built-in functions, I decided to give an example code to solve this request.

Also WIlliams code was not addressing this request directly, as any of the other answeres failed to do so. 


Whatever, it took me about 20 Mins to write that piece of code and another maybe 20 mins to test, debug and refine it. - So all in all, its a contribution to the community, as it gets asked again over time. 

Similar to the code around Tick-Value, which was really a hazzle and took me maybe 3 weeks to figure out in all details, although I havent shared that code in total, at least I could shed some light on the whole issue.


Sometimes I also mistakenly understand something wrong and it takes me a while to get away from what I believe.