473,888 Members | 1,559 Online

# arrays

Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #1
8 2371
Hi,
Find below some link Displaying Arrays .. you may find a solution to your
case:
http://msdn2.microsoft.com/en-us/lib...53(VS.71).aspx

Husam Al-A'araj

"Ro********@yah oo.co.uk" wrote:
Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #2
Rober,

Do the "columns" in your data have any significance, or are they just
sets of 15 values? If they are sets of 15 values, then I would create a
List<int[](or List<decimal[]>, or List<double[]>), creating your arrays,
and then adding them to the list, then have one method that takes your list

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
<Ro********@yah oo.co.ukwrote in message
news:11******** *************@w 5g2000hsg.googl egroups.com...
Hi, What is the best way to use an array to find moving averages. I
want to calculate a 15 day and 41 day m/a. I know my array will need
to contain 12 columns, but the rows need to be dynamic. The row size
will need to be aminium of 50 rows. So far Im thinking of either
creating 2 arrays, one for 15 and one for 41 day m/a. Keeping them
each to that row size then iterate through them creating new arrays
when new data added. Eg when 16 data point added to the 15 day m/a,
remove the first data point, add the new data point at the end. I
think im right in thinking that I would have to add the data to a new
array to do this. The other idea was to create an array 12 cols wide
and some how have dynamic rows. Add data indefinatly, then itirate
through the array in reverse, first back 15 points, summing as I go,
and the again 41 points to calculate the average. I dont know how to
iterate in reverse or create an array with set col width but dynamic
rows. Any ideas appreciated.
Regards Rober

May 12 '07 #3
to 12 different shares, and the values represent changes updated every
minute. So over a day there can be many values, whence the need for
dynamic rows.
Regards Robert

May 12 '07 #4
On Sat, 12 May 2007 02:49:56 -0700, Ro********@yaho o.co.uk
<Ro********@yah oo.co.ukwrote:
Hi, What is the best way to use an array to find moving averages. [...]
In answer to the basic question, I would say that two good solutions would
be to either use the Queue<generic class, or implement a circular buffer
yourself with an array. Using the Queue<class involves a little more
overhead "behind the scenes", but allows your own code to most closely
match what it is you're actually doing. A circular buffer is slightly
more efficient, but the implementation in your own code would be less
direct.

Now, that said, your question has a bunch of other pieces of information
that may or may not be relevant to those of us trying to answer it. If
that information is relevant, IMHO you should do a better job of
explaining why. The information about the length of the moving average
(15 and 41 days) seems useful, but I'm left having no idea why you say "my
array will need to contain 12 columns", nor do I know why you say "the row
size will need to be a minimum of 50 rows". These statements may well be
correct, but we don't have enough context from you to understand why.

As an example of what I mentioned above, here are a couple of
implementations of a moving average you might consider using as an outline

// Circular buffer

void MovingAverage(d ouble[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAver age;
double[] pricesMoving = new double[cdayAverage];
int ipriceCur, ipriceCircle, cpriceAveraged;

ipriceCur = 0;
ipriceCircle = 0;
cpriceAveraged = 0;

while (ipriceCur < pricesHistory.L ength)
{
double priceCur = pricesHistory[ipriceCur];

priceSum += priceCur;

if (cpriceAveraged < pricesMoving.Le ngth)
{
cpriceAveraged+ +;
}
else
{
ipriceCircle = ipriceCircle % pricesMoving.Le ngth;
priceSum -= pricesMoving[ipriceCircle];
}

pricesMoving[ipriceCircle++] = priceCur;

priceMovingAver age = priceSum / cpriceAveraged;
}
}
// Queue

void MovingAverage(d ouble[] pricesHistory, int cdayAverage)
{
double priceSum, priceMovingAver age;
Queue<doublepri cesMoving = new Queue<double>(c dayAverage)
int ipriceCur;

ipriceCur = 0;

while (ipriceCur < pricesHistory.L ength)
{
double priceCur = pricesHistory[ipriceCur];

priceSum += priceCur;

if (!(pricesMoving .Count < cdayAverage))
{
priceSum -= pricesMoving.De queue();
}

pricesMoving.En queue(priceCur) ;

priceMovingAver age = priceSum / pricesMoving.Co unt;
}
}

In either algorithm, the input data is passed in with "pricesHistory" , the
number of days to average is pased in with "cdayAverag e", and the
"priceMovingAve rage" variable contains, at the end of each loop, the
current moving average given the input data.

Hope that helps.

Pete
May 12 '07 #5
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share. Then when I collected the data,

May 12 '07 #6
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share. Then when I collected the data, Id add the
result for each share to its relevent column. Id have to have a new
row for each set of new data. The min 50 rows was to make sure that
the 41 day average was calculated correctly. The 15 and 41 day would
be calculated on the same data.In my program I iterate through each
share, updating the data to a row, andhopefully to an array to do the
ave calcs. Thanks for your code example.
Regards Robert
May 12 '07 #7
On Sat, 12 May 2007 13:25:20 -0700, Ro********@yaho o.co.uk
<Ro********@yah oo.co.ukwrote:
not very clear. My data is for 12 shares, and I was thinking Id need 1
column for each share.
If you want to keep all of your data in a single two-dimensional array,
then yes...I see now why you would want 12 columns.

That said, I think it might make more sense to just maintain a per-share
moving average data structure, rather than trying to combine everything
into a single array.
Then when I collected the data, Id add the
result for each share to its relevent column. Id have to have a new
row for each set of new data. The min 50 rows was to make sure that
the 41 day average was calculated correctly.
Well, you only need 41 rows (or whatever) for that.
The 15 and 41 day would
be calculated on the same data.
Well, part of the question if implementation depends on exactly how you're
going to be using the data. The code I posted would be most useful if you
intended to generate a daily moving average value for every day in a
series of days for which you have data.

However, if you are only going to generate the "moving average" on request
for a particular day, then it makes more sense to just retrieve the
maximum number of days (41 in this case) up to and including the day for
which you want the calculation, and then calculate the averages directly
for that day only. This would correspond more closely to your "rows and
columns" description, especially if you are retrieving the data from a
database in a way that creates such a two-dimensional array.

Pete
May 13 '07 #8
Thanks again for your reply. The more I delve into this, I think your
suggestion of maintaining a per share data structure is the best
route. The m/a are calculated throughout the day. Eg 41 minutes into
the day the first 15/41 point is calculated, then from that point
onwards its maintained for each share. The calcs itself are easy
enough, its the 12 different share arrays/variables I struggle with.
Regards Robert

May 13 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.