Announcement

Collapse
No announcement yet.

addBand zIndex or setting draw below indicator

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

  • addBand zIndex or setting draw below indicator

    Hello,

    I am creating an indicator where I want to specify certain "zones" on my indicator. For example, if my indicator oscillates between the values 3 and -3, I want to draw a color band on the range from 1 to 2 and from -1 to -2 (and a third from 1 to -1).

    It seems I can only use setBarBgColor() to specify one zone. I need 3 zones, as mentioned above. Also, it seems using setBarBgColor() only colors up to the current bar, not a complete band across the entire indicator, including into the future.

    A possible solution was to use addBand() repeatedly in a "for" loop, coloring in the area, one "tick" at a time. This works pretty well (though, I don't know about performance of having hundreds of "bands"). However, this draws the bands on top of the actual indicator that I am returning. Is there a way to specify this?

    Another possible solution was to break up the indicator into multiple separate indicators so I can stack them within the chart itself. However, I could not get them to scale properly. I set them both to "scale to the right", but if I drag or resize the indicator, they don't drag and resize together (since it is imperative they scale together (the lines and the indicator itself), obviously)

    Here is a screenshot:


    The green band is using the "addBand" method to color in the "zone", but it is drawing over the indicator itself. But it does draw into the future, which is what I want. The red band is using the setBarBgColor() method, which colors behind the indicator, but I can only color one zone and it does not color into the future.

    Can anyone offer some advice? Thanks in advance!

    Daniel
    Last edited by neoikon; 11-01-2011, 02:34 PM.
    eSignal File Share: http://share.esignal.com/groupcontents.jsp?groupid=1130

  • #2
    Here is one possible solution (though, as you stated, it only colors up to the current bar):

    Wayne

    PHP Code:
    /*********************************************************
    By Alexis C. Montenegro for eSignal © December 2004       
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a 
    description of any changes you make.                      
    **********************************************************/
    //added Bg Colors and Bands
    var fpArray = new Array();

    function 
    preMain() {

        
    setStudyTitle("MACD");
        
    setCursorLabelName("MACD",0);
        
    setCursorLabelName("MACDSig",1);
        
    setCursorLabelName("MACDHist",2);
        
    setDefaultBarFgColor(Color.blue,0); 
        
    setDefaultBarFgColor(Color.red,1);
        
    setDefaultBarFgColor(Color.magenta,2); 
        
    setPlotType(PLOTTYPE_LINE,0);
        
    setPlotType(PLOTTYPE_LINE,1);
        
    setPlotType(PLOTTYPE_HISTOGRAM,2); 
        
    setDefaultBarThickness(1,0);
        
    setDefaultBarThickness(1,1);
        
    setDefaultBarThickness(1,2);
        
    askForInput();
        
        var 
    x=0;
        
    fpArray[x] = new FunctionParameter("Fast"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(12);
        }
        
    fpArray[x] = new FunctionParameter("Slow"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(26);
        }
        
    fpArray[x] = new FunctionParameter("Smoothing"FunctionParameter.NUMBER);
        
    with(fpArray[x++]){
            
    setLowerLimit(1);        
            
    setDefault(9);
        }
        
    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    addOption("open"); 
            
    addOption("high");
            
    addOption("low");
            
    addOption("close");
            
    addOption("hl2");
            
    addOption("hlc3");
            
    addOption("ohlc4"); 
            
    setDefault("close"); 
        }
        
    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
        
    with(fpArray[x++]){
            
    setDefault();
        }
        
    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
        
    with(fpArray[x++]){
            
    setName("Show Parameters");
            
    setDefault(false);
        }
    }

    var 
    bInit false;
    var 
    xMACD null;
    var 
    xMACDSig null;
    var 
    xMACDHist null;

    function 
    main(Fast,Slow,Smoothing,Source,Symbol,Interval,Params){

        if(
    bInit == false){
            if(
    Symbol == nullSymbol getSymbol();
            if(
    Interval == nullInterval getInterval();
            var 
    vSymbol Symbol+","+Interval;
            
    xMACD macd(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
            
    xMACDSig macdSignal(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
            
    xMACDHist macdHist(Fast,Slow,Smoothing,eval(Source)(sym(vSymbol)));
            
    addBand(0,PS_SOLID,1,Color.black,"Centerline");
            
    addBand(2,PS_SOLID,2,Color.RGB(0,208,0),"C1");// --ADDED CODE
            
    addBand(1,PS_SOLID,2,Color.RGB(0,208,0),"C2");// --ADDED CODE
            
    addBand(-1,PS_SOLID,2,Color.RGB(255,165,181),"C3");// --ADDED CODE
            
    addBand(-2,PS_SOLID,2,Color.RGB(255,165,181),"C4");// --ADDED CODE
            
    setShowTitleParameters(eval(Params));
            
    bInit true;
        }
        
    //if(condition1)// --ADDED CODE
            
    setBarBgColorColor.RGB(204,255,204), 01); // --ADDED CODE
        //else if(condition2)// --ADDED CODE
            
    setBarBgColorColor.RGB(254,216,212), 1, -1, -); // --ADDED CODE
        //else if(condition3)// --ADDED CODE
            
    setBarBgColorColor.RGB(255,240,161), 21, -); // --ADDED CODE
        //JUST HAVE A DIFFERENT # FOR THE INDEX REFERENCE # FOR EACH "setBarBgColor(color, index, xmin, xmax)"
        //IF YOU RUN OUT OF RETURN INDEX #'S THEN JUST ADD A TEXT REF IN THE RETURN STATEMENT AND USE THE NEW INDEX #, i.e.,
        //        setBarBgColor( Color.RGB(255,240,161), 3, 4, 5 ); // --ADDED CODE
        //        var xtraIdx = 0;
        //    return new Array (getSeries(xMACD), getSeries(xMACDSig), getSeries(xMACDHist), xtraIdx+"");
        
    return new Array (getSeries(xMACD), getSeries(xMACDSig), getSeries(xMACDHist));} 
    Attached Files
    Last edited by waynecd; 11-01-2011, 09:25 PM.

    Comment


    • #3
      Originally posted by waynecd
      Here is one possible solution (though, as you stated, it only colors up to the current bar):

      Wayne

      Wow, thanks for taking the time to respond!

      So, the short answer is to use setBarBgColor(), but make sure to 1up the barindex value. Too bad it doesn't color past the current bar (as you mention), but I think I can live with that.

      Thanks again!
      Daniel
      eSignal File Share: http://share.esignal.com/groupcontents.jsp?groupid=1130

      Comment

      Working...
      X