StochOfRSI.efs

ICE Data Services -

StochOfRSI.efs    

File Name: StochOfRSI.efs

Description:
Plots the Stochastic %K and %D of the RSI.

Formula Parameters:

  • PriceSource: Default is Close
  • rsiLength: Default is 13
  • Klength: Default is 8
  • Ksmooth: Default is 8
  • Dlength: Default is 5
  • SmoothKline: Default is False

Notes:
All study input parameters are adjustable. Smoothing of %K can be turned on or off by setting the input parameter, SmoothKline, to True or False.

Download File:
StochOfRSI.efs


EFS Code:

/****************************************************************************************************
Copyright © eSignal, a division of Interactive Data Corporation. 2003. All rights reserved. 
This sample eSignal Formula Script (EFS) may be modified and saved under a new 
filename; however, eSignal is no longer responsible for the functionality once modified.
eSignal reserves the right to modify and overwrite this EFS file with each new release.
*****************************************************************************************************/

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);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarFgColor(Color.red, 1);
    
    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);
}

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) {
        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;
}

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;
    
function main(PriceSource, rsiLength, Klength, Ksmooth, Dlength, SmoothKline) {
    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(Dlength);
        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;
    }

    return new Array(vK, vD);
}