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.
Thanks,
Ted.
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(
vRSILength, vRSISource, vStoRSIPerK, vRSIUpperBand, vRSILowerBand, vMALength, vMAType
) {
// Check for New Bar
if ( getBarState() == BARSTATE_NEWBAR ) {
vBarCount += 1;
vNewBar = "true";
} else {
vNewBar = "false";
}
// Add Bands
addBand(vRSIUpperBand, PS_DASH, 1, Color.red, 1);
addBand(vRSILowerBand, PS_DASH, 1, Color.red, 2);
// RSI
if (vRSI == null) vRSI = new RSIStudy(vRSILength, vRSISource);
// Stochastic of RSI
if (
//( vNewBar == "true" )
( vBarCount > ( vRSILength + vStoRSIPerK ) )
) {
var vRSIHH = vRSI.getValue(RSIStudy.RSI);
var vRSILL = vRSI.getValue(RSIStudy.RSI);
var i;
for(i = 1; i < vStoRSIPerK; i++) {
vRSIHH = Math.max( vRSIHH, vRSI.getValue(RSIStudy.RSI, -i) );
vRSILL = Math.min( vRSILL, vRSI.getValue(RSIStudy.RSI, -i) );
}
var vStoRSI = ( ( vRSI.getValue(RSIStudy.RSI) - vRSILL) / (vRSIHH - vRSILL ) ) * 100;
}
// Exponential Moving Average
if (
( vBarCount > ( vRSILength + vStoRSIPerK ) )
) {
var vSmoothConst = 2 / ( vMALength + 1 );
var K = 2 / (vMALength + 1);
vEMA = vSmoothConst * vStoRSI + (1 - vSmoothConst ) * vPPEMA;
}
return vEMA;
}
Ted.
Comment