Announcement

Collapse
No announcement yet.

Corrected Moving Average

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Corrected Moving Average

    The following Code of a moving average is published in a bulletin board by Andreas Uhl.

    I canĀ“t "read" a Metastock Code. Is it possible to code this in eSignal efs?


    Thank You!

    Helmut





    {Corrected Average (CA), A.Uhl, Oct. 25, 2005}

    vars:
    CA(0),SA(0),n(0),
    v1(0),v2(0),K(0);

    inputs:
    Price(Close),length(35);

    if CurrentBar=1 then
    CA=Price
    else
    begin

    if CurrentBar<length then
    n=CurrentBar
    else
    n=length;

    SA=Average(Price,n);
    v1=Square(StdDev(Price,n));
    v2=Square(CA[1]-SA);

    if v2<v1 then
    K=0
    else
    K=1-v1/v2;
    CA=CA[1]+K*(SA-CA[1]);
    end;

    Plot1(CA,"Corrected Average",Red,White,3);
    Plot2(SA,"Simple Average",Blue,White,1);
    Attached Files

  • #2
    Helmut
    As I think the study could be of interest to this community I went ahead and coded the indicator. You can find a basic version of the study here
    Alex

    Comment


    • #3
      Alex,

      great, thank you for this!

      Helmut

      Comment


      • #4
        A. Uhl published another MA- concept .

        Maybe this study could also be of interest to this community.

        Mr. Uhl was so kind to give me C++ Code.


        // local function AEPMA
        // copyright (c) A. Uhl 2005
        // zero lag if input linear, that is line with constant slope

        void AEPMAfun (const MSXDataInfoRec *a_psSrc, MSXDataInfoRec *a_psRslt)
        {
        int l_iIndex = a_psSrc->iFirstValid;
        int l_iMaxIndex = a_psSrc->iLastValid;

        float l_fSLOPE = 0 ; // slope of regression function
        float l_fTAU = 1; // new time value
        float l_fTAU0; // last time value
        float l_fDTAU; // variable time step
        float l_fDT = 1; // constant data time step (sample rate)
        float l_fR0; // prediction one time step ahead
        float l_fRX; // data regression
        float l_fWAVG; // weighted average
        float l_fYOLD; // last filter value
        float l_fYNEW; // new filter value
        float l_fQREST; // sum of squares of residuals
        float l_fQTOT; // sum of squares of residuals
        float l_fG; // gain factor
        float l_fSX0; // sum of x0
        float l_fSX0S; // sum of x0 squared
        float l_fK = 0; // measure of determinism

        int l_iN; // period (variable)
        int X; // x-axis in regression


        a_psRslt->pfValue[l_iIndex] = a_psSrc->pfValue[l_iIndex];

        l_iIndex += 1;

        while (l_iIndex <= l_iMaxIndex) {

        l_fTAU0 = l_fTAU + l_fDT;
        l_fYOLD = a_psRslt->pfValue[l_iIndex-1];
        l_fR0 = l_fYOLD + l_fK*l_fSLOPE*l_fTAU0;
        l_iN = int(floor(l_fTAU0));

        l_fSX0 = 0;
        l_fSX0S = 0;
        for(X=0; X<=l_iN; X++) {
        l_fSX0 += (l_fTAU0 - X);
        l_fSX0S += pow(l_fTAU0 - X,2);
        }

        l_fWAVG=0;
        for(X=0; X<=l_iN; X++) {
        if(X==0) l_fRX = l_fR0;
        else l_fRX = a_psSrc->pfValue[l_iIndex-X+1];
        l_fWAVG += (l_fTAU0 - X) * l_fRX;
        }

        l_fWAVG = l_fWAVG / l_fSX0;
        l_fSLOPE = (l_fWAVG - l_fYOLD) * l_fSX0 / l_fSX0S;

        l_fQREST = 0;
        l_fQTOT = 0;
        for(X=0; X<=l_iN; X++) {
        if(X==0) l_fRX = l_fR0;
        else l_fRX = a_psSrc->pfValue[l_iIndex-X+1];
        l_fQREST += pow(l_fRX - (l_fSLOPE*(l_fTAU0-X)+l_fYOLD),2);
        l_fQTOT += pow(l_fRX - l_fYOLD,2);
        }

        l_fK = 1 - l_fQREST/l_fQTOT;
        l_fDTAU = l_fK*(l_fTAU0-1);
        l_fG = l_fDTAU*l_fSX0/l_fSX0S;
        l_fTAU = l_fTAU0 - l_fDTAU;

        // AEPMA
        l_fYNEW = l_fYOLD + l_fG*(l_fWAVG-l_fYOLD);
        a_psRslt->pfValue[l_iIndex] = l_fYNEW;

        l_iIndex++;
        }
        Attached Files

        Comment

        Working...
        X