Announcement

Collapse
No announcement yet.

EMA of Stochastic of RSI

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

  • EMA of Stochastic of RSI

    I'm trying to create an Exponential Moving Average of a Stochastic of a RSI. I can compare it to another platform, so know that what I've created isn't right. Stochastic of RSI is correct, but the EMA isn't. A little help?

    I'd prefer to be able to use the built-in MA study, so I can change the MAType if possible. Unfortunately, I still couldn't figure it out after browsing the forum. It appeared that I couldn't use the built-in MA unless I used high/low/close/open or another built-in study as the source.

    PHP Code:
    /******************
    EMA of Stochastic of RSI
    *******************/

    var vBarCount   0;
    var 
    vNewBar     "false";

    var 
    vRSI        null;
    var 
    aryStoRSI   null;
    var 
    vEMA        1;
    var 
    vPPEMA      1;
    var 
    vMAofStoRSI null;



    function 
    preMain() {
        
    setStudyTitle("EMAStoRSI v1.021");
        
    setCursorLabelName("EMAStoRSI");
        
    setPriceStudy(false);
        
    setStudyMin(0);
        
    setStudyMax(100);

        var 
    fp01 = new FunctionParameter("vRSILength"FunctionParameter.NUMBER);
            
    fp01.setName("RSI Length");
            
    fp01.setDefault(8);  

        var 
    fp02 = new FunctionParameter("vRSISource"FunctionParameter.STRING);
            
    fp02.setName("RSISource");
            
    fp02.addOption("Close");
            
    fp02.addOption("High");
            
    fp02.addOption("Low");
            
    fp02.addOption("Open");
            
    fp02.addOption("HL/2");
            
    fp02.addOption("HLC/3");
            
    fp02.addOption("OHLC/4");
            
    fp02.setDefault("Close");

        var 
    fp03 = new FunctionParameter("vStoRSIPerK"FunctionParameter.NUMBER);
            
    fp03.setName("RSI Length");
            
    fp03.setDefault(8);  

        var 
    fp05 = new FunctionParameter("vRSIUpperBand"FunctionParameter.NUMBER);
            
    fp05.setName("IFRSI Upper Band");
            
    fp05.setUpperLimit(100);
            
    fp05.setLowerLimit(0);        
            
    fp05.setDefault(80);

        var 
    fp06 = new FunctionParameter("vRSILowerBand"FunctionParameter.NUMBER);
            
    fp06.setName("IFRSI Lower Band");
            
    fp06.setUpperLimit(100);
            
    fp06.setLowerLimit(0);        
            
    fp06.setDefault(20);

        var 
    fp07 = new FunctionParameter("vMALength"FunctionParameter.NUMBER);
            
    fp07.setName("MA Length");
            
    fp07.setDefault(3);  

        var 
    fp8 = new FunctionParameter("vMAType"FunctionParameter.STRING);
            
    fp8.setName("MA Type");
            
    fp8.addOption("MAStudy.SIMPLE");
            
    fp8.addOption("MAStudy.EXPONENTIAL");
            
    fp8.addOption("MAStudy.WEIGHTED");
            
    fp8.addOption("MAStudy.VOLUMEWEIGHTED");
            
    fp8.setDefault("MAStudy.SIMPLE");
    }


    function 
    main(

      
    vRSILengthvRSISourcevStoRSIPerKvRSIUpperBandvRSILowerBandvMALengthvMAType

    ) {

        
    // Check for New Bar
        
    if ( getBarState() == BARSTATE_NEWBAR ) {
            
    vBarCount += 1;
            
    vNewBar "true";
        } else {
            
    vNewBar "false";
        }

        
    // Add Bands
        
    addBand(vRSIUpperBandPS_DASH1Color.red1);
        
    addBand(vRSILowerBandPS_DASH1Color.red2);

        
    // RSI
        
    if (vRSI == nullvRSI = new RSIStudy(vRSILengthvRSISource);

        
    // Stochastic of RSI
        
    if (
            
    //( vNewBar == "true" )
            
    vBarCount > ( vRSILength vStoRSIPerK ) )
        ) {
            var 
    vRSIHH vRSI.getValue(RSIStudy.RSI);
            var 
    vRSILL vRSI.getValue(RSIStudy.RSI);
            var 
    i;
            for(
    1vStoRSIPerKi++) {
                
    vRSIHH Math.maxvRSIHHvRSI.getValue(RSIStudy.RSI, -i) );
                
    vRSILL Math.minvRSILLvRSI.getValue(RSIStudy.RSI, -i) );
            }
            var 
    vStoRSI = ( ( vRSI.getValue(RSIStudy.RSI) - vRSILL) / (vRSIHH vRSILL ) ) * 100;
         }

        
    // Exponential Moving Average
        
    if (
            ( 
    vBarCount > ( vRSILength vStoRSIPerK ) )
        ) {
            var 
    vSmoothConst / ( vMALength );
            var 
    / (vMALength 1); 
            
    vEMA vSmoothConst vStoRSI + (vSmoothConst ) * vPPEMA;
        }

        return 
    vEMA;


    Thanks,
    Ted.

  • #2
    Ted
    At this time builtin studies can only use OHLCVoi or another builtin study as inputs.
    With regards to the code required for an ema you may want to check emacalc.efs written by JasonK which is available here
    Alex

    Comment


    • #3
      still not right :\

      Ok, I did that, but it still doesn't look right. Any ideas?

      Thanks,
      Ted.

      PHP Code:
      /****************************************************
      *****************************************************/

      var vBarCount   0;
      var 
      vNewBar     "false";

      var 
      vRSI        null;
      var 
      aryStoRSI   null;

      var 
      vEMAlength  null;
      var 
      vEMA        null;
      var 
      vEMA1       null;

      var 
      dPercent 0.0;
      var 
      bPrimed false;

      function 
      preMain() {
          
      setStudyTitle("EMAStoRSI v1.021");
          
      setCursorLabelName("EMAStoRSI");
          
      setPriceStudy(false);
          
      //setStudyMin(0);
          //setStudyMax(100);

          
      var fp01 = new FunctionParameter("vRSILength"FunctionParameter.NUMBER);
              
      fp01.setName("RSI Length");
              
      fp01.setDefault(8);  

          var 
      fp02 = new FunctionParameter("vRSISource"FunctionParameter.STRING);
              
      fp02.setName("RSISource");
              
      fp02.addOption("Close");
              
      fp02.addOption("High");
              
      fp02.addOption("Low");
              
      fp02.addOption("Open");
              
      fp02.addOption("HL/2");
              
      fp02.addOption("HLC/3");
              
      fp02.addOption("OHLC/4");
              
      fp02.setDefault("Close");

          var 
      fp03 = new FunctionParameter("vStoRSIPerK"FunctionParameter.NUMBER);
              
      fp03.setName("RSI Length");
              
      fp03.setDefault(8);  

          var 
      fp05 = new FunctionParameter("vRSIUpperBand"FunctionParameter.NUMBER);
              
      fp05.setName("IFRSI Upper Band");
              
      fp05.setUpperLimit(100);
              
      fp05.setLowerLimit(0);        
              
      fp05.setDefault(80);

          var 
      fp06 = new FunctionParameter("vRSILowerBand"FunctionParameter.NUMBER);
              
      fp06.setName("IFRSI Lower Band");
              
      fp06.setUpperLimit(100);
              
      fp06.setLowerLimit(0);        
              
      fp06.setDefault(20);

          var 
      fp07 = new FunctionParameter("vMALength"FunctionParameter.NUMBER);
              
      fp07.setName("MA Length");
              
      fp07.setDefault(3);  
      }


      function 
      main(

        
      vRSILengthvRSISourcevStoRSIPerKvRSIUpperBandvRSILowerBandvMALength

      ) {

          
      // Check for New Bar
          
      if ( getBarState() == BARSTATE_NEWBAR ) {
              
      vBarCount += 1;
              
      vNewBar "true";
          } else {
              
      vNewBar "false";
          }

          
      // Add Bands
          
      addBand(vRSIUpperBandPS_DASH1Color.red1);
          
      addBand(vRSILowerBandPS_DASH1Color.red2);

          
      // Initialize vEMAlength
          
      if (vEMAlength == nullvEMAlength vMALength;

          
      // RSI
          
      if (vRSI == nullvRSI = new RSIStudy(vRSILengthvRSISource);


          
      // EMA of StoRSI
          
      if (
              ( 
      vBarCount > ( vRSILength vStoRSIPerK ) )
          ) {
              
      // Stack Array of StoRSI from last to current
              
      if (aryStoRSI == nullaryStoRSI = new Array(vMALength);
              var 
      u;
              for ( 
      = ((vMALength -1) * -1); <= 0u++ ) {
                  
      // Stochastic of RSI
                  
      var vRSIHH vRSI.getValue(RSIStudy.RSIu);
                  var 
      vRSILL vRSI.getValue(RSIStudy.RSIu);
                  var 
      i;
                  for(
      1vStoRSIPerKi++) {
                      
      vRSIHH Math.maxvRSIHHvRSI.getValue(RSIStudy.RSI, -u) );
                      
      vRSILL Math.minvRSILLvRSI.getValue(RSIStudy.RSI, -u) );
                  }
                  var 
      vStoRSI = ( ( vRSI.getValue(RSIStudy.RSIu) - vRSILL) / (vRSIHH vRSILL ) ) * 100;

                  
      aryStoRSI.pop();
                  
      aryStoRSI.unshift(vStoRSI);
              }

              
      // EMA
              
      var nBarState getBarState();
              var 
      dSum 0.0;
              var 
      dRef;
          
              if(
      nBarState == BARSTATE_ALLBARS || bPrimed == false) {
                  
      dPercent = (2.0 / (vEMAlength 1.0));
                  
      bPrimed false;
              }
          
              if (
      nBarState == BARSTATE_NEWBAR) {
                  
      vEMA1 vEMA;
              }
          
              if(
      bPrimed == false) {
                  for(
      0vEMAlengthi++) {
                      
      dSum += aryStoRSI[i];
                  }
                  
      bPrimed true;
                  return (
      dSum vEMAlength);
              } else {
                  return (((
      vStoRSI vEMA1) * dPercent) + vEMA1);
              }
          } else {
              return;
          }

      Comment


      • #4
        original code:

        { Relative Strength Index}
        rsi_r:= (CLOSE - ref(CLOSE,-1));
        rsi_rs := Wilders(if(rsi_r>0,rsi_r,0),Per) / Wilders(if(rsi_r<0,Abs(rsi_r),0),Per);
        RS:= 100-(100/(1+rsi_rs));

        {Stochastic RSI Oscillator}
        StRSI := Mov( ( ( ( RS - LLV( RS,PerK ) ) /
        ( HHV(RS,PerK) - LLV( RS,PerK) ) ) * 100 ) , Sl, E );

        should look like this...
        Attached Files

        Comment


        • #5
          looks like this...
          Attached Files

          Comment


          • #6
            Hello Ted,

            Here's a formula that should help you figure out the variance in your formula. This version also displays the %K and %D (of RSI). If you want to remove those and just display the EMA of %K, change the return line to return vEMA.

            EMAof(StochOfRSI).efs



            PHP Code:
            /*****************************************************************
            Provided By : eSignal. (c) Copyright 2004
            *****************************************************************/

            addBand(90PS_SOLID2Color.yellow"Upper");
            addBand(10PS_SOLID2Color.yellow"Lower");

            function 
            preMain() {
                
            setStudyTitle("Stochastic of RSI ");
                
            setCursorLabelName("\%K of RSI"0);
                
            setCursorLabelName("\%D of RSI"1);
                
            setCursorLabelName("EMA of \%K "2);
                
            setDefaultBarFgColor(Color.blue0);
                
            setDefaultBarFgColor(Color.red1);
                
            setDefaultBarFgColor(Color.navy2);
                
                
            setStudyMax(105);
                
            setStudyMin(-5);

                var 
            fp1 = new FunctionParameter("PriceSource"FunctionParameter.STRING);
                
            fp1.setName("RSI Price Source");
                
            fp1.addOption("Open");
                
            fp1.addOption("High");
                
            fp1.addOption("Low");
                
            fp1.addOption("Close");
                
            fp1.addOption("HL/2");
                
            fp1.addOption("HLC/3");
                
            fp1.addOption("OHLC/4");
                
            fp1.setDefault("Close");
                
                var 
            fp2 = new FunctionParameter("rsiLength"FunctionParameter.NUMBER);
                
            fp2.setName("RSI Length");
                
            fp2.setLowerLimit(1);
                
            fp2.setDefault(13);
                
                var 
            fp3 = new FunctionParameter("Klength"FunctionParameter.NUMBER);
                
            fp3.setName("\%K Length");
                
            fp3.setLowerLimit(1);
                
            fp3.setDefault(8);

                var 
            fp4 = new FunctionParameter("Ksmooth"FunctionParameter.NUMBER);
                
            fp4.setName("\%K Smoothing");
                
            fp4.setLowerLimit(1);
                
            fp4.setDefault(8);
                
                var 
            fp5 = new FunctionParameter("Dlength"FunctionParameter.NUMBER);
                
            fp5.setName("\%D Length");
                
            fp5.setLowerLimit(1);
                
            fp5.setDefault(5);
                
                var 
            fp6 = new FunctionParameter("SmoothKline"FunctionParameter.BOOLEAN);
                
            fp6.setName("Smooth \%K");
                
            fp6.setDefault(true);
                
                var 
            fp7 = new FunctionParameter("EMAlength"FunctionParameter.NUMBER);
                
            fp7.setName("EMA Length");
                
            fp7.setLowerLimit(1);
                
            fp7.setDefault(3);
            }

            function 
            HHrsi(nLength) {
                var 
            hh null;
                
            hh aRSI[0];
                for(
            0nLengthi++) {
                    
            hh Math.max(hhaRSI[i]);
                }
                return 
            hh;
            }

            function 
            LLrsi(nLength) {
                var 
            ll null;
                
            ll aRSI[0];;
                for(
            0nLengthi++) {
                    
            ll Math.min(llaRSI[i]);
                }
                return 
            ll;
            }

            var 
            vStochRSI null;

            function 
            StochK(inputrsiLengthnLengthnSmoothing) {
                var 
            nState getBarState();
                var 
            percentK;
                var 
            vValue;
                var 
            llhh;
                var 
            sum 0;

                
            ll LLrsi(inputrsiLength)       
                
            hh HHrsi(inputrsiLength);

                if (
            nState == BARSTATE_NEWBAR && vStochRSI != null) {
                    
            aStoch.pop();
                    
            aStoch.unshift(vStochRSI);
                }

                
            vStochRSI = ((vRSI ll) / (hh ll)) * 100;    // no smoothing
                
            aStoch[0] = vStochRSI;

                if (
            vSmooth == "true") {
                    for(
            0nSmoothingi++) { // for smoothing
                        
            sum += aStoch[i];
                    }
                    
            sum /= nSmoothing;
                    return 
            sum;
                } else {
                    return 
            vStochRSI;
                }
            }

            function 
            StochD(nSmoothing) {   
                var 
            sum 0;
                for(
            0nSmoothingi++) { 
                   
            sum += aStochb[i];
                }
                
            sum sum nSmoothing;
                return 
            sum;
            }

            function 
            EMA(nLengthnArray) {
                var 
            nBarState getBarState();
                var 
            dSum 0.0;

                if(
            nBarState == BARSTATE_ALLBARS || bPrimed == false) {
                    
            dPercent = (2.0 / (nLength 1.0));
                    
            bPrimed false;
                }

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

                if(
            bPrimed == false) {
                    for(
            0nLengthi++) {
                        
            dSum += nArray[i];
                    }
                    
            bPrimed true;
                    
            //debugPrintln(nArray);
                    
            return (dSum nLength);
                } else {
                    return (((
            vK vEMA1) * dPercent) + vEMA1);
                }
            }

            var 
            RSIstudy null;
            var 
            aRSI null;
            var 
            vRSI null;
            var 
            aStoch null;
            var 
            aStochb null;
            var 
            vSmooth "false";
            var 
            bEdit true;
            var 
            vK null;
            var 
            vD null;

            // EMA variables
            var vEMA null;
            var 
            vEMA1 null;
            var 
            dPercent 0.0;
            var 
            bPrimed false;

                
            function 
            main(PriceSourcersiLengthKlengthKsmoothDlengthSmoothKlineEMAlength) {
                var 
            nState getBarState();
                
                if (
            bEdit == true || RSIstudy == null || aRSI == null || aStoch == null) {
                    
            RSIstudy = new RSIStudy(rsiLengthPriceSource);
                    
            aStoch = new Array(Math.max(KsmoothKlength));
                    
            aStochb = new Array(Math.max(DlengthEMAlength));
                    
            aRSI = new Array(Math.max(KlengthKsmooth));
                    if (
            SmoothKline != nullvSmooth SmoothKline;
                    
            bEdit false;
                }
                
                if (
            nState == BARSTATE_NEWBAR && vRSI != null) {
                    
            aRSI.pop();
                    
            aRSI.unshift(vRSI);
                    
            aStochb.pop();
                    
            aStochb.unshift(vK);
                }
                
                
            vRSI RSIstudy.getValue(RSIStudy.RSI);
                if (
            vRSI == null) return;
                
            aRSI[0] = vRSI;


                
            vK null;
                
            vD null;
                
                var 
            vLen Math.max(KlengthKsmooth);
                if (
            aRSI[vLen-1] != null) {
                    
            vK StochK(KlengthKlengthKsmooth);
                    
            aStochb[0] = vK;
                    
            vD StochD(Dlength);
                } else {
                    return;
                }

                if (
            aStochb[EMAlength-1] != null && !isNaN(aStochb[EMAlength-1])) {
                    
            vEMA EMA(EMAlengthaStochb);
                }

                return new Array(
            vKvDvEMA);

            Jason K.
            Project Manager
            eSignal - an Interactive Data company

            EFS KnowledgeBase
            JavaScript for EFS Video Series
            EFS Beginner Tutorial Series
            EFS Glossary
            Custom EFS Development Policy

            New User Orientation

            Comment


            • #7
              JasonK,

              Absolutely perfect.

              Thanks!
              Ted.

              Comment

              Working...
              X