in Search

EMA formula

Last post 07-24-2005, 9:44 by Jose. 10 replies.
Sort Posts: Previous Next
  •  07-20-2005, 21:36 4902

    EMA formula

    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 Average

    12.987 05/08/2003
    12.909 06/08/2003
    13.055 07/08/2003
    13.438 08/08/2003 13.1725
    13.787 11/08/2003 13.4183
    13.764 12/08/2003 13.5566
    13.708 13/08/2003 13.6171

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

    How is the first EMA_1 calculated? :?:

    Thank you
  •  07-20-2005, 23:24 4905 in reply to 4902

    EMA - Patrick needed here!

    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=0

    BUT 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?
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-23-2005, 15:26 5032 in reply to 4902

    Re: EMA - Patrick needed here!

  •  07-23-2005, 15:34 5033 in reply to 4902

    Re: EMA - Patrick needed here!

    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
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-24-2005, 2:08 5047 in reply to 4902

    Re: EMA - Patrick needed here!

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



    I 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.9955

    How did we end up with number from the values:
    12.987 05/08/2003
    12.909 06/08/2003
    13.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.6
    EMA (at date 06/08/2003) = 12.9558

    12.9558 = (0.4 * 12.909) + EMA (at date 05/08/2003) * 0.6
    EMA (at date 05/08/2003) = 12.9870

    ...PASS


    Working forwards again...

    Close Date EMA
    12.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.9558
    13.055 07/08/2003 0.4*13.055 + 0.6*12.9558 = 12.99548
    13.438 08/08/2003 0.4*13.438 + 0.6*12.99548 = 13.172488

    Done....

    wabbit :D

    P.S. Be careful rounding numbers too!
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-24-2005, 2:22 5048 in reply to 4902

    Re: EMA - Patrick needed here!

    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
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-24-2005, 2:29 5050 in reply to 4902

    postage due

    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
  •  07-24-2005, 2:41 5052 in reply to 4902

    Re: postage due

    wanna swap?
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-24-2005, 3:48 5059 in reply to 4902

    Re: postage due

    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
    "It takes no more time to see the good side of life than it takes to see the bad."
    Jimmy Buffett
  •  07-24-2005, 3:56 5061 in reply to 4902

    Re: postage due

    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
    Please update your thread status:

    FREE Equis Formula Primer : http://forum.equis.com/files/19673/download.aspx
    To post code, *code* your code */code*
    To post links, [URL] URL goes here [/URL]
    For custom MetaStock and DLL programming : http://www.wabbit.com.au
    Contact via SkyPE
  •  07-24-2005, 9:44 5086 in reply to 4902

    Re:

    wabbit:
    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 :D


    The 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:

    ===
    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<---------------------------



    jose '-)
    http://www.metastocktools.com
    MetaStockTools.com
View as RSS news feed in XML