# Calculate average of a dynamic array

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 123 times

### Calculate average of a dynamic array

I made an indicator which calculates the openprice of today minus the close of yesterday. (the openingsgap). I used an dynamic array for this.

What I then do is to make all the outcome an positive number. So if the openinggap was -0,40. I multiple this with -1 to get 0,40. I got this to work.

But now I want to calculate the average of all the opening gaps. This to determine the risk involved when I have a strategy that will go overnight.

I tried to use AverageArray, but it is not working.

Code: Select all

`variables:  Openingsgap(0),       Count(0),       Averagegap(0);       array: open_gap[](0),          close_gap[](0),          avg_gap[] (0);   Count = count;      if date <> date[1] then beginArray_setmaxindex(open_gap, count);open_gap[count] = opend(0);Array_setmaxindex(close_gap, count); close_gap[count] = closed(1);Openingsgap = open_gap[count] - close_gap[count];if openingsgap < 0 thenopeningsgap = (open_gap[count] - close_gap[count])*-1;Array_setmaxindex(avg_gap, count);avg_gap[count] = (opend(0) - closed(1));averagegap = averagearray(avg_gap, count);Print("Date: ",formatdate("dd-MM-yyyy ",eldatetodatetime(date))," open ", open_gap[count]," Close Yesterday ", close_gap[count]," openingsgap ", Openingsgap," average", Averagegap);`

Can someone give me a hint?

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:I made an indicator which calculates the openprice of today minus the close of yesterday. (the openingsgap). I used an dynamic array for this.

What I then do is to make all the outcome an positive number. So if the openinggap was -0,40. I multiple this with -1 to get 0,40. I got this to work.

But now I want to calculate the average of all the opening gaps. This to determine the risk involved when I have a strategy that will go overnight.

I tried to use AverageArray, but it is not working.

Code: Select all

`variables:  Openingsgap(0),       Count(0),       Averagegap(0);       array: open_gap[](0),          close_gap[](0),          avg_gap[] (0);   Count = count;      if date <> date[1] then beginArray_setmaxindex(open_gap, count);open_gap[count] = opend(0);Array_setmaxindex(close_gap, count); close_gap[count] = closed(1);Openingsgap = open_gap[count] - close_gap[count];if openingsgap < 0 thenopeningsgap = (open_gap[count] - close_gap[count])*-1;Array_setmaxindex(avg_gap, count);avg_gap[count] = (opend(0) - closed(1));averagegap = averagearray(avg_gap, count);Print("Date: ",formatdate("dd-MM-yyyy ",eldatetodatetime(date))," open ", open_gap[count]," Close Yesterday ", close_gap[count]," openingsgap ", Openingsgap," average", Averagegap);`

Can someone give me a hint?

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:I made an indicator which calculates the openprice of today minus the close of yesterday. (the openingsgap). I used an dynamic array for this.

What I then do is to make all the outcome an positive number. So if the openinggap was -0,40. I multiple this with -1 to get 0,40. I got this to work.

But now I want to calculate the average of all the opening gaps. This to determine the risk involved when I have a strategy that will go overnight.

I tried to use AverageArray, but it is not working.

What do you mean by "but it is not working"?

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 123 times

### Re: Calculate average of a dynamic array

Hi Tj,

The chart resolution is 1 minute. And I tried different average reserve words. With avglist for example, it is not calculating the average over all the opening gaps but only over the opening gap of that particular day. If the opening gap is 0,15 on a day than the average is also 0,15.

So it seems like it is not seeing all the opening gaps and take an average of that. The problem is that you can't tell the range of the opening gaps because this will be determend dynamically.

That is why I tried to use the AverageArray, as in the attached code, but this gives an error when adding the indicator to the chart. (Array bounds. wrong index value: 1).

With the AverageArray function I could not get the indicator to work at all, but with avglist it is not calculating the average over all the opening gaps.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:Hi Tj,

The chart resolution is 1 minute. And I tried different average reserve words. With avglist for example, it is not calculating the average over all the opening gaps but only over the opening gap of that particular day. If the opening gap is 0,15 on a day than the average is also 0,15.

So it seems like it is not seeing all the opening gaps and take an average of that. The problem is that you can't tell the range of the opening gaps because this will be determend dynamically.

That is why I tried to use the AverageArray, as in the attached code, but this gives an error when adding the indicator to the chart. (Array bounds. wrong index value: 1).

With the AverageArray function I could not get the indicator to work at all, but with avglist it is not calculating the average over all the opening gaps.

What is this line for?
What does it do?

Code: Select all

`Count = count;`

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 123 times

### Re: Calculate average of a dynamic array

The count variable is used to set the maximum index value I read in the essential programmers guide. But I am new to dynamic arrays so that is maybe not the right code to use.

The dynamic arrays with the openprice and closeprice works ok though.

The calculating of the opening gaps also works. But the calculating of the average opening gaps does not work. I probably doing something wrong in the code. But to be sure, is it possible to do calculating like average on a dynamic array?

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:The count variable is used to set the maximum index value I read in the essential programmers guide. But I am new to dynamic arrays so that is maybe not the right code to use.

The dynamic arrays with the openprice and closeprice works ok though.

The calculating of the opening gaps also works. But the calculating of the average opening gaps does not work. I probably doing something wrong in the code. But to be sure, is it possible to do calculating like average on a dynamic array?

You have declared your array to have zero elements.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

regarding this snippet:

Code: Select all

`Openingsgap = open_gap[count] - close_gap[count];if openingsgap < 0 thenopeningsgap = (open_gap[count] - close_gap[count])*-1;`

it could be written as:

Code: Select all

`Openingsgap = absvalue(open - close[1]);`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:I made an indicator which calculates the openprice of today minus the close of yesterday. (the openingsgap). I used an dynamic array for this.

What I then do is to make all the outcome an positive number. So if the openinggap was -0,40. I multiple this with -1 to get 0,40. I got this to work.

But now I want to calculate the average of all the opening gaps. This to determine the risk involved when I have a strategy that will go overnight.
...Can someone give me a hint?

do you want this average to go on indefinitely?

ie. if you have 50 days of data on the chart, you want a 50 average of the gap,
and if you have 200 days of data on the chart, you want a 200 average of the gap?

ie. not a running moving average?

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 123 times

### Re: Calculate average of a dynamic array

do you want this average to go on indefinitely?

ie. if you have 50 days of data on the chart, you want a 50 average of the gap,
and if you have 200 days of data on the chart, you want a 200 average of the gap?

Exactly! That is what I try to do.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

What is this line for?
What does it do?

Code: Select all

`Count = count;`

evdl wrote:The count variable is used to set the maximum index value I read in the essential programmers guide. But I am new to dynamic arrays so that is maybe not the right code to use.

The dynamic arrays with the openprice and closeprice works ok though.

The calculating of the opening gaps also works. But the calculating of the average opening gaps does not work. I probably doing something wrong in the code. But to be sure, is it possible to do calculating like average on a dynamic array?

the line should be

Code: Select all

`Count = count + 1;`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

Give this a try.
Add the indicator as a subchart. Plot the gap as histogram and average as a line.

Code: Select all

`// indicator: average opening gap// version: beta 0.3// date: 20120622// http://www.multicharts.com/discussion/viewtopic.php?f=1&t=10540//// description: this indicator calculates the average gap between // previous day's close and today's opening price// the average is taken over the entire series of days on the chart//variables:  Count(0),Averagegap(0);       array: open_gap[](0),close_gap[](0),day_gap[](0);{========== end of declarations ==========}        if date > date[1] then begin   // increment the count at the beginning of a new day   Count = count + 1;   // expand the array by one   Array_setmaxindex(open_gap, count);   Array_setmaxindex(close_gap, count);    Array_setmaxindex(day_gap, count);   // post data to array   open_gap[count] = open;   close_gap[count] = close[1];   day_gap[count] = absvalue(open - close[1]);   // calculate the average gap   averagegap = averagearray(day_gap, count);   Print("Date: ",formatdate("dd-MM-yyyy ",eldatetodatetime(date)),   " open ", open_gap[count],   " Close Yesterday ", close_gap[count],   " openingsgap ", day_gap[count],   " average", Averagegap,   "/", count:0:0, " days");      plot100(day_gap[count], "Gap");  // set as histogram   plot200(Averagegap, "Avg Gap");  // set as lineend;`

evdl
Posts: 401
Joined: 19 Jan 2011
Location: Netherlands
Has thanked: 85 times
Been thanked: 123 times

### Re: Calculate average of a dynamic array

Hi Tj,

That is more then a hint

After a while trying, you read the manuals and the code, but you just can't see what you need to change at a certain moment....

The code works great.

Edwin

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

evdl wrote:Hi Tj,

That is more then a hint

After a while trying, you read the manuals and the code, but you just can't see what you need to change at a certain moment....

The code works great.

Edwin

You are welcome!
I am glad the indicator works as you have envisioned.
I know you have worked hard, so I don't mind to give you a bit extra help.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Calculate average of a dynamic array

Here's a version that calculates the gap in a moving average.
Add the indicator in a subchart. Set the Gap to histogram, and average as a line.

Enjoy.
TJ

Code: Select all

`// indicator: opening gap moving average// version: beta 0.1// date: 20120623// http://www.multicharts.com/discussion/viewtopic.php?f=1&t=10540//// description: this indicator calculates the moving average of the daily gap between // previous day's close and today's opening price// the moving average period can be set by the user//input:MA.length(10);variables:  Count(0),Averagegap(0);array: open_gap[](0),close_gap[](0),day_gap[](0);{========== end of declarations ==========}// set up array sizeArray_setmaxindex(open_gap, MA.length);Array_setmaxindex(close_gap, MA.length); Array_setmaxindex(day_gap, MA.length);   if date > date[1] then begin   // shift array history by one element in preparation    // to accept the new data for the new day   array_copy(day_gap, 2, day_gap, 1, MA.length-1);   // assign the open/close/gap data to array   open_gap[MA.length] = open;   close_gap[MA.length] = close[1];   day_gap[MA.length] = absvalue(open - close[1]);   // calculate the average of gaps   averagegap = averagearray(day_gap, MA.length);      plot100(day_gap[MA.length], "daily gap");  // set as histogram   plot200(Averagegap, "avg gap"); // set as line   Print("Date: ", formatdate("dd-MM-yyyy ", eldatetodatetime(date)),   " open ", open_gap[MA.length],   " Close Yesterday ", close_gap[MA.length],   " daily gap ", day_gap[MA.length],   " average", Averagegap,   "/", MA.length:0:0, " days");end;`