EMAof(StochOfRSI).efs

ICE Data Services -

EMAof(StochOfRSI).efs  

EFSLibrary - Discussion Board  

File Name: EMAof(StochOfRSI).efs

Description:
Plots the Exponential Moving Average of the %K output from a Stochastic of RSI study.

Formula Parameters:

  • RSI Price Source - Close - [Open, High, Low, Close, HL/2, HLC/3, OHLC/4]
  • RSI Length - 13
  • %K Length - 8
  • %K Smoothing - 8
  • %D Length - 5
  • Smooth %K - true - [true, false]
  • EMA Length - 3

Notes:

NA

Download File:
EMAof(StochOfRSI).efs


EFS Code:

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

addBand(90, PS_SOLID, 2, Color.yellow, "Upper");
addBand(10, PS_SOLID, 2, Color.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.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    setDefaultBarFgColor(Color.navy, 2);
    
    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(i = 0; i < nLength; i++) {
        hh = Math.max(hh, aRSI[i]);
    }
    return hh;
}

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

var vStochRSI = null;

function StochK(inputrsiLength, nLength, nSmoothing) {
    var nState = getBarState();
    var percentK;
    var vValue;
    var ll, hh;
    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" || vSmooth == true) {
        for(i = 0; i < nSmoothing; i++) { // for smoothing
            sum += aStoch[i];
        }
        sum /= nSmoothing;
        return sum;
    } else {
        return vStochRSI;
    }
}

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

function EMA(nLength, nArray) {
    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(i = 0; i < nLength; i++) {
            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 = "true";
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(PriceSource, rsiLength, Klength, Ksmooth, Dlength, SmoothKline, EMAlength) {
    var nState = getBarState();
    
    if (bEdit == true || RSIstudy == null || aRSI == null || aStoch == null) {
        RSIstudy = new RSIStudy(rsiLength, PriceSource);
        aStoch = new Array(Math.max(Ksmooth, Klength));
        aStochb = new Array(Math.max(Dlength, EMAlength));
        aRSI = new Array(Math.max(Klength, Ksmooth));
        if (SmoothKline != null) vSmooth = 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(Klength, Ksmooth);
    if (aRSI[vLen-1] != null) {
        vK = StochK(Klength, Klength, Ksmooth);
        aStochb[0] = vK;
        vD = StochD(Dlength);
    } else {
        return;
    }

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

    return new Array(vK, vD, vEMA);
}