in Search

Newbie code interpretation question

Last post 04-16-2009, 6:47 by wabbit. 5 replies.
Sort Posts: Previous Next
  •  04-15-2009, 16:58 29439

    Newbie code interpretation question

    Hi - I have spent a couple of hours trying to decipher what this code means. I think the first part calculates a 13 day exponential Moving Avg, but what about the bit in the Ref brackets? Does the -1 suggest 1 day previous? Any help would be great - thanks. Smile [:)]

     

    "Periods :=13;
    Mov(C ,Periods ,E ) - Ref(Mov(C ,Periods ,E ),-1)"

     

     

  •  04-15-2009, 17:46 29440 in reply to 29439

    Re: Newbie code interpretation question

    Yes it does.  It could also have been written:

    ROC(Mov(C ,Periods ,E ),1,$);


    It's all in the MS Users Manual.



    Hope this helps.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  04-16-2009, 5:07 29455 in reply to 29440

    Re: Newbie code interpretation question

    Thanks for that - much appreciated :)

    I am trying to replicate the 13 day EMA calculation in Excel for Citicorp.

    This is the series I am looking at:

     

    Day Date Close my SMA DAY 13 Metastocks
    1 25/04/2006 47.46
    2 26/04/2006 47.75
    3 27/04/2006 48.15
    4 28/04/2006 49.95
    5 01/05/2006 49.46
    6 02/05/2006 49.6
    7 03/05/2006 49.85
    8 04/05/2006 49.38
    9 05/05/2006 50.37
    10 08/05/2006 50.16
    11 09/05/2006 50.35
    12 10/05/2006 50.3
    13 11/05/2006 49.48 49.40 49.43

    To calculate the 13 day's SMA (and start the EMA process which is no problem), I summed the first 13 days of close prices and divided by 13, getting 49.40. When I run the chart in Metastock, the first value of the EMA is 49.43 - any idea as to why there's a difference? Surprise [:O]

  •  04-16-2009, 5:22 29456 in reply to 29455

    Re: Newbie code interpretation question

    I think you have some confusion in the different types of moving average.

    From the MS Help files:

    An exponential (or exponentially weighted) moving average is calculated by applying a percentage of today's closing price to yesterday's moving average value.
    For example, to calculate a 9% exponential moving average of IBM:  First, we would take today's closing price and multiply it by 9%.  We would then add this product to the value of yesterday's moving average multiplied by 91% (100% - 9% = 91%).

    A simple, or arithmetic, moving average is calculated by adding the closing price of the security for a number of time periods (e.g., 12 days) and then dividing this total by the number of time periods.  The result is the average price of the security over the time period.  For example, to calculate a 21-day moving average of IBM: First, we would add up IBM's closing prices for the preceding 21 days.  Next, we would divide that sum by 21; this would give us the average price of IBM over the preceding 21 days.  We would plot this average price on the chart.  The following day (tomorrow) we would do the same calculations:  add up the previous 21 days' closing prices, divide by 21, and plot the resulting figure on the chart.


    You can recreate a SIMPLE moving average in code, like (simplistically)

    prd:=13;
    data:=CLOSE;
    Sum(data,prd) / prd;


    You can recreate an EXPONENTIAL moving average in code, like

    prd:=13;
    data:=CLOSE;
    n:=2/(prd+1);
    If(Cum(1)=1,data,PREV*(1-n) + data*n);




    Hope this helps.

    wabbit Big Smile [:D]



    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

  •  04-16-2009, 5:38 29457 in reply to 29456

    Re: Newbie code interpretation question

    Thanks again wabbit - you're right - I am confused!

    I thought for the first period's exponential moving average (day 13 in the above example), the simple moving average would be used as the previous period's exponential moving average because no EMA exists at that stage. Then, from period 14 onward, the previous period's EMA was used? I can't understand where the 49.43 is coming from

     

  •  04-16-2009, 6:47 29459 in reply to 29457

    Re: Newbie code interpretation question

    The EMA only requires two bars of data: the first bar seeds the value and the second bar uses "some" of the first bar's moving average value and the current bar's data.  MS uses the "length" as the number of bars, so it doesn't return values whilst there is less than "periods" data, but it is still used in the computation (I wrote a post in the last couple of days on the topic).

    The SMA uses the average of the period data, so to take a 13 period average requires 13 periods of data.  It is possible to use some codes (or the forum.dll) to return an average of the data which is valid before "periods" bars of data (but that is another topic) and there is a lot of imformation on the forum on the subject.



    Hope this helps.

    wabbit Big Smile [:D]


    "The question of whether a computer can think is no more interesting than the question of whether a submarine can swim."
    Edsgar W. Dijkstra

     

    MS: 6.52 EOD, 7.x EOD, 8.0 PRO, 9.2 PRO w/QC, 10 PRO w/QC C, 11 PRO w/QC & MDK
    For custom MetaStock programming : http://www.wabbit.com.au
    My SkyPE status :
    My SkyPE account : wabbit.com.au

View as RSS news feed in XML