Announcement

Collapse
No announcement yet.

Strategy code messing up plot

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

  • Strategy code messing up plot

    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.brown0);
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setDefaultBarThickness(1,0);
        
    setStudyMin(0);
        
    setShowTitleParametersfalse );
        
    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);
            
    withfpArray[x] ) {
            
    setName("Add-to positions");
            
    setDefault(false);    
        }
        
    x++;
        
    fpArray[x] =  new FunctionParameter("bDisp"FunctionParameter.BOOLEAN);
            
    withfpArray[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_LengthMA_LenTWXWbAddbDispcRSI) {
        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);
            
    drawTextPixel24"RSI-avg Ver2.01a"Color.yellownullText.RELATIVETOLEFT Text.RELATIVETOBOTTOMnull10, -200 );     
            
    addBand(OSPS_DOT1Color.lime,"Lower");
            
    addBand(OBPS_DOT1Color.red,"Upper");
            
    bInit true;
        }
        
        
    setBarFgColor(cRSI,0);
        
    avgRSI efsInternal"custSeriesFunc"RSI_LengthMA_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.MARKETStrategy.THISBARStrategy.ALL);
            } else if(
    Strategy.isShort() && xLstRSI xShort) {
                
    Strategy.doCover("eXit Short"Strategy.MARKETStrategy.THISBARStrategy.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 " RSINumStrategy.MARKETStrategy.THISBAR);    
            } else if ( 
    xLstRSI OS && close(-1) > x200 ) {
                
    Strategy.doLong("RSI-OS " RSINumStrategy.MARKETStrategy.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 (lenRSIlenMA ) {
        var 
    xRSI = new RSIStudy(lenRSI);
        var 
    study1 = new MAStudy(lenMA0xRSIRSIStudy.RSIMAStudy.SIMPLE);
        return 
    study1.getValue(MAStudy.MA);


  • #2
    EFS doesn't alway report errors...

    bAdd() isn't a function, try getting rid of the parenthesis.

    Comment


    • #3
      That seems to have fixed the problem. Thank you.

      When I using debugPrintln with more than one argument, it would print them in reverse order. Is that normal?

      Comment


      • #4
        Yep, debugPrintln does things in reverse order...

        Comment

        Working...
        X