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

Notification

Icon
Error

Options
Go to last post Go to first unread
wblam  
#1 Posted : Friday, April 27, 2007 4:34:31 PM(UTC)
wblam

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/25/2006(UTC)
Posts: 79

I adapt the indicator from this post:

http://forum.equis.com/forums/post/16502.aspx

to:

time:=Input("number of periods",2,200,100); end:=LastValue(Cum(1));
pt:=LastValue(LinearReg(log(c),time)); slope:=LastValue(LinRegSlope(log(c),time));

start:=BarsSince(Cum(1)>=end-(time-1));
center:=If(start=0,pt - (slope*(end-Cum(1))),0);
topdis:= LastValue(HHV(log(h)-center,time));
botdis:= LastValue(HHV(center-log(L),time));
dis:=Max(topdis,botdis);
Exp(center+dis);
Exp(center);
Exp(center-dis);

It calculate the linear regression line of log (price) and work correctly.

But when I try to combine the 2 into one like this:

periods",2,200,100); end:=LastValue(Cum(1));
scale:=Input("y scale - linear(1)/log(2)",1,2,1);
cc:=if(scale=2,log(c),c);
hh:=if(scale=2,log(h),h);
ll:=if(scale=2,log(l),l);
pt:=LastValue(LinearReg(cc,time)); slope:=LastValue(LinRegSlope(cc,time));

start:=BarsSince(Cum(1)>=end-(time-1));
center:=If(start=0,pt - (slope*(end-Cum(1))),0);
topdis:= LastValue(HHV(hh-center,time));
botdis:= LastValue(HHV(center-ll,time));
dis:=Max(topdis,botdis);
if(scale=2,Exp(center+dis),center+dis);
if(scale=2,Exp(center),center);
if(scale=2,Exp(center-dis),center-dis);

Then it has overflow problem in the exp() function if I choose linear scale for y but no problem for choosing log scale for y.

Could someone tell me what wrong of my second formula and teach me how to correct it. Thanks in advance.

wabbit  
#2 Posted : Friday, April 27, 2007 7:46:33 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)
This code works.... you just need to be careful on which charts you plot the function.

If you try plotting the indicator on a penny dreadful, it has no problems, but if you try to plot this on say the Dow Jones it will create an overflow because you have asked MS to compute

exp(12700 ish) = 3.467E+5515

or to put it another way, 3467 with five thousand five hundred and twelve zeros after it.


No wonder there is an overflow error!



wabbit [:D]


wblam  
#3 Posted : Saturday, April 28, 2007 8:30:59 AM(UTC)
wblam

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/25/2006(UTC)
Posts: 79

Thanks again wabbit, I am great appreciate that you are alway the one who answer my questions.

Yes, the problem happen when I try to plot it on an index chart.

However I still can not understand is that, my 1st indicator has same formula as the 2nd one. It does not have such problem.

Furthermore, the problem happen when I choose linear scale for y and this should execute the formula without the log and exp function which equivalent to the orginal indicator.

My 2nd indicator just try to combine my 1st one with the orginal one.

BTW my adapted indicator first take the log of the numbers and finally calculate the exp() of the result to make it plot correctly on the chart.

The final result is not as large as in your example.

exp(log(x))=x

Maybe I use too many 'if ' function or use it wrongly.

wabbit  
#4 Posted : Saturday, April 28, 2007 11:17:21 AM(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)
wblam,

The problem you are encountering is because the MetaStock Formula Language fully evaluates every expression. You were in fact trying to get MS to do some very large number maths, so what we have to do is trick MS into not seeing the seeds of these very large number calculations:

Here is your function reworked, please try to see the 'fooling' of MS, you might need it again the future?

Code:

time:=Input("number of periods",2,2000,100);
scale:=Input("y scale - linear(1)/log(2)",1,2,2);

cc:=If(scale=1,C,Log(C));
hh:=If(scale=1,H,Log(H));
ll:=If(scale=1,L,Log(L));

pt:=LastValue(LinearReg(cc,time));
slope:=LastValue(LinRegSlope(cc,time));

end:=LastValue(Cum(1));
start:=BarsSince(Cum(1)>=end-(time-1));

center:=If(start=0,pt-(slope*(end-Cum(1))),0);
topdis:=LastValue(HHV(hh-center,time));
botdis:=LastValue(HHV(center-ll,time));

dis:=Max(topdis,botdis);

{lets fool MS}
l1:=If(scale=2,center+dis,0);
l2:=If(scale=2,center,0);
l3:=If(scale=2,center-dis,0);

ul:=If(scale=1,center+dis,Exp(l1));
cl:=If(scale=1,center,Exp(l2));
ll:=If(scale=1,center-dis,Exp(l3));

{plot/return}
ul;cl;ll;




Hope this helps.

wabbit [:D]

P.S. Of course, instead of:
{lets fool MS}
l1:=If(scale=2,center+dis,0);
l2:=If(scale=2,center,0);
l3:=If(scale=2,center-dis,0);

you could write:
{lets fool MS}
l1:=(scale=2)*(center+dis);
l2:=(scale=2)*center;
l3:=(scale=2)*(center-dis);

which I think is a little neater, it is also slightly faster to execute too.

wblam  
#5 Posted : Sunday, April 29, 2007 8:55:42 AM(UTC)
wblam

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 11/25/2006(UTC)
Posts: 79

wabbit wrote:

{lets fool MS}

wabbit,

Yes it work now. Thank you for your help.

MS is really a fool!

wabbit  
#6 Posted : Sunday, April 29, 2007 9:08:48 AM(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)
No problems. Glad I could help.

wabbit [:D]


uasish  
#7 Posted : Sunday, April 29, 2007 9:39:38 AM(UTC)
uasish

Rank: Advanced Member

Groups: Registered, Registered Users
Joined: 8/13/2005(UTC)
Posts: 170

Thanks: 7 times

Wabbit,

Learnt,thks.

Asish

Users browsing this topic
Guest (Hidden)
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.