How to get average from indicator over last X minutes

 

I have written a custom indicator, and referenced it in an EA using iCustom.  The indicator gives out a reading of either +1,-1, or 0, depending on market conditions.  A new reading is given on every tick.  I'd simply like to tally up the +1 readings and tally up the -1 readings and see what the average between them was over the last 20 minutes.

I'm just not experienced enough with coding to know how to go about doing this.  I thought about creating a 2 dimensional array with one column for whether the tick had a +1, -1, or 0 reading, and a time stamp for the other column.  Then writing a For Loop to go through the array and tally up the +1s and the -1s if the time stamp was witihin the last 20 minutes.  But that brings up a problem of an ever expanding array.  With every tick more data will be added to the array.  If I let it run for the whole week, the array will become huge and clog up too much memory.

Ideas?

 

You can use an array of a fixed length (it should be long enough to hold 20+ mins of ticks for the instrument in question). Then you have two options: shift an array with each tick or organize a ring buffer (e.g. using two variables holding the index of the buffer's head and tail).


Shifting an array is simpler as you can use ArrayCopy() for that. A ring buffer will be faster though.

 
Ok. Thanks.  How do I do that?  Do you know of any ring buffer code I can look at?  I don't understand these concepts.
 
I couldn't log on to this forum for the last 20 hours or so. Anybody else notice this? Anyway, I'm bumping this up to the top. I still need answers.
 
 
inkexit:
I couldn't log on to this forum for the last 20 hours or so. Anybody else notice this? Anyway, I'm bumping this up to the top. I still need answers.
 

Hey inkexit

Did you ever come up with a solution regarding controlling your array size, only keeping the values you want in your current "rolling window" of designated time?


Your help would be appreciated. I'm incurring the same issue.


What did you do to solve your problem?


Tks


jrc

Reason: