Welcome Guest! To enable all features please try to register or login.
 Previous Topic Next Topic
 Glyn24 #1 Posted : Wednesday, July 20, 2005 9:36:59 PM(UTC) Rank: NewbieGroups: Registered, Registered Users, SubscribersJoined: 4/22/2005(UTC)Posts: 4 Can anyone help me understand how Metastock calculates the EMA?Say I have the following time series using a 4 periods EMA Close Date Exponential Moving Average12.987 05/08/2003 12.909 06/08/2003 13.055 07/08/2003 13.438 08/08/2003 13.172513.787 11/08/2003 13.418313.764 12/08/2003 13.556613.708 13/08/2003 13.6171In order to calculate the first average (13.1725) Metastock should use the following formula:EMA(at date 08/08/2003)=13.438*0.4+EMA_1 (at date 07/08/2003)*0.6How is the first EMA_1 calculated? :?: Thank you
 wabbit #2 Posted : Wednesday, July 20, 2005 11:24:18 PM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) To answer this one we need to know how MS does its calculations....Patrick was kind enough to post the actual source code used by MS to do MA calculations, here : http://forum.equis.com/viewtopic.php?t=615&start=0BUT its missing the bottom portion of the code! As Murphy would have it - it happens to be the Exponential MA part too! It finishes thus:--8<-----------------------------void ExponentialMovingAverage (const MSXDataInfoRec *a_psSrc,MSXDataInfoRec *a_psRslt,int a_iPeriod){ int l_iIndex = a_psSrc->iFirstValid; int l_iMaxIndex = a_psSrc->iLastValid; double l_lfSum = 0.0; double l_l--8<-----------------------------then what!!! Perhaps if we ask Patrick really nicely then he might post the rest for us...and we can finish answering the question!wabbit :D( Patrick: With any luck my MSDK should be here today ! Yippee ) \\:D/[update:]DHL this morning delivered my copy of 9.1 pro, not msdk?
 Patrick #3 Posted : Saturday, July 23, 2005 3:26:13 PM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, SubscribersJoined: 9/8/2004(UTC)Posts: 2,266 [code:1:4222a6f69c]void ExponentialMovingAverage (const MSXDataInfoRec *a_psSrc, MSXDataInfoRec *a_psRslt, int a_iPeriod){ int l_iIndex = a_psSrc->iFirstValid; int l_iMaxIndex = a_psSrc->iLastValid; double l_lfSum = 0.0; double l_lfDivisor; double l_lfExponent; int i; if (a_iPeriod > 0 && ((l_iIndex + a_iPeriod - 1) <= l_iMaxIndex)) { l_lfExponent = ForceFloatRange(2.0 / (a_iPeriod+1)); l_lfDivisor = double(a_iPeriod); // start with simple moving average; for (i=0; ipfValue[l_iIndex+i]; l_lfSum = ForceFloatRange(l_lfSum); a_psRslt->pfValue[l_iIndex + a_iPeriod - 1] = float(ForceFloatRange(l_lfSum / l_lfDivisor)); l_iIndex += a_iPeriod; while (l_iIndex <= l_iMaxIndex) { a_psRslt->pfValue[l_iIndex] = float(ForceFloatRange((a_psSrc->pfValue[l_iIndex] - a_psRslt->pfValue[l_iIndex-1]) * l_lfExponent + a_psRslt->pfValue[l_iIndex-1])); l_iIndex++; } a_psRslt->iFirstValid = a_psSrc->iFirstValid + a_iPeriod - 1; a_psRslt->iLastValid = l_iMaxIndex; } else { a_psRslt->iFirstValid = 0; a_psRslt->iLastValid = -1; }}[/code:1:4222a6f69c]Wabbit I will talk to Marilyn about your DevKit, my guess is that the products were not sent together or you receive the wrong thing :roll:Now regarding the code, I doubt that it is the one used in MetaStock.That is just an example of how to use the DevKit.Patrick :mrgreen:
 wabbit #4 Posted : Saturday, July 23, 2005 3:34:06 PM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) Thanks for the code Patrick...and to answer Glyn's question.... dunno!. Patrick was right, the code posted isnt the code used by MS to form the EMA. The code first computes an SMA until sufficient data items are available to compute the EMA. This isnt the case with the numbers in the initial problem.I guess we will have to look a little further into this one!wabbit :D
 wabbit #5 Posted : Sunday, July 24, 2005 2:08:30 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) Quote:In order to calculate the first average (13.1725) Metastock should use the following formula:EMA(at date 08/08/2003)=13.438*0.4+EMA_1 (at date 07/08/2003)*0.6I concur with your formula so far, so what are we trying to discover?The EMA on 08/08/2003is 13.1725 ==> which means the EMA_1 (at date 07/08/2003) = 12.9955How did we end up with number from the values:12.987 05/08/200312.909 06/08/200313.055 07/08/2003 12.9955 ???Lets start by reverse engineering the EMA:12.9955 = (0.4 * 13.055) + EMA (at date 06/08/2003) * 0.6EMA (at date 06/08/2003) = 12.955812.9558 = (0.4 * 12.909) + EMA (at date 05/08/2003) * 0.6EMA (at date 05/08/2003) = 12.9870...PASSWorking forwards again... ```Close Date EMA12.987 05/08/2003 12.987 (just the value on day 1)12.909 06/08/2003 0.4*12.909 + 0.6*12.987 = 12.955813.055 07/08/2003 0.4*13.055 + 0.6*12.9558 = 12.9954813.438 08/08/2003 0.4*13.438 + 0.6*12.99548 = 13.172488 ```Done....wabbit :D P.S. Be careful rounding numbers too!
 wabbit #6 Posted : Sunday, July 24, 2005 2:22:19 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) This answer does raise another question:Why does MS not plot the EMA in the first 'x' bars? It has to calculate the values, so why aren't these available to be plotted?What were the code cutters thinking? smoking?wabbit :D
 hayseed #7 Posted : Sunday, July 24, 2005 2:29:41 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, SubscribersJoined: 3/7/2005(UTC)Posts: 1,345 hey wabbit.... dangit, i was half way through writing that out when i noticed you beat me to it.... its a recursive formula.....btw, i was suposed to have receive a ms pro 9.1 cd this week and got some sort of msdk outfit with postage due..... what ya reckon thats about.....h
 wabbit #8 Posted : Sunday, July 24, 2005 2:41:17 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) wanna swap?
 Marilyn #9 Posted : Sunday, July 24, 2005 3:48:12 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, SubscribersJoined: 9/10/2004(UTC)Posts: 863 Far out! I promise that I raised the orders properly but it looks like our mail house is just sending stuff out left right and center again! I will sort it all out on Monday... promise!Sorry :( M
 wabbit #10 Posted : Sunday, July 24, 2005 3:56:26 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered Users, Subscribers, Unverified UsersJoined: 10/28/2004(UTC)Posts: 3,041Was thanked: 1 time(s) in 1 post(s) s'alright... "I don't suffer from insanity - I enjoy it"(cannot remember who to credit with this gem, I think it was steve wright?)wabbit :D
 Jose #11 Posted : Sunday, July 24, 2005 9:44:03 AM(UTC) Rank: Advanced MemberGroups: Registered, Registered UsersJoined: 1/19/2005(UTC)Posts: 1,065Was thanked: 1 time(s) in 1 post(s) wabbit wrote:Why does MS not plot the EMA in the first 'x' bars? It has to calculate the values, so why aren't these available to be plotted?wabbit :DThe only way around the null plot on the 'x' bars of any indicator, is to shorten x periodicity on the initial low/invalid bar count.Here is an example:MetaStock -> Tools -> Indicator Builder -> New -> Copy and paste formula below.[code:1:90aaf480aa]===EMA===---8<---------------------------{ Exponential Moving Average v2.2 }{ EMA periodicity shortens on low bar count }{ Copyright (c)2003-2005 Jose Silva }{ http://www.metastocktools.com }{ User inputs }pds:=Input("EMA periods",1,2520,21);x:=Input("use Open=1 High=2 Low=3 Close=4 WClose=5 P=6",1,6,4);shift:=Input("EMA vertical shift %", -100,100,0)/100+1;plot:=Input("[1]EMA, [2]Crossover signals", 1,2,1);{ Choose data array }x:=If(x=1,O,If(x=2,H,If(x=3,L,If(x=5,WC(),If(x=6,P,C)))));{ Shorten periodicity on initial low bar count }pds:=If(pds>Cum(IsDefined(x)), Cum(IsDefined(x)),pds);{ Exponential Mov Avg }Ema:=x*2/(pds+1)+PREV*(1-2/(pds+1));{ EMA vertical shift }Ema:=Ema*shift;{ EMA crossover signals }signals:=Cross(x,Ema)-Cross(Ema,x);{ Plot EMA on price chart }If(plot=2,signals,Ema)---8<---------------------------[/code:1:90aaf480aa]jose '-)http://www.metastocktools.com
 Users browsing this topic
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.