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.

## Recommended Posts

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

what do you think?

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");

and the geometric mean function

{ 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

##### Share on other sites

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

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×

• 0
• -1
• 0
• -1
• -1
• ### Posts

• SPOT Spotify stock good move higher off the 326 gap support area at https://stockconsultant.com/?SPOT
• SNAP stock, nice bounce off the 8.66 double support area, from Stocks to Watch at https://stockconsultant.com/?SNAP
• SCHW Charles Schwab stock someone's buying at the 62.36 triple+ support area, bullish indicators at https://stockconsultant.com/?SCHW
• PDSB PDS Biotechnology stock watch, attempting to move higher off the 3 triple+ support area, needs more volume to kickstart the rally at https://stockconsultant.com/?PDSB
• NVDA NVIDIA stock big day off the 106 support, easy setup at https://stockconsultant.com/?NVDA
×
×
• Create New...