logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

2 Pages12>
Options
Go to last post Go to first unread
Alan R  
#1 Posted : Sunday, January 15, 2012 11:42:22 AM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

MetaStock has a Standard Error indicator. I have tried to use the Help files to get a better understanding of what this indicator is actually measuring but the explanations are very vague. Can anyone explain what the Standard Error indicator is actually measuring and perhaps give an example. Also does anyone have the MetaStock language code for this indicator?
jjstein  
#2 Posted : Wednesday, January 18, 2012 2:20:01 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Standard Error measures how close prices are to a regression line. It's a straight-line based channel, rather than a squiggly band.

In contrast, "Bollinger Bands" are actually just a "moving standard deviation" -- moving variance of price from its moving average.

Two different way of measuring volatility, I suppose. Any statistics book or reference will have the basic formula; probably most spreadsheets, as well.

Here's the MSFL formula.

jjstein  
#3 Posted : Wednesday, January 18, 2012 3:07:21 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Alan -- Sorry, I should know better than to post something when I'm tired; at least, not without plotting it first.

That Equis formula was for StdErr bands.

Had code for an LSQ line lying around, so have adapted to use STE. It's not quite on the money with the built-in "line study", but is very close:

Code:
{_LSQ w/StdErr}

{Least Squares Trendline: Y:=mx+B}
{INPUT}
Period:=Input("Periods:",10,250,30);
Units:=Input("Units:",1,5,2);

{CALC}
PlotPrice:=C;
LSQLine:=LinearReg(PlotPrice,Period);
EndPoint:=LastValue(LSQLine);
Inc:=LastValue(LinRegSlope(PlotPrice,Period));
LastX:=LastValue(Cum(1));
FirstX:=LastX-Period-1;
x:=Cum(1);
LSQ:=If(BarsSince(x=FirstX)>-1,
 EndPoint-((LastX-x)*Inc),
 0);
SE:=LastValue(STE(PlotPrice,Period));

{PLOT}
LSQ+SE*Units;
LSQ;
LSQ-SE*Units;


Alan R  
#4 Posted : Wednesday, January 18, 2012 3:13:35 PM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

I am referring to the Standard Error indicator (STE) from the Indicator QuickList drop down window not from the Standard Error Channel from the toolbar. These are two different indicators. When I write the code for the standard error formula from my statistics text book and overlay it on top of a plot of MetaStocks Standard Error indicator (STE) they are noticably different. The shapes are not the same. This leads me to wanting to know what the MetaStock standard error indicator (STE) is actually measuring and what its formula is. What good is an indicator if you don't know what it is indicating...

jjstein  
#5 Posted : Wednesday, January 18, 2012 4:18:23 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
How about posting the code, so one of us can have a look?

wabbit  
#6 Posted : Wednesday, January 18, 2012 6:00:46 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Alan R wrote:
I am referring to the Standard Error indicator (STE) from the Indicator QuickList drop down window not from the Standard Error Channel from the toolbar. These are two different indicators.


Yes they are, but one requires the other.

Alan R wrote:
When I write the code for the standard error formula from my statistics text book and overlay it on top of a plot of MetaStocks Standard Error indicator (STE) they are noticably different. The shapes are not the same.


Well, we wont know what the problem is as you haven't given us the code which you are trying.

Alan R wrote:
This leads me to wanting to know what the MetaStock standard error indicator (STE) is actually measuring and what its formula is. What good is an indicator if you don't know what it is indicating.


The Standard Error indicator, as the MS Bible and jj have said, is a measure of absolute price-distance from a linear regression line (of a user specified length), but we only get to see the value on the last of that data segment e.g. draw a line-of-best-fit through say 14 data points, then measure the absolute price-distance between the last data item and the trendline at the corresponding time, this is the Standard Error.

To plot the Stand Error Channel, the Standard Error is added and subtracted from the endpoint value of the linear trendline:

Code:

{pseudo code}
UpperBand:=LinearRegressionTrendLine + StandardError;
LowerBand:=LinearRegressionTrendline - StandardError:



Hope this helps.


wabbit [:D]

Alan R  
#7 Posted : Thursday, January 19, 2012 12:48:03 PM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

{Standard Error Code}

n:= 5; {number of data}

mn:= (CLOSE + Ref(CLOSE,-1) + Ref(CLOSE,-2) + Ref(CLOSE,-3) + Ref(CLOSE,-4))/n; {mean}

variance:= (Power(CLOSE - mn,2) + Power(Ref(CLOSE,-1) - mn,2) + Power(Ref(CLOSE,-2) - mn,2) + Power(Ref(CLOSE,-3) - mn,2) + Power(Ref(CLOSE,-4) - mn,2))/n; {variance}

sd:= Sqrt(variance/(n-1)); {standard deviation}

se:= sd/Sqrt(n); {standard error}

se;


{METASTOCK STANDARD ERROR INDICATOR}

STE(CLOSE,5);

jjstein  
#8 Posted : Thursday, January 19, 2012 3:14:17 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Well, this is interesting:

Variance works, Standard Deviation works, but Standard Error is off:

UserPostedImage

I took out the N-1 to be consistent, assuming that's for a sample, while the rest is for population.

Alan R  
#9 Posted : Thursday, January 19, 2012 4:27:31 PM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

jjstein:
Can you supply your version of the code? The amplitude of my indicators seem to be different from yours and I would like to figure out why. Thanks!
wabbit  
#10 Posted : Thursday, January 19, 2012 6:43:13 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Alan R wrote:

se:= sd/Sqrt(n); {standard error}

This is the standard error of the mean, an entirely different concept.


Try these:

Code:

{== Standard Deviation ==}
pds:=14;
data:=CLOSE;

{Bessel's Correction}
{0 = standard deviation of the sample (MS)}
{1 = sample standard deviation}
bessel:=0;

{-- KEEP OUT --}

y:=data;

yBar:=Mov(y,pds,S);
SumY:=Sum(y,pds);
sumYY:=Sum(y*y,pds);

variance:=
(pds*Power(yBar,2))
-(2*yBar*sumY)
+(sumYY);

sd:=Sqrt(variance/(pds-bessel));

{plot}
sd;


Code:

{== Linear Regression Trendline EndPoint ==}

prd:=14;
data:=C;

{-- KEEP OUT --}
x:=Cum(1);
y:=data;

sumX:=Sum(x,prd);
sumY:=Sum(y,prd);
sumXX:=Sum(x*x,prd);
sumXY:=Sum(x*y,prd);

m:=(prd*sumXY-sumX*sumY)/(prd*sumXX-Pwr(sumX,2));
b:=Mov(data,prd,S)-(m*Mov(x,prd,S));

lrtlep:=m*x+b;

{plot}
lrtlep;



Hope they help.


wabbit [:D]


jjstein  
#11 Posted : Thursday, January 19, 2012 7:01:34 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Alan,

1. Your graph is still on your "C:" drive; it's not visible here. Use IMGUR and its BBCODE option to put inside your forum msgs.


2. >>Can you supply your version of the code?

Um, actually, it's YOUR code, though I did take the "-1" out for StdDev, and reformat to check the logic.


3. >>The amplitude of my indicators seem to be different from yours and I would like to figure out why. Thanks!

You might right-click the plot and make sure both are on the same scale (right or left).

*** FLASH ***
I just noticed that you had (and I copied) the division by "n" for "variance", and did it again for the "sd" calculation, using "n-1" (dividing twice, when it should only be once).

I have taken out the 2nd one, but IT DID NOT MAKE ANY DIFFERENCE in the graph. Now THAT is DECIDEDLY strange!

The custom formula for Standard Error still doesn't match.

Anyway, here's the code:

Code:
{Variance}
n:= 5; {number of data}

mn:=
(
CLOSE
+Ref(CLOSE,-1)
+Ref(CLOSE,-2)
+Ref(CLOSE,-3)
+Ref(CLOSE,-4)
)/n; {mean}

variance:=
(
 Power(CLOSE - mn,2)
 + Power(Ref(CLOSE,-1) - mn,2)
 + Power(Ref(CLOSE,-2) - mn,2)
 + Power(Ref(CLOSE,-3) - mn,2)
 + Power(Ref(CLOSE,-4) - mn,2)
)/n; {variance}

variance


Code:
{Standard Deviation}
n:= 5; {number of data}

mn:=
(
CLOSE
+Ref(CLOSE,-1)
+Ref(CLOSE,-2)
+Ref(CLOSE,-3)
+Ref(CLOSE,-4)
)/n; {mean}

variance:=
(
Power(CLOSE - mn,2)
+ Power(Ref(CLOSE,-1) - mn,2)
+ Power(Ref(CLOSE,-2) - mn,2)
+ Power(Ref(CLOSE,-3) - mn,2)
+ Power(Ref(CLOSE,-4) - mn,2)
)/n; {variance}

sd:= Sqrt(variance); {standard deviation}

sd;



Code:
{Standard Error Code}
n:= 5; {number of data}

mn:=
(
CLOSE
+Ref(CLOSE,-1)
+Ref(CLOSE,-2)
+Ref(CLOSE,-3)
+Ref(CLOSE,-4)
)/n; {mean}

variance:=
(
 Power(CLOSE - mn,2)
 + Power(Ref(CLOSE,-1) - mn,2)
 + Power(Ref(CLOSE,-2) - mn,2)
 + Power(Ref(CLOSE,-3) - mn,2)
 + Power(Ref(CLOSE,-4) - mn,2)
)/n; {variance}

sd:=Sqrt(variance); {standard deviation}
se:=sd/Sqrt(n); {standard error}

se;



jjstein  
#12 Posted : Thursday, January 19, 2012 7:21:43 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Wabbit -- Well, SOMEBODY'S been cracking the books, haven't they?

Two questions:

1. I assume correcting for sample size is the same for StdErr (Bessel)?

2. Does MSFL use Sample or Population?


wabbit  
#13 Posted : Thursday, January 19, 2012 8:07:57 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
jjstein wrote:

Wabbit -- Well, SOMEBODY'S been cracking the books, haven't they?


I had written these some time ago.... had to go digging them out the archives.


jjstein wrote:

1. I assume correcting for sample size is the same for StdErr (Bessel)?

I had a quick look at how I thought the Standard Error was implemented in MS, but it appears it not just based on the endpoint, but on the average delta of all the data points along the linear regression trendline segment of length (x). This is problematic in MSFL, because we'd either need an internal loop (cannot be done) or only compute the value for the last bar of the chart (the value of this is questionable). If one could be bothered (and I cannot) an external function could be written with internal loops to prove the concept, or one might even play around the +PREV-PREV to force recomputation of the x-length trendline on each bar, then cycle the summation in order to obtain the averaged standard error?

Equis are not prone to give out their implementations.

jjstein wrote:

2. Does MSFL use Sample or Population?


Code:

{Bessel's Correction}
{0 = standard deviation of the sample (MS)}
{1 = sample standard deviation}
bessel:=0;
wabbit [:D]
jjstein  
#14 Posted : Thursday, January 19, 2012 8:22:28 PM(UTC)
jjstein

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 5/13/2005(UTC)
Posts: 715
Location: Midwest, USA

Was thanked: 1 time(s) in 1 post(s)
Shouldn't that read:
Code:
{Bessel's Correction}
{0 = standard deviation of the POPULATION (MS)}
{1 = sample standard deviation}
bessel:=0;
wabbit  
#15 Posted : Thursday, January 19, 2012 9:05:10 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
Yes it could read as you have described... but I remember it this way from my Uni days (some time ago now -- and I really wasn't a fan of stats!)


Tomato, tomato. Potato, potato. Call it however you remember it best.


wabbit [:D]

wabbit  
#16 Posted : Thursday, January 19, 2012 9:14:26 PM(UTC)
wabbit

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers, Unverified Users
Joined: 10/28/2004(UTC)
Posts: 3,111
Location: Perth, Western Australia

Was thanked: 16 time(s) in 16 post(s)
wabbit wrote:
I had a quick look at how I thought the Standard Error was implemented in MS, but it appears it not just based on the endpoint, but on the average delta of all the data points along the linear regression trendline segment of length (x).


D'Oh! <slaps forehead>

That's going to be in relationship with r-Squared isn't it?

If it's that important, I might have to do some more thinking?



wabbit [:D]

Alan R  
#17 Posted : Friday, January 20, 2012 2:57:43 AM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

Johnathan:

I notice that the left and right scales for the two standard deviation plots are different. Does this mean that the amplitude of the MetaStock standard deviation formula is different from the one I wrote?

Alan,

Johnathan:

Nevermind. When I used the code you supplied (even though it is the same as what I wrote, go figure) and made my left and right scales the same, the standard deviation formula's did lay on top of each other with the same scaling as did the two variance plots. (Stange things going on around here!)

The remaining problem is the standard errors. The code I wrote does not match the MS code. Still working on this.

Alan,

Alan R  
#18 Posted : Friday, January 20, 2012 3:02:44 AM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

OK guys, how do I use the quote feature?
Alan R  
#19 Posted : Friday, January 20, 2012 3:36:53 AM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

Here's what I know from statistics. When stating the standard error of some data without any further reference it is inferred that the standard error is the standard error of the mean. So when MetaStock supplies an indicator of standard error without any further reference I have to assume that it is the standard error of the mean. However, due to the differences between the standard error formual I wrote and the plot of the standard error from the MS indicator there seems to be some difference. I am trying to figure out what the difference is so I can then know how to properly interpret MS's standard error indicator.
Alan R  
#20 Posted : Friday, January 20, 2012 5:06:43 AM(UTC)
Alan R

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 9/23/2009(UTC)
Posts: 51

I figured it out.

From statistics the standard error, without any additional reference to what is being measured, is inferred to mean the standard error of the mean. MetaStocks standard error STE() is not the standard error of the mean as it infers but is the standard error of the residuals. The residuals are the difference between the straight line linear regession line and the actual data, for each day. Further reference to the standard error symbol should be noted to make this distinction, such as RSE or RSTE to reference the Residual Standard Error.

The following is my code for the Residual Standard Error RSE which overlaps MetaStocks standard error indicator STE().

RESIDUAL STANDARD ERROR (CODE)

{Standard Error of the residuals}

n:= 5; {number of data}

{Takes the least means square fit linear regession line over the last five days and calculates its value for each day}
lrl:= LinearReg(CLOSE,n) - (0)*LinRegSlope(CLOSE,n); {linear regession line today}
lrl1:= LinearReg(CLOSE,n) - (1)*LinRegSlope(CLOSE,n); {linear regession line 1 day ago}
lrl2:= LinearReg(CLOSE,n) - (2)*LinRegSlope(CLOSE,n); {linear regession line 2 days ago}
lrl3:= LinearReg(CLOSE,n) - (3)*LinRegSlope(CLOSE,n); {linear regession line 3 days ago}
lrl4:= LinearReg(CLOSE,n) - (4)*LinRegSlope(CLOSE,n); {linear regession line 4 days ago}


{Calculates the residual for eack day by taking the close of each day and subtracting the value of the least means square fit linear regession line for that day from it.}
r0:= CLOSE - lrl; {residuals today}
r1:= Ref(CLOSE,-1) - lrl1; {residuals 1 day ago}
r2:= Ref(CLOSE,-2) - lrl2; {residuals 2 days ago}
r3:= Ref(CLOSE,-3) - lrl3; {residuals 3 days ago}
r4:= Ref(CLOSE,-4) - lrl4; {residuals 4 days ago}


{Squares each days residual and sums them together.}
srs:= Power(r0,2)
+ Power(r1,2)
+ Power(r2,2)
+ Power(r3,2)
+ Power(r4,2); {sum of residuals squared}

{Divides the srs by n-2 and takes the square root of the result. This is the standard error of the residuals.}
rse:= Sqrt(srs/(n-2)); {standard error of the residuals}

{plots the standard errror of the residuals}
rse;

"Now that I know what STE() is measuring I can properly interpret its results..."

Users browsing this topic
Guest (Hidden)
Similar Topics
Standard Error Channel (Formula Assistance)
by Derek Worswick 9/21/2016 2:50:12 PM(UTC)
Relative standard error (Formula Assistance)
by michmauro 10/20/2012 10:18:10 AM(UTC)
Standard Error Indicator - What does it indicate? (Advanced Coding Techniques)
by Alan R 1/18/2012 9:20:04 AM(UTC)
Standard Error Bands (Formula Assistance)
by Patrick 8/19/2005 4:41:49 PM(UTC)
2 Pages12>
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.