Announcement

Collapse
No announcement yet.

Problem with EMA...

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

  • Problem with EMA...

    Hi,

    I'm trying to write code for the Percent Price Oscillator...

    The algorithm for this code:

    Value1 = (EMA(Close, Length1) - EMA(Close, Length2)/EMA(Close, Length1); //PPO
    Value2 = EMA(Value1,Length); //Signal Line
    Value3 = (Value1 - Value2); //PPO-histogram
    Return (Value2, Value3);

    I have a problem with Value2... I don't know how to compute EMA on Value1...

    Could anyone help me?

    Thank you.

  • #2
    Nooze
    You can use the efs attached to this post as a blueprint to calculate the EMA of Value1 of your script.
    Alex

    Comment


    • #3
      Alex, thank you very much for the link!

      I just recently switched from TradeStation to eSignal and do not enough experience programming EFS.... I really need this indicator because it is my primary indicator, but I can't write it myself...

      I have compiled the code, but it is not working in my eSignal.

      Here is example of my code:



      PHP Code:
      /************************************************
      Alexis C. Montenegro © August 2004                        
      Use and/or modify this code freely. If you redistribute it
      please include this and/or any other comment blocks and a 
      description of any changes you make.                      
      *****************************************************/

      var vMA1 null;
      var 
      vMA2 null;

      function 
      preMain() {

          
      setStudyTitle("PPO");
          
      setShowCursorLabel(false);
          
      setDefaultBarStyle(PS_SOLID0);
          
      setDefaultBarFgColor(Color.yellow0);
          
      setDefaultBarThickness(10);
          
      setPlotType(PLOTTYPE_LINE0);
          
      //setPlotType(PLOTTYPE_HISTOGRAM); 
          //checkVersion(1,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/basicMAx2.efs"); //Comment this line out if modifying the code
              
          
      var fp1 = new FunctionParameter("MALength"FunctionParameter.NUMBER);
          
      fp1.setLowerLimit(1);        
          
      fp1.setDefault(9);
          
          var 
      fp2 = new FunctionParameter("MAType"FunctionParameter.STRING);
          
      fp2.setName("MAType");
          
      fp2.addOption("MAStudy.SIMPLE");
          
      fp2.addOption("MAStudy.EXPONENTIAL");
          
      fp2.addOption("MAStudy.WEIGHTED");
          
      fp2.addOption("MAStudy.VOLUMEWEIGHTED");
          
      fp2.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default

          
      var fp3 = new FunctionParameter("MA1Length"FunctionParameter.NUMBER);
          
      fp3.setLowerLimit(1);        
          
      fp3.setDefault(12); //Edit this value to set a new default
          
          
      var fp4 = new FunctionParameter("MA1Source"FunctionParameter.STRING);
          
      fp4.setName("MA1Source");
          
      fp4.addOption("Close");
          
      fp4.addOption("High");
          
      fp4.addOption("Low");
          
      fp4.addOption("Open");
          
      fp4.addOption("HL/2");
          
      fp4.addOption("HLC/3");
          
      fp4.addOption("OHLC/4");
          
      fp4.setDefault("Close"); //Edit this value to set a new default     
          
          
      var fp5 = new FunctionParameter("MA1Type"FunctionParameter.STRING);
          
      fp5.setName("MA1Type");
          
      fp5.addOption("MAStudy.SIMPLE");
          
      fp5.addOption("MAStudy.EXPONENTIAL");
          
      fp5.addOption("MAStudy.WEIGHTED");
          
      fp5.addOption("MAStudy.VOLUMEWEIGHTED");
          
      fp5.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
          
          
      var fp6 = new FunctionParameter("MA2Length"FunctionParameter.NUMBER);
          
      fp6.setLowerLimit(1);        
          
      fp6.setDefault(26); //Edit this value to set a new default
          
          
      var fp7 = new FunctionParameter("MA2Source"FunctionParameter.STRING);
          
      fp7.setName("MA1Source");
          
      fp7.addOption("Close");
          
      fp7.addOption("High");
          
      fp7.addOption("Low");
          
      fp7.addOption("Open");
          
      fp7.addOption("HL/2");
          
      fp7.addOption("HLC/3");
          
      fp7.addOption("OHLC/4");
          
      fp7.setDefault("Close"); //Edit this value to set a new default
          
          
      var fp8 = new FunctionParameter("MA2Type"FunctionParameter.STRING);
          
      fp8.setName("MA2Type");
          
      fp8.addOption("MAStudy.SIMPLE");
          
      fp8.addOption("MAStudy.EXPONENTIAL");
          
      fp8.addOption("MAStudy.WEIGHTED");
          
      fp8.addOption("MAStudy.VOLUMEWEIGHTED");
          
      fp8.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
              
             
      }

      var 
      bPrimed false
      var 
      vEMA null;
      var 
      vEMA1 null;   
      var 
      dPercent 0.0;
      var 
      vValue 0.0;
      var 
      aValue null;
      var 
      vAvg null;//used in the example provided

      function main(MALength,MAType,MA1Length,MA1Source,MA1Type,MA2Length,MA2Source,MA2Type) {

          var 
      i;
          var 
      vSum 0.0;
          
          
      //insert below the calculations required for the user defined variable (example provided)
          
      if (vMA1 == nullvMA1 = new MAStudy(MA1LengthMA1Source, eval(MA1Type));
          if (
      vMA2 == nullvMA2 = new MAStudy(MA2LengthMA2Source, eval(MA2Type));
          
      vAvg = ((vMA1-vMA2)/vMA1);/*((vMA1.getValue(MAStudy.MA)-vMA2.getValue(MAStudy.MA))/(vMA1.getValue(MAStudy.MA)));*/
          
          //end section for user defined variable

          
      vValue vAvg//replace vAvg with the user defined variable

          
      if(vValue == null) {
              return;
          }
          
          if (
      aValue == null) {
              
      aValue = new Array(MALength);
          }
          if (
      getBarState() == BARSTATE_NEWBAR) {
              
      aValue.pop();  
              
      aValue.unshift(vValue);
          } else {
          
      aValue[0] = vValue;
          }

          for(
      0MALengthi++) {
              
      vSum += aValue[i];
          }
          
          var 
      vMA = (vSum/MALength);
          
          
          if(
      MAType=="Exponential"){
              if(
      aValue[MALength-1] != nullvEMA EMA(MALengthaValue); 
              return 
      vEMA;
          }else{
              return 
      vMA;
          }
          return new Array(
      vValuevMA);
          
      }


      function 
      EMA(MALengthaValue) {

          var 
      dSum 0.0;

          if(
      getBarState() == BARSTATE_ALLBARS || bPrimed == false) {
              
      dPercent = (2.0 / (MALength 1.0));
              
      bPrimed false;
          }

          if (
      getBarState() == BARSTATE_NEWBAR) {
              
      vEMA1 vEMA;
          }

          if(
      bPrimed == false) {
              for(var 
      0MALengthi++) {
                  
      dSum += aValue[i];
              }
              
      bPrimed true;
              return (
      dSum MALength);
          } else {
              return (((
      vValue vEMA1) * dPercent) + vEMA1);
          }

      Comment


      • #4
        Nooze
        The enclosed revision should now work
        Alex

        PHP Code:
        /************************************************
        Alexis C. Montenegro © August 2004                        
        Use and/or modify this code freely. If you redistribute it
        please include this and/or any other comment blocks and a 
        description of any changes you make.                      
         *****************************************************/

        var vMA1 null;
        var 
        vMA2 null;

        function 
        preMain() {

            
        setStudyTitle("PPO");
            
        setShowCursorLabel(false);
            
        setDefaultBarStyle(PS_SOLID0);
            
        setDefaultBarFgColor(Color.yellow0);
            
        setDefaultBarThickness(10);
            
        setPlotType(PLOTTYPE_LINE0);
            
        //setPlotType(PLOTTYPE_HISTOGRAM); 
            //checkVersion(1,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/basicMAx2.efs"); //Comment this line out if modifying the code
                
            
        var fp1 = new FunctionParameter("MALength"FunctionParameter.NUMBER);
            
        fp1.setLowerLimit(1);        
            
        fp1.setDefault(9);
            
            var 
        fp2 = new FunctionParameter("MAType"FunctionParameter.STRING);
            
        fp2.setName("MAType");
            
        fp2.addOption("Simple");
            
        fp2.addOption("Exponential");
            
        fp2.setDefault("Exponential"); //Edit this value to set a new default

            
        var fp3 = new FunctionParameter("MA1Length"FunctionParameter.NUMBER);
            
        fp3.setLowerLimit(1);        
            
        fp3.setDefault(12); //Edit this value to set a new default
            
            
        var fp4 = new FunctionParameter("MA1Source"FunctionParameter.STRING);
            
        fp4.setName("MA1Source");
            
        fp4.addOption("Close");
            
        fp4.addOption("High");
            
        fp4.addOption("Low");
            
        fp4.addOption("Open");
            
        fp4.addOption("HL/2");
            
        fp4.addOption("HLC/3");
            
        fp4.addOption("OHLC/4");
            
        fp4.setDefault("Close"); //Edit this value to set a new default     
            
            
        var fp5 = new FunctionParameter("MA1Type"FunctionParameter.STRING);
            
        fp5.setName("MA1Type");
            
        fp5.addOption("MAStudy.SIMPLE");
            
        fp5.addOption("MAStudy.EXPONENTIAL");
            
        fp5.addOption("MAStudy.WEIGHTED");
            
        fp5.addOption("MAStudy.VOLUMEWEIGHTED");
            
        fp5.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
            
            
        var fp6 = new FunctionParameter("MA2Length"FunctionParameter.NUMBER);
            
        fp6.setLowerLimit(1);        
            
        fp6.setDefault(26); //Edit this value to set a new default
            
            
        var fp7 = new FunctionParameter("MA2Source"FunctionParameter.STRING);
            
        fp7.setName("MA1Source");
            
        fp7.addOption("Close");
            
        fp7.addOption("High");
            
        fp7.addOption("Low");
            
        fp7.addOption("Open");
            
        fp7.addOption("HL/2");
            
        fp7.addOption("HLC/3");
            
        fp7.addOption("OHLC/4");
            
        fp7.setDefault("Close"); //Edit this value to set a new default
            
            
        var fp8 = new FunctionParameter("MA2Type"FunctionParameter.STRING);
            
        fp8.setName("MA2Type");
            
        fp8.addOption("MAStudy.SIMPLE");
            
        fp8.addOption("MAStudy.EXPONENTIAL");
            
        fp8.addOption("MAStudy.WEIGHTED");
            
        fp8.addOption("MAStudy.VOLUMEWEIGHTED");
            
        fp8.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
                
               
        }

        var 
        bPrimed false
        var 
        vEMA null;
        var 
        vEMA1 null;   
        var 
        dPercent 0.0;
        var 
        vValue 0.0;
        var 
        aValue null;
        var 
        vAvg null;//used in the example provided

        function  main(MALength,MAType,MA1Length,MA1Source,MA1Type,MA2Length,MA2Source,MA2Type) {

            var 
        i;
            var 
        vSum 0.0;
            
            
        //insert below the calculations required for the user defined variable (example provided)
            
        if (vMA1 == nullvMA1 = new MAStudy(MA1LengthMA1Source, eval(MA1Type));
            if (
        vMA2 == nullvMA2 = new MAStudy(MA2LengthMA2Source, eval(MA2Type));
            
            var 
        Avg1 vMA1.getValue(MAStudy.MA)
            if(
        Avg1 == null) return;
            var 
        Avg2 vMA2.getValue(MAStudy.MA);
            if(
        Avg2 == null) return;
            
            
        vAvg = (Avg1-Avg2)/Avg1;
            
            
        //end section for user defined variable

            
        vValue vAvg//replace vAvg with the user defined variable

            
        if(vValue == null) {
                return;
            }
            
            if (
        aValue == null) {
                
        aValue = new Array(MALength);
            }
            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        aValue.pop();  
                
        aValue.unshift(vValue);
            } else {
            
        aValue[0] = vValue;
            }

            for(
        0MALengthi++) {
                
        vSum += aValue[i];
            }
            
            var 
        vMA = (vSum/MALength);
            
            
            if(
        MAType=="Exponential"){
                if(
        aValue[MALength-1] != nullvEMA EMA(MALengthaValue); 
                return new Array( 
        vValue,vEMA);
            }else{
                return new Array (
        vValue,vMA);
            }
        }


        function 
        EMA(MALengthaValue) {

            var 
        dSum 0.0;

            if(
        getBarState() == BARSTATE_ALLBARS || bPrimed == false) {
                
        dPercent = (2.0 / (MALength 1.0));
                
        bPrimed false;
            }

            if (
        getBarState() == BARSTATE_NEWBAR) {
                
        vEMA1 vEMA;
            }

            if(
        bPrimed == false) {
                for(var 
        0MALengthi++) {
                    
        dSum += aValue[i];
                }
                
        bPrimed true;
                return (
        dSum MALength);
            } else {
                return (((
        vValue vEMA1) * dPercent) + vEMA1);
            }

        Comment


        • #5
          Nooze
          FWIW you could have also written this section

          PHP Code:
          //insert below the calculations required for the user defined variable (example provided)
              
          if (vMA1 == nullvMA1 = new MAStudy(MA1LengthMA1Source, eval(MA1Type));
              if (
          vMA2 == nullvMA2 = new MAStudy(MA2LengthMA2Source, eval(MA2Type));
              
              var 
          Avg1 vMA1.getValue(MAStudy.MA)
              if(
          Avg1 == null) return;
              var 
          Avg2 vMA2.getValue(MAStudy.MA);
              if(
          Avg2 == null) return;
              
              
          vAvg = (Avg1-Avg2)/Avg1;
              
              
          //end section for user defined variable 
          in the following way

          PHP Code:
          //insert below the calculations required for the user defined variable (example provided)
              
          if (vMA1 == nullvMA1 = new MAStudy(MA1LengthMA1Source, eval(MA1Type));
              if (
          vMA2 == nullvMA2 = new MAStudy(MA2LengthMA2Source, eval(MA2Type));
              
                  
              
          vAvg = (vMA1.getValue(MAStudy.MA)-vMA2.getValue(MAStudy.MA))/vMA1.getValue(MAStudy.MA);
              
              
          //end section for user defined variable 
          which is how you had it at one point
          Hope this helps
          Alex

          Comment


          • #6
            Hi,

            Alex, I understand my mistake in syntax... but still I have different value for the indicator PPO in eSignal and TradeStation.

            I can't find what is I wrong... So, I can't trade at full capacity wihtout this indicator.

            Your help will be invaluable!

            Thank you!

            PPO code (EasyLanguage)

            Code:
             Inputs: Len1(12), Len2(26), LenSig(9);
            
            Value1 = (XAverage(C,Len1)-XAverage(C,Len2))/XAverage(C,Len1);
            Value2 = XAverage(Value1,LenSig);
            Value3 = (Value1-Value2);
            
            Plot1(Value1, "PPO", cyan);
            Plot2(Value2, "Sig", red);
            Plot3(Value3, "Hist", 16);
            PPO code (eSignal)

            PHP Code:
            /************************************************
            Alexis C. Montenegro © August 2004                        
            Use and/or modify this code freely. If you redistribute it
            please include this and/or any other comment blocks and a 
            description of any changes you make.                      
              *****************************************************/

            var vMA1 null;
            var 
            vMA2 null;

            function 
            preMain() {

                
            setStudyTitle("PPO");
                
            setShowCursorLabel(false);
                
            setDefaultBarFgColor(Color.cyan,0); 
                
            setDefaultBarFgColor(Color.red,1); 
                
            setDefaultBarFgColor(Color.lightgrey,2);
                
            //addBand(0, PS_SOLID, 1, Color.lightgrey,"zero"); 
                
            setDefaultBarThickness(2,0);
                
            setDefaultBarThickness(1,1);
                
            setDefaultBarThickness(2,2);
                
            setPlotType(PLOTTYPE_HISTOGRAM,2);
                
            //checkVersion(1,"http://share.esignal.com/ContentRoot/ACM%20Test/Formulas/basicMAx2.efs"); //Comment this line out if modifying the code
                    
                
            var fp1 = new FunctionParameter("MALength"FunctionParameter.NUMBER);
                
            fp1.setLowerLimit(1);        
                
            fp1.setDefault(9);
                
                var 
            fp2 = new FunctionParameter("MAType"FunctionParameter.STRING);
                
            fp2.setName("MAType");
                
            fp2.addOption("Simple");
                
            fp2.addOption("Exponential");
                
            fp2.setDefault("Exponential"); //Edit this value to set a new default

                
            var fp3 = new FunctionParameter("MA1Length"FunctionParameter.NUMBER);
                
            fp3.setLowerLimit(1);        
                
            fp3.setDefault(12); //Edit this value to set a new default
                
                
            var fp4 = new FunctionParameter("MA1Source"FunctionParameter.STRING);
                
            fp4.setName("MA1Source");
                
            fp4.addOption("Close");
                
            fp4.addOption("High");
                
            fp4.addOption("Low");
                
            fp4.addOption("Open");
                
            fp4.addOption("HL/2");
                
            fp4.addOption("HLC/3");
                
            fp4.addOption("OHLC/4");
                
            fp4.setDefault("Close"); //Edit this value to set a new default     
                
                
            var fp5 = new FunctionParameter("MA1Type"FunctionParameter.STRING);
                
            fp5.setName("MA1Type");
                
            fp5.addOption("MAStudy.SIMPLE");
                
            fp5.addOption("MAStudy.EXPONENTIAL");
                
            fp5.addOption("MAStudy.WEIGHTED");
                
            fp5.addOption("MAStudy.VOLUMEWEIGHTED");
                
            fp5.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
                
                
            var fp6 = new FunctionParameter("MA2Length"FunctionParameter.NUMBER);
                
            fp6.setLowerLimit(1);        
                
            fp6.setDefault(26); //Edit this value to set a new default
                
                
            var fp7 = new FunctionParameter("MA2Source"FunctionParameter.STRING);
                
            fp7.setName("MA1Source");
                
            fp7.addOption("Close");
                
            fp7.addOption("High");
                
            fp7.addOption("Low");
                
            fp7.addOption("Open");
                
            fp7.addOption("HL/2");
                
            fp7.addOption("HLC/3");
                
            fp7.addOption("OHLC/4");
                
            fp7.setDefault("Close"); //Edit this value to set a new default
                
                
            var fp8 = new FunctionParameter("MA2Type"FunctionParameter.STRING);
                
            fp8.setName("MA2Type");
                
            fp8.addOption("MAStudy.SIMPLE");
                
            fp8.addOption("MAStudy.EXPONENTIAL");
                
            fp8.addOption("MAStudy.WEIGHTED");
                
            fp8.addOption("MAStudy.VOLUMEWEIGHTED");
                
            fp8.setDefault("MAStudy.EXPONENTIAL"); //Edit this value to set a new default
                    
                   
            }

            var 
            bPrimed false
            var 
            vEMA null;
            var 
            vEMA1 null;   
            var 
            dPercent 0.0;
            var 
            vValue 0.0;
            var 
            aValue null;
            var 
            vAvg null;//used in the example provided

            function   main(MALength,MAType,MA1Length,MA1Source,MA1Type,MA2Length,MA2Source,MA2Type) {

                var 
            i;
                var 
            vSum 0.0;
                
                
            //insert below the calculations required for the user defined variable (example provided)
                
            if (vMA1 == nullvMA1 = new MAStudy(MA1LengthMA1Source, eval(MA1Type));
                if (
            vMA2 == nullvMA2 = new MAStudy(MA2LengthMA2Source, eval(MA2Type));
                
                var 
            Avg1 vMA1.getValue(MAStudy.MA)
                if(
            Avg1 == null) return;
                var 
            Avg2 vMA2.getValue(MAStudy.MA);
                if(
            Avg2 == null) return;
                
                
            vAvg = (Avg1-Avg2)/Avg1;
                
                
            //end section for user defined variable

                
            vValue vAvg//replace vAvg with the user defined variable

                
            if(vValue == null) {
                    return;
                }
                
                if (
            aValue == null) {
                    
            aValue = new Array(MALength);
                }
                if (
            getBarState() == BARSTATE_NEWBAR) {
                    
            aValue.pop();  
                    
            aValue.unshift(vValue);
                } else {
                
            aValue[0] = vValue;
                }

                for(
            0MALengthi++) {
                    
            vSum += aValue[i];
                }
                
                var 
            vMA = (vSum/MALength);
                
                
                if(
            MAType=="Exponential"){
                    if(
            aValue[MALength-1] != nullvEMA EMA(MALengthaValue); 
                    return new Array( 
            vValue,vEMA, (vValue-vEMA));
                }else{
                    return new Array (
            vValue,vMA, (vValue-vMA));
                }
            }


            function 
            EMA(MALengthaValue) {

                var 
            dSum 0.0;

                if(
            getBarState() == BARSTATE_ALLBARS || bPrimed == false) {
                    
            dPercent = (2.0 / (MALength 1.0));
                    
            bPrimed false;
                }

                if (
            getBarState() == BARSTATE_NEWBAR) {
                    
            vEMA1 vEMA;
                }

                if(
            bPrimed == false) {
                    for(var 
            0MALengthi++) {
                        
            dSum += aValue[i];
                    }
                    
            bPrimed true;
                    return (
            dSum MALength);
                } else {
                    return (((
            vValue vEMA1) * dPercent) + vEMA1);
                }

            See charts:



            Comment


            • #7
              Nooze
              The errors are in the missing Offset parameter in these two lines

              if (vMA1 == null) vMA1 = new MAStudy(MA1Length, MA1Source, eval(MA1Type));
              if (vMA2 == null) vMA2 = new MAStudy(MA2Length, MA2Source, eval(MA2Type));


              which need to be replaced with the following

              if (vMA1 == null) vMA1 = new MAStudy(MA1Length, 0, MA1Source, eval(MA1Type));
              if (vMA2 == null) vMA2 = new MAStudy(MA2Length, 0, MA2Source, eval(MA2Type));


              I believe this will take care of the issue
              Alex

              Comment


              • #8
                Alex, you are great!

                It's exactly what I need...

                Thank you again!

                Best wishes,

                Comment


                • #9
                  Nooze
                  My pleasure and thank you for the compliment.
                  Alex

                  Comment


                  • #10
                    Hi Alex,

                    I'd like to ask you one more question... I find your EFS "2daybar_box" very interesting ... and I have slightly modified it (add mid range line and remove "SetBarColor") because I intend to use this indicator for intraday...

                    Unfortunately, I have a problem: this plot does not update on every bar (I attached an example).

                    Can I solve this problem without rewriting the entire code.

                    Thank you in advance!

                    Best wishes,


                    Comment


                    • #11
                      Nooze
                      The easiest solution is to add setComputeOnClose(); in preMain(). This will leave the current bar "unboxed" but as soon as that is completed the boxes will update.
                      Alex

                      Comment

                      Working...
                      X