Announcement

Collapse
No announcement yet.

Modifying zeroLag_EC

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

  • Modifying zeroLag_EC

    Hi All

    I'm struggling to get to grips with EFS (my only programming knowledge comes from BASIC and a bit of EasyLanguage) and I'd be grateful if someone could help me with a simple modification to zeroLag_EC.efs.

    Quite simply, I'd like to plot a red point (circle, square, whatever) when zeroLag_EC > zeroLag_EMA and a green point when zeroLag__EC < zeroLag_EMA. Ideally on a sub chart.

    I can follow small sections of the code but my lack of ability is shown when I can't even see the part where the lines are actually plotted!

    Hope someone can help.

    Many thanks,
    Ian

    PHP Code:
    /*********************************
    Provided By:  
        Interactive Data Corporation (Copyright © 2010) 
        All rights reserved. This sample eSignal Formula Script (EFS)
        is for educational purposes only. Interactive Data Corporation
        reserves the right to modify and overwrite this EFS file with 
        each new release. 
        
    Description:        
        Zero Lag (Well, Almost) Indicator by John Ehlers and Ric Way
        
    Version:            1.0  13/09/2010
     
    Formula Parameters:                     Default:
        Length                              20
        Gain Limit                          50
        
    Notes:
        The related article is copyrighted material. If you are not
        a subscriber of Stocks & Commodities, please visit [url]www.traders.com[/url].
        
    **********************************/

    var fpArray = new Array();
    var 
    bVersion null;

    function 
    preMain()
    {
        
    setPriceStudy(true);
        
    setStudyTitle("zeroLag_EC");
        
    setCursorLabelName("zeroLag_EMA"0);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarThickness(20);
        
    setPlotType(PLOTTYPE_LINE0);
        
    setCursorLabelName("zeroLag_EC"1);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarThickness(21);
        
    setPlotType(PLOTTYPE_LINE1);
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("gLength"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setName("Length");
            
    setLowerLimit(1);        
            
    setDefault(20);
        }
        
    fpArray[x] = new FunctionParameter("gGainLimit"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setName("Gain Limit");
            
    setLowerLimit(1);        
            
    setDefault(50);
        }
    }

    var 
    bMainInit false
    var 
    xEMA null;
    var 
    xEC null;
    var 
    nAlpha 0;

    function 
    main(gLengthgGainLimit)
    {
        var 
    nGain 0;
        var 
    nBestGain 0;
        var 
    nError 0;
        var 
    nLeastError 1000000;
     
        if (
    bVersion == nullbVersion verify();
        if (
    bVersion == false) return;   
        var 
    nBarState getBarState();
        if (
    nBarState == BARSTATE_ALLBARS) {
            if (
    gLength == nullgLength 20;
            if (
    gGainLimit == nullgGainLimit 50;
            
    nAlpha 2/(gLength+1);
        }    
        if (!
    bMainInit)
        {
            
    xEMA efsInternal("calcEMA",nAlpha);
            
    xEC efsInternal("calcEC",nAlpha,gGainLimit,xEMA);
        }

        var 
    nEMA xEMA.getValue(0);
        var 
    nEC xEC.getValue(0);
        if( 
    nEMA == null || nEC == null) return ;

        return new Array( 
    nEMAnEC) ;
    }

    function 
    calcEMA(nAlpha)
    {
        if (
    getCurrentBarCount()==1) var nRefEMA close(0
        else  var 
    nRefEMA ref(-1);
        var 
    vEMA nAlpha*close(0)+(1-nAlpha)*nRefEMA;
        return 
    vEMA;
    }

    var 
    nBestGain 0;

    function 
    calcEC(nAlpha,nGaintLimitxEMA)
    {
        var 
    nLeastError 1000000;
        if (
    getCurrentBarCount()==1) var nRefEC close(0
        else var 
    nRefEC ref(-1);

        var 
    nClose close(0);
        var 
    vEMA xEMA.getValue(0);
        var 
    nCount 0;
        var 
    nGain 0;
        var 
    nError 0;
        var 
    vEC ;
        for (
    nCount=-nGaintLimitnCount<=nGaintLimitnCount++)
        {
            
    nGain nCount/10;
            
    vEC nAlpha*(vEMA+nGain*(nClose-nRefEC))+(1-nAlpha)*nRefEC;
            
    nError nClose vEC;
            if (
    Math.abs(nError)<nLeastError)
            {
                
    nLeastError Math.abs(nError);
                
    nBestGain nGain;
            }
        }
        
    vEC nAlpha * (vEMA nBestGain*(nClose nRefEC))+(1-nAlpha)*nRefEC;
      
        return 
    vEC;
    }

    function 
    verify() {
        var 
    false;
        if (
    getBuildNumber() < 779) {
            
    drawTextAbsolute(535"This study requires version 8.0 or later."
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"error");
            
    drawTextAbsolute(520"Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp"
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"upgrade");
            return 
    b;
        } else {
            
    true;
        }
        return 
    b;


  • #2
    Ian
    The plots are determined by this line of code in the main function



    The variables used in the return statement are assigned the values in the lines just above it and a null check is performed on those variables
    To do what you want you need to create two conditional statements to determine those two states and then following each statement execute a command to draw the shape you want. See the example shown in the following screenshot.



    Insert the lines just after the null check on the variables and replace your_shape and your_color with the desired shape and color. See this article in the EFS KnowledgeBase for a list of available shapes and also for alternative locations [within the same pane as an efs cannot plot, draw, etc in a pane other than the one in which it is running]
    If – as you indicate – you are unfamiliar with programming in EFS [despite having used it for around a decade] then you may find it to your benefit to review the JavaScript for EFS video series and the Core JavaScript Reference Guide. Those will provide you with a thorough introduction to programming in JavaScript which is at the foundation of EFS. Then go through the EFS KnowledgeBase and study the Help Guides and Tutorials which will provide you with the specifics of EFS.
    Alex


    Originally posted by iantrader View Post
    Hi All

    I'm struggling to get to grips with EFS (my only programming knowledge comes from BASIC and a bit of EasyLanguage) and I'd be grateful if someone could help me with a simple modification to zeroLag_EC.efs.

    Quite simply, I'd like to plot a red point (circle, square, whatever) when zeroLag_EC > zeroLag_EMA and a green point when zeroLag__EC < zeroLag_EMA. Ideally on a sub chart.

    I can follow small sections of the code but my lack of ability is shown when I can't even see the part where the lines are actually plotted!

    Hope someone can help.

    Many thanks,
    Ian

    PHP Code:
    /*********************************
    Provided By:  
        Interactive Data Corporation (Copyright © 2010) 
        All rights reserved. This sample eSignal Formula Script (EFS)
        is for educational purposes only. Interactive Data Corporation
        reserves the right to modify and overwrite this EFS file with 
        each new release. 
        
    Description:        
        Zero Lag (Well, Almost) Indicator by John Ehlers and Ric Way
        
    Version:            1.0  13/09/2010

    Formula Parameters:                     Default:
        Length                              20
        Gain Limit                          50
        
    Notes:
        The related article is copyrighted material. If you are not
        a subscriber of Stocks & Commodities, please visit [url]www.traders.com[/url].
        
    **********************************/

    var fpArray = new Array();
    var 
    bVersion null;

    function 
    preMain()
    {
        
    setPriceStudy(true);
        
    setStudyTitle("zeroLag_EC");
        
    setCursorLabelName("zeroLag_EMA"0);
        
    setDefaultBarStyle(PS_SOLID0);
        
    setDefaultBarFgColor(Color.red0);
        
    setDefaultBarThickness(20);
        
    setPlotType(PLOTTYPE_LINE0);
        
    setCursorLabelName("zeroLag_EC"1);
        
    setDefaultBarStyle(PS_SOLID1);
        
    setDefaultBarFgColor(Color.blue1);
        
    setDefaultBarThickness(21);
        
    setPlotType(PLOTTYPE_LINE1);
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("gLength"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setName("Length");
            
    setLowerLimit(1);        
            
    setDefault(20);
        }
        
    fpArray[x] = new FunctionParameter("gGainLimit"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setName("Gain Limit");
            
    setLowerLimit(1);        
            
    setDefault(50);
        }
    }

    var 
    bMainInit false
    var 
    xEMA null;
    var 
    xEC null;
    var 
    nAlpha 0;

    function 
    main(gLengthgGainLimit)
    {
        var 
    nGain 0;
        var 
    nBestGain 0;
        var 
    nError 0;
       var 
    nLeastError 1000000;

        if (
    bVersion == nullbVersion verify();
        if (
    bVersion == false) return;   
        var 
    nBarState getBarState();
        if (
    nBarState == BARSTATE_ALLBARS) {
            if (
    gLength == nullgLength 20;
            if (
    gGainLimit == nullgGainLimit 50;
            
    nAlpha 2/(gLength+1);
        }    
        if (!
    bMainInit)
        {
            
    xEMA efsInternal("calcEMA",nAlpha);
            
    xEC efsInternal("calcEC",nAlpha,gGainLimit,xEMA);
        }

        var 
    nEMA xEMA.getValue(0);
        var 
    nEC xEC.getValue(0);
        if( 
    nEMA == null || nEC == null) return ;

        return new Array( 
    nEMAnEC) ;
    }

    function 
    calcEMA(nAlpha)
    {
        if (
    getCurrentBarCount()==1) var nRefEMA close(0
        else  var 
    nRefEMA ref(-1);
        var 
    vEMA nAlpha*close(0)+(1-nAlpha)*nRefEMA;
        return 
    vEMA;
    }

    var 
    nBestGain 0;

    function 
    calcEC(nAlpha,nGaintLimitxEMA)
    {
        var 
    nLeastError 1000000;
        if (
    getCurrentBarCount()==1) var nRefEC close(0
        else var 
    nRefEC ref(-1);

        var 
    nClose close(0);
        var 
    vEMA xEMA.getValue(0);
        var 
    nCount 0;
        var 
    nGain 0;
        var 
    nError 0;
        var 
    vEC ;
        for (
    nCount=-nGaintLimitnCount<=nGaintLimitnCount++)
        {
            
    nGain nCount/10;
            
    vEC nAlpha*(vEMA+nGain*(nClose-nRefEC))+(1-nAlpha)*nRefEC;
            
    nError nClose vEC;
            if (
    Math.abs(nError)<nLeastError)
            {
                
    nLeastError Math.abs(nError);
                
    nBestGain nGain;
            }
        }
        
    vEC nAlpha * (vEMA nBestGain*(nClose nRefEC))+(1-nAlpha)*nRefEC;
      
        return 
    vEC;
    }

    function 
    verify() {
        var 
    false;
        if (
    getBuildNumber() < 779) {
            
    drawTextAbsolute(535"This study requires version 8.0 or later."
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"error");
            
    drawTextAbsolute(520"Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp"
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"upgrade");
            return 
    b;
        } else {
            
    true;
        }
        return 
    b;

    Comment


    • #3
      Hi Alex,

      Brilliant! Many thanks.

      I'd also like to add a different plot if the two values are equal. I tried this (plus many variations on it):

      if ( nEC < nEMA) drawShape(Shape.DOWNARROW, BottomRow1, Color.red) ;
      else if (nEC > nEMA) drawShape(Shape.UPARROW, BottomRow1, Color.green) ;
      else if (nEC = nEMA) drawShape(Shape.DIAMOND, BottomRow1, Color.yellow ;

      but when the values are equal, it simply plots the green arrow.

      I've tried to make sense of the section on using the if... else statement, especially the part about using curly braces and I've tried putting them in several places but can't get it to plot correctly so I probably haven't understood it

      I'd be most grateful if you could help again.

      Comment


      • #4
        I've tried adding the curly braces and got this far, but it's still not working properly.

        I suspect it may be something more than just syntax...

        if ( nEC < nEMA)
        {
        drawShape(Shape.DOWNARROW, BottomRow1, Color.red);
        }
        else
        {
        if (nEC > nEMA)
        {
        drawShape(Shape.UPARROW, BottomRow1, Color.green);
        }
        else
        {
        drawShape(Shape.DIAMOND, BottomRow1, Color.yellow);
        }
        }

        Comment


        • #5
          Ian
          I suggest you review section 2.4 [and the examples provided therein] of the Beginner Tutorial 2: Commonly Used Core JavaScript which is in the Help Guides and Tutorials folder of the EFS KnowledgeBase and that covers the if(), else if(), else conditional statements
          Also in the example you posted in your prior reply you are using the wrong operator for equality. See section 2.5 of the afore mentioned tutorial which explains the most common operators and specifically section 2.5.2 on equality operators
          Alex

          Comment


          • #6
            Hi Alex

            Thanks for the pointers. I'm watching the videos and I've read the sections you recommended, many thanks.

            This is my best shot at it:

            if ( nEC < nEMA)
            {
            drawShape(Shape.DOWNARROW, BottomRow1, Color.red);
            }
            else if (nEC > nEMA)
            {
            drawShape(Shape.UPARROW, BottomRow1, Color.green);
            }
            else if ( nEC == nEMA )
            {
            drawShape(Shape.DIAMOND, BottomRow1, Color.yellow);
            }

            Which still doesn't plot for the == condition.

            So I removed the others and tried this:

            if ( nEC == nEMA)
            {
            drawShape(Shape.DIAMOND, BottomRow1, Color.yellow);
            }

            And that still doesn't plot anything. In fact, when nEC and nEMA ARE equal (according to the Data Window), one of the other shapes plots so I'm wondering if the code is reading the variables correctly or reading them to x-number of decimal places beyond the 5-digit precision of the data.

            Very much appreciate any help.

            Ian

            Comment


            • #7
              Can anyone help with this, please? Give me a few pointers?

              Many thanks.

              Ian

              Comment


              • #8
                Ian
                This is due to the fact that in JavaScript numbers are binary floating point numbers which means that they do not always accurately represent decimal fractions. For example 0.1 + 0.2 returns 0.30000000000000004 instead of 0.3 and 0.05+0.35 returns 0.39999999999999997 instead of 0.40
                You can easily verify this at your end by using a debug statement in your script and checking for the values of your variables and you will likely see they are not equal [beyond the displayed decimals].
                One solution is to use the .toFixed() method of the number object to define the required number of decimals. Alternatively you can use the formatPriceNumber() function which sets the numbers to the same format used in plotting the symbol
                See the related articles in the EFS KnowledgeBase for information on these functions. Also search the forums where you will find a multitude of threads on this topic as it has been covered at length before
                Alex


                Originally posted by iantrader View Post
                Hi Alex

                Thanks for the pointers. I'm watching the videos and I've read the sections you recommended, many thanks.

                This is my best shot at it:

                if ( nEC < nEMA)
                {
                drawShape(Shape.DOWNARROW, BottomRow1, Color.red);
                }
                else if (nEC > nEMA)
                {
                drawShape(Shape.UPARROW, BottomRow1, Color.green);
                }
                else if ( nEC == nEMA )
                {
                drawShape(Shape.DIAMOND, BottomRow1, Color.yellow);
                }

                Which still doesn't plot for the == condition.

                So I removed the others and tried this:

                if ( nEC == nEMA)
                {
                drawShape(Shape.DIAMOND, BottomRow1, Color.yellow);
                }

                And that still doesn't plot anything. In fact, when nEC and nEMA ARE equal (according to the Data Window), one of the other shapes plots so I'm wondering if the code is reading the variables correctly or reading them to x-number of decimal places beyond the 5-digit precision of the data.

                Very much appreciate any help.

                Ian

                Comment


                • #9
                  Ian,

                  Just a note:

                  Consider stopping the creation on every tick of the "xEMA" and "xEC" series by adding "vMainInit=true;" to the code. As in:
                  PHP Code:
                      if (!bMainInit)
                      {
                          
                  xEMA efsInternal("calcEMA",nAlpha);
                          
                  xEC efsInternal("calcEC",nAlpha,gGainLimit,xEMA);
                          
                  bMainInit=true;
                      } 
                  Wayne
                  Last edited by waynecd; 07-10-2013, 09:05 PM.

                  Comment


                  • #10
                    Ah, my suspicions confirmed Many thanks Alex. I'm wading through the pages now.

                    Ian

                    Comment


                    • #11
                      Ian
                      You are welcome
                      Alex


                      Originally posted by iantrader View Post
                      Ah, my suspicions confirmed Many thanks Alex. I'm wading through the pages now.

                      Ian

                      Comment

                      Working...
                      X