Jump to content

Welcome to the new Traders Laboratory! Please bear with us as we finish the migration over the next few days. If you find any issues, want to leave feedback, get in touch with us, or offer suggestions please post to the Support forum here.

  • Welcome Guests

    Welcome. You are currently viewing the forum as a guest which does not give you access to all the great features at Traders Laboratory such as interacting with members, access to all forums, downloading attachments, and eligibility to win free giveaways. Registration is fast, simple and absolutely free. Create a FREE Traders Laboratory account here.


Help About Geometric Mean

Recommended Posts



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: with an error (Floating-point Invalid Operation Exception)


what do you think?


Inputs :
 Price(Close), GeometricMeanLength(500);

  PriceUp(False), PriceDn(False),
  NextDayReturnUp(0), CounterUp(0), NextDayReturnUpGeometricMean(0),
  NextDayReturnDn(0), CounterDn(0), NextDayReturnDnGeometricMean(0);

  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
  NextDayReturnUp = ((Price-Price[1])/Price[1])*100;
  For Value1 = 0 to GeometricMeanLength
     NextDayReturnUpDate[1000-Value1] = NextDayReturnUpDate[1000-Value1];
     NextDayReturnUpBar[1000-Value1] = NextDayReturnUpBar[1000-Value1];
     NextDayReturnUpVal[1000-Value1] = NextDayReturnUpVal[1000-Value1];
  NextDayReturnUpDate[0] = Date;
  NextDayReturnUpBar[0] = BarNumber;
  NextDayReturnUpVal[0] = NextDayReturnUp;
  CounterUp = CounterUp + 1;
  If CounterUp > GeometricMeanLength then
     NextDayReturnUpGeometricMean = GeometricMean(NextDayReturnUp, GeometricMeanLength);

If PriceDn then
  NextDayReturnDn = ((Price-Price[1])/Price[1])*100;
  For Value1 = 0 to GeometricMeanLength
     NextDayReturnDnDate[1000-Value1] = NextDayReturnDnDate[1000-Value1];
     NextDayReturnDnBar[1000-Value1] = NextDayReturnDnBar[1000-Value1];
     NextDayReturnDnVal[1000-Value1] = NextDayReturnDnVal[1000-Value1];
  NextDayReturnDnDate[0] = Date;
  NextDayReturnDnBar[0] = BarNumber;
  NextDayReturnDnVal[0] = NextDayReturnDn;
  CounterDn = CounterDn + 1;
  If CounterDn > GeometricMeanLength then
     NextDayReturnDnGeometricMean = GeometricMean(NextDayReturnDn, GeometricMeanLength);

Plot1( NextDayReturnUp, "NextDayReturnUp" );
Plot2( NextDayReturnDn, "NextDayReturnDn" );
plot3(0, "Zero");
Plot4( NextDayReturnUpGeometricMean, "NextDayReturnUpGeometricMean");
Plot5( NextDayReturnDnGeometricMean , "NextDayReturnDnGeometricMean");


and the geometric mean function


{ Geometric Mean }
  Price(NumericSeries), Length(NumericSimple);

  Product(0), ix(0);

Product = 1;
For ix = 0 to Length-1 
  Product = Product * Price[ix];
GeometricMean = Power(Product, 1/Length);


thank you


Share this post

Link to post
Share on other sites



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.





Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.