The else if (Strategy... && bAdd... right before the debugPrintln(xCurRSI) at the end of the script has been commented out. When I include it the RSI plot has missing data. Even though the OB snd OS indicators are painting red and green in the appropriate spots, in some parts of the chart the RSI plot is interrupted with straight lines. Why would a Strategy statement affect the values of the plot?
PHP Code:
/**************************************************
Computes a Simple Moving average of the RSI
v2.00 back testing version
***************************************************/
var fpArray = new Array();
var bInit = false;
var xCurRSI = 0;
var xLstRSI = 0;
var OB = 90;
var OS = 10;
var xLong = 90;
var xShort = 10;
function preMain() {
var xRSI = null;
var avgRSI = null;
setPriceStudy(false);
setStudyTitle("avgRSI");
setCursorLabelName("avgRSI", 0);
setDefaultBarFgColor(Color.brown, 0);
setPlotType(PLOTTYPE_LINE,0);
setDefaultBarThickness(1,0);
setStudyMin(0);
setShowTitleParameters( false );
setStudyMax(100);
askForInput();
var x=0;
fpArray[x] = new FunctionParameter("RSI_Length", FunctionParameter.NUMBER);
with(fpArray[x]){
setLowerLimit(1);
setDefault(2);
}
x++;
fpArray[x] = new FunctionParameter("MA_Len", FunctionParameter.NUMBER);
with(fpArray[x]){
setLowerLimit(1);
setDefault(2);
}
x++;
// Long Trigger when RSI OverSold at 0+TW, Short when OB=100-TW
fpArray[x] = new FunctionParameter("TW", FunctionParameter.NUMBER);
with(fpArray[x]){
setName("OB/OS Trigger Width");
setLowerLimit(1);
setDefault(10);
}
x++;
// Longs will eXit when RSI = 100-XW, Shorts when RSI=0+XW
fpArray[x] = new FunctionParameter("XW", FunctionParameter.NUMBER);
with(fpArray[x]){
setName("eXit Width");
setLowerLimit(1);
setDefault(10);
}
x++;
fpArray[x] = new FunctionParameter("bAdd", FunctionParameter.BOOLEAN);
with( fpArray[x] ) {
setName("Add-to positions");
setDefault(false);
}
x++;
fpArray[x] = new FunctionParameter("bDisp", FunctionParameter.BOOLEAN);
with( fpArray[x] ) {
setName("Display in Cursor Window");
setDefault(true);
}
x++;
fpArray[x] = new FunctionParameter("cRSI", FunctionParameter.COLOR);
with(fpArray[x]){
setDefault(Color.brown);
}
}
function main(RSI_Length, MA_Len, TW, XW, bAdd, bDisp, cRSI) {
if (bInit == false) {
OB = 100 - TW;
OS = TW;
xLong = 100-XW;
xShort = XW;
vparams = ( "v2.01a RSI(" + RSI_Length + ")" + " avg=" + MA_Len );
setStudyTitle ( vparams );
if ( bDisp == false ) setShowCursorLabel(false);
drawTextPixel( 2, 4, "RSI-avg Ver2.01a", Color.yellow, null, Text.RELATIVETOLEFT | Text.RELATIVETOBOTTOM, null, 10, -200 );
addBand(OS, PS_DOT, 1, Color.lime,"Lower");
addBand(OB, PS_DOT, 1, Color.red,"Upper");
bInit = true;
}
setBarFgColor(cRSI,0);
avgRSI = efsInternal( "custSeriesFunc", RSI_Length, MA_Len);
var x200 = sma(200);
var xCurRSI = avgRSI.getValue(0);
var xLstRSI = avgRSI.getValue(-1);
if (xCurRSI==null || xLstRSI==null) return;
if ( xCurRSI > OB && close(0) < x200 ) {
setBarBgColor(Color.red,0,OB,100);
} else if ( xCurRSI < OS && close(0) > x200 ) {
setBarBgColor(Color.green,0,0,OS);
}
var RSINum = xLstRSI.toFixed(2);
// Exit trade at open if signal was true at close of last bar
if (Strategy.isInTrade()) {
if(Strategy.isLong() && xLstRSI > xLong) {
Strategy.doSell("eXit Long", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
} else if(Strategy.isShort() && xLstRSI < xShort) {
Strategy.doCover("eXit Short", Strategy.MARKET, Strategy.THISBAR, Strategy.ALL);
}
}
/**************************************************
Enter trade at open if signal was true at close of last bar (1 lot only)
or if bAdd()==true add to existing position
buy oversold RSI only if price > 200 SMA
sell overbot RSI only if price < 200 SMA
***************************************************/
if (!Strategy.isInTrade() ) {
if ( xLstRSI > OB && close(-1) < x200 ) {
Strategy.doShort("RSI-OB " + RSINum, Strategy.MARKET, Strategy.THISBAR);
} else if ( xLstRSI < OS && close(-1) > x200 ) {
Strategy.doLong("RSI-OS " + RSINum, Strategy.MARKET, Strategy.THISBAR);
}
}
/* else if (Strategy.isInTrade() && bAdd()==true) {
if ( xLstRSI > OB && close(-1) < x200 ) {
Strategy.doShort("RSI-OB " + RSINum, Strategy.MARKET, Strategy.THISBAR);
} else if ( xLstRSI < OS && close(-1) > x200 ) {
Strategy.doLong("RSI-OS " + RSINum, Strategy.MARKET, Strategy.THISBAR);
}
}
*/
debugPrintln(xCurRSI);
return ( xCurRSI );
}
function custSeriesFunc (lenRSI, lenMA ) {
var xRSI = new RSIStudy(lenRSI);
var study1 = new MAStudy(lenMA, 0, xRSI, RSIStudy.RSI, MAStudy.SIMPLE);
return study1.getValue(MAStudy.MA);
}
Comment