# Help about Geometric mean

Questions about MultiCharts and user contributed studies.
nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Help about Geometric mean

Hello

I need help about my study (for a daily chart)

I want to insert in the study a 2 year moving average (geometric mean) of the next-day returns following an up day and a down day

here is the code:

Code: Select all

```Inputs : Price(Close); Variables: PriceUp(False), PriceDn(False), NextDayReturnUp(0), NextDayReturnDn(0); PriceUp = Price[1] > Price[2]; PriceDn = Price[1] <= Price[2]; If PriceUp then begin NextDayReturnUp = ((Price-Price[1])/Price[1])*100; end; If PriceDn then begin NextDayReturnDn = ((Price-Price[1])/Price[1])*100; end; Plot1( NextDayReturnUp, "NextDayReturnUp" ); Plot2( NextDayReturnDn, "NextDayReturnDn" ); plot3(0, "Zero"); ```
and the geometric mean function

Code: Select all

``` { Geometric Mean } Inputs: Price(NumericSeries), Length(NumericSimple); Variables: Product(0), ix(0); Product = 1; For ix = 0 to Length-1 begin Product = Product * Price[ix]; end; GeometricMean = Power(Product, 1/Length);```
thank you
Nuno

nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: Help about Geometric mean

I continue to develop the study but i have an error
(Floating-point Invalid Operation Exception)

what do you think?

Code: Select all

``` Inputs : Price(Close), GeometricMeanLength(500); Variables: PriceUp(False), PriceDn(False), NextDayReturnUp(0), CounterUp(0), NextDayReturnUpGeometricMean(0), NextDayReturnDn(0), CounterDn(0), NextDayReturnDnGeometricMean(0); Arrays: NextDayReturnUpDate[1000](0), NextDayReturnUpBar[1000](0), NextDayReturnUpVal[1000](0), NextDayReturnDnDate[1000](0), NextDayReturnDnBar[1000](0), NextDayReturnDnVal[1000](0); PriceUp = Price[1] > Price[2]; PriceDn = Price[1] <= Price[2]; If PriceUp then begin NextDayReturnUp = ((Price-Price[1])/Price[1])*100; For Value1 = 0 to GeometricMeanLength begin NextDayReturnUpDate[1000-Value1] = NextDayReturnUpDate[1000-Value1]; NextDayReturnUpBar[1000-Value1] = NextDayReturnUpBar[1000-Value1]; NextDayReturnUpVal[1000-Value1] = NextDayReturnUpVal[1000-Value1]; end; NextDayReturnUpDate[0] = Date; NextDayReturnUpBar[0] = BarNumber; NextDayReturnUpVal[0] = NextDayReturnUp; CounterUp = CounterUp + 1; end; If CounterUp > GeometricMeanLength then NextDayReturnUpGeometricMean = GeometricMean(NextDayReturnUp, GeometricMeanLength); If PriceDn then begin NextDayReturnDn = ((Price-Price[1])/Price[1])*100; For Value1 = 0 to GeometricMeanLength begin NextDayReturnDnDate[1000-Value1] = NextDayReturnDnDate[1000-Value1]; NextDayReturnDnBar[1000-Value1] = NextDayReturnDnBar[1000-Value1]; NextDayReturnDnVal[1000-Value1] = NextDayReturnDnVal[1000-Value1]; end; NextDayReturnDnDate[0] = Date; NextDayReturnDnBar[0] = BarNumber; NextDayReturnDnVal[0] = NextDayReturnDn; CounterDn = CounterDn + 1; end; If CounterDn > GeometricMeanLength then NextDayReturnDnGeometricMean = GeometricMean(NextDayReturnDn, GeometricMeanLength); Plot1( NextDayReturnUp, "NextDayReturnUp" ); Plot2( NextDayReturnDn, "NextDayReturnDn" ); plot3(0, "Zero"); Plot4( NextDayReturnUpGeometricMean, "NextDayReturnUpGeometricMean"); Plot5( NextDayReturnDnGeometricMean , "NextDayReturnDnGeometricMean"); ```

Henry MultiСharts
Posts: 9086
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2921 times

### Re: Help about Geometric mean

Hello nuno-online,

Please refer to the following article: Floating-Point_Division_by_Zero_Error_Message

Smoky
Posts: 399
Joined: 03 Dec 2010
Location: Thailand
Has thanked: 65 times
Been thanked: 104 times

### Re: Help about Geometric mean

Compile without error for me

nuno-online
Posts: 174
Joined: 31 Jan 2006
Has thanked: 74 times
Been thanked: 5 times

### Re: Help about Geometric mean

Hello

i always have the same error

Any help would be apprecied

here the screenshot about what i want to do:

The graph below shows a 5-year moving average (geometric average) of the next-day returns on the S&P 500 following an up day (red) and a down day (blue) from 1950 to 06/2008.

(1) returns have been normalized by subtracting the average return of all days in each observation period to remove the influence of bull vs bear markets,
(2) averages are geometric.

Nuno
Attachments
graph.gif (16.4 KiB) Viewed 573 times

Henry MultiСharts
Posts: 9086
Joined: 25 Aug 2011
Has thanked: 1258 times
Been thanked: 2921 times

### Re: Help about Geometric mean

Hello Nuno,

Do you still have the error message? If yes then please specify the error you have and attach the workspace you are using.