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

Notification

Icon
Error

Options
Go to last post Go to first unread
tsenks  
#1 Posted : Friday, December 25, 2009 12:19:23 AM(UTC)
tsenks

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/8/2008(UTC)
Posts: 3

Any one know why the Weekly EMA value calculated using the following formula difference from Weekly EMA value calculated by Metastock? This happen especially in the first 25 bars : {Time Period: 5 Days} { User inputs } pds:=Input("Weekly EMA periods",1,520,5); shift:=1+Input("EMA vertical shift %",-100,100,0)/100; { Reference EOW signals } WkEnd:= Cum(1)=LastValue(Cum(1)) OR FmlVar("Week's true Start & End","WEEKEND"); { Week's Close } WkCl:=ValueWhen(1,WkEnd,C); WkCl:=ValueWhen(1,WkCl0,WkCl); { Reduce periodicity if insufficient periods } pds:=If(pds>Cum(WkEnd),Cum(WkEnd),pds); { Weekly EMA } factor:=2/(pds+1); WkEma:=PREV*If(WkEnd,1-factor,1)+If(WkEnd,WkCl*factor,0); { Vertical shift } WkEma:=WkEma*shift; { Plot on price chart } WkEma
mstt  
#2 Posted : Saturday, December 26, 2009 9:42:54 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi tsenks

The difference comes about because Jose's code uses a different seeding method from that used by the MetaStock EMA Mov() function (seeding determines how the first 1-N bars are calculated). My Multi-Frame formulas are different again. What's more inportant than the seeding is that you understand that an EMA requires around 5 times the number of bars stipulated by the periods paramater to generate a reasonably accurate value. Put another way, the first 25 bars of a 5-period EMA should not be relied upon anyway. A classic examle of this is seen when a 5-period EMA value generated by a "Minimum Periods" exploration is compared with the same EMA plotted on any normal chart.

Roy

tsenks  
#3 Posted : Sunday, December 27, 2009 2:07:20 AM(UTC)
tsenks

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/8/2008(UTC)
Posts: 3

Hi Roy,

Do you have any ideas how to amend the formula, so that the value calculated using Jose's Weekly EMA can be the same with/close to MetaStock EMA Mov() ?

I think, may be? this can be done because Weekly SMA value calculated using Jose's Weekly SMA formula are the same with the value calculated using MetaStock SMA Mov().

Thanks

Tsen

mstt  
#4 Posted : Sunday, December 27, 2009 2:50:24 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi Tsen

A weekly SMA is a much simpler proposition than a weekly EMA. I already have a weekly EMA that matches the EMA Mov() function on weekly data but generating the same value after switching the data back to daily, while it probably can be done, is simply not worth the effort. There are two main reasons why this is so, and one reason or the other will always frustrate one's best efforts.

When PREV is used to calculate an EMA the first data bar cannot return a valid result (on bar one PREV cannot look back and retrieve a result from a non-existant bar zero), so your EMA calculation is already in trouble. Ideally an EMA is seeded with the relevant price on bar one and then uses the "2/(pds+1)" ratio on every subsequent bar to update the EMA value. MFL does not make it easy to access that seed value so seeding for PREV will most likely be zero or the value of the second or maybe third bar.

Even when a non-PREV method is used to calculate a weekly EMA on daily data there's still the problem of accurately picking up or recognising the first end-of-week, usually because of inherent N/A results in the end-of-week calculation. This leaves one with virtually the same dilemma as above - the proper seed value cannot be accessed.

The IsDefined() and IsUndefined() functions were introdced wih MetaStock 7.0 and these functions should have provided a solution to most of the N/A issues that beset MetaStock. For whatever reason, however, Equis chose to restrict the fully functional DLL-based versions of these functions to expensive add-ons and leave regular MS users with the less than helpful functions we're still stuck with. The Equis Forum DLL can be used to eliminate most N/A results but it's a tool that can cause unexpected results when used inappropriately.

The short answer to your question is, yes I do have ideas on how to make weekly EMA's match the EMA Mov() function from the N'th bar, but I'm not about to spend a lot of time following through on those ideas. In my veiw it's an academic exercise that has no practical value other than to educate users about the very issues I've briefly mentioned. If you feel you really need a solution to your problem then I suggest that you contact Jose, Wabbit, myself or some other competent MFL programmer for a quotation.

Roy

mstt  
#5 Posted : Sunday, December 27, 2009 4:36:38 PM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

H Tsen

After making my previous post I realized that I had already pulled all the ideas together to do what you want. The following Multi-Frame formula plots EMA weekly through 1, 2, 3, 4, 6 and 12 mothly periodicities. The Forum DLL is used to remove unwanted N/A results and calculate the selected higher periodicity EMA values. Price options include Open, High, Low, Close, Mean Price, Typical Price and Weighted Close. A quick check shows that it is accurate to at least 2 decimal places on 109 years of DJIA daily data and that on weekly data it starts with the same value as the EMA Mov() function. However, there's still a possibility that the initially seeding might fail, so beware.

The commercial version of this formula uses a different DLL for calculating the EMA and is accurate to at least 4 decimal places. That DLL can also be used to accurately reproduce the likes of Kaufman's AMA for higher periodicities, something that the Forum DLL can't quite manage.

Roy

{Multi-Frame D EMA Special}
{Exponential Moving Average}
{Requires Equis Forum DLL}
{Roy Larsen, 2009, 28/12/09}

{User settings}
N:=Input("Multi-Frame D EMA, Periods",1,99,5);
J:=Input("Months per Frame, 0=Weekly",0,12,0);
J:=If(J<5 OR Mod(J,3)=0 AND (J<>9),Int(J),-1);
U:=Input("Price, 1=O 2=H 3=L 4=C 5=MP 6=WC 7=Typical",1,7,4);
G:=Input("End-of-week Offset in Days",0,6,0);
Q:=Input("Mode, 0=Static 1=Dynamic 2=Delayed",0,2,1);
{Update on last bar, new bar or new frame}

{Timing signals}
{Day counter by metastocktools.com}
D:=DayOfWeek();M:=Month();Y:=Year();
Z:=Cum(1);F:=Rnd(Life(291231));
M:=If(J=0,ValueWhen(1,Z=1,F+D-G-1)-F,
(Y-ValueWhen(1,Z=1,Y))*12+M);
I:=If(J=0,Int(M/7),Int((M-1)/Max(1,J)));
I:=I>ValueWhen(2,1,I);
G:=LastValue(J<0 OR Lowest(Sum(I>0,5))=5);
I:=ExtFml("Forum.Sum",I,1);M:=G+I;
F:=ExtFml("Forum.Sum",Ref(I,1),1)*(M=0)*(Z>1)+G;
B:=LastValue(Z);A:=B-1=Z;B:=B=Z;
F:=F+B*(Q=0)*(J=0)*(D=5);
J:=If(F,1,(Alert(F,2)=0)*M*2*(Z>1));
J:=If(A+LastValue(J)>2 OR B+(Q=1)=2,1,J);
J:=If(G,1,If(Q=2,M*2,J));

{Frame prices}
Q:=ValueWhen(1,M+(Z=1),O);
B:=HighestSince(1,M+(Z=1),H);
Y:=LowestSince(1,M+(Z=1),L);
Q:=ValueWhen(1,J,If(J=1,Q,ValueWhen(2-G,1,Q))); {O}
B:=ValueWhen(1,J,If(J=1,B,ValueWhen(2-G,1,B))); {H}
Y:=ValueWhen(1,J,If(J=1,Y,ValueWhen(2-G,1,Y))); {L}
K:=ValueWhen(1,J,If(J=1,C,ValueWhen(2-G,1,C))); {C}
A:=(B+Y)/2; D:=(B+Y+2*K)/4; I:=(B+Y+K)/3;
K:=If(U=1,Q,If(U=2,B,If(U=3,Y,If(U=5,A,If(U=6,D,If(U=7,I,K))))));

{EMA}
I:=Cum(J>0); R:=If(J,N,Pwr(10,10));
ValueWhen(1,I>=N,ExtFml("Forum.Mov",K,R,E));

tsenks  
#6 Posted : Monday, December 28, 2009 6:36:45 AM(UTC)
tsenks

Rank: Newbie

Groups: Registered, Registered Users
Joined: 5/8/2008(UTC)
Posts: 3

Hi Roy,

Thanks ......... !

Its worked, the value calculated using your's Weekly EMA formula is the same with the value calculated by Metastock weekly EMA.

Thanks from your help!!!

By the way, Roy are you also the person that developing the metastock formula for the following web site ?

http://www.metastocktips.co.nz

Tsen

mstt  
#7 Posted : Monday, December 28, 2009 11:30:14 AM(UTC)
mstt

Rank: Advanced Member

Groups: Registered, Registered Users, Subscribers
Joined: 7/25/2005(UTC)
Posts: 1,042

Was thanked: 57 time(s) in 54 post(s)

Hi Tsen

I'm relieved to hear that it works for you. Yes, www.metastocktips.co.nz is my web site, and to answer your next question, yes, I know that the weekly and multi-frame formula pages links don't work any longer.

Roy

Users browsing this topic
Guest (Hidden)
Similar Topics
Weekly EMA, SMA, MACD, MACDH (Advanced Coding Techniques)
by Jose 9/26/2005 11:42:36 PM(UTC)
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.