Announcement

Collapse
No announcement yet.

why setbar did not work as I expected

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

  • why setbar did not work as I expected

    I am trying to use setBar() to change the indicator values of previous 50 bars when getCurrentBarIndex() == -20, however it seems setBar() did not work as I expected. I am expecting the test script shows a black line segment between bar -21 and -70, what was wrong?

    - Clearpicks

    var returnArray;
    function preMain()
    {
    setPriceStudy(true);
    setStudyTitle("TestSetBar");
    }

    function main()
    {
    if ( getBarState() == BARSTATE_ALLBARS ) {
    returnArray = new Array(1);
    returnArray[0] = close(0);
    setDefaultBarThickness(4, 0);
    setDefaultBarStyle(PS_DASH, 0);
    setPlotType(PLOTTYPE_FLATLINES, 0);
    setDefaultBarFgColor(Color.lime, 0);
    }
    if ( getCurrentBarIndex() == -20 ) {
    var highColor = Color.RGB(255, 153, 0);
    returnArray[1] = close(0);
    setDefaultBarThickness(4, 1);
    setDefaultBarStyle(PS_DASH, 1);
    setPlotType(PLOTTYPE_FLATLINES, 1);
    setDefaultBarFgColor(highColor, 1);

    var j;
    for ( j = 1; j < 50; j ++ ) {
    setBar(Bar.Value, -j, 1, returnArray[1] );
    setBar(Bar.FgColor, -j, 1, Color.black );
    setBar(Bar.Thickness, -j, 1, 4 );
    setBar(Bar.Style, -j, 1, PLOTTYPE_FLATLINES );
    }
    }

    return returnArray;
    }
    Attached Files

  • #2
    Re: why setbar did not work as I expected

    clearpicks
    You are setting the array length incorrectly ie
    returnArray = new Array(1);
    whereas it should be
    returnArray = new Array(2);
    Once you change that it will work as intended (see screenshot)
    Alex




    Originally posted by clearpicks
    I am trying to use setBar() to change the indicator values of previous 50 bars when getCurrentBarIndex() == -20, however it seems setBar() did not work as I expected. I am expecting the test script shows a black line segment between bar -21 and -70, what was wrong?

    - Clearpicks

    var returnArray;
    function preMain()
    {
    setPriceStudy(true);
    setStudyTitle("TestSetBar");
    }

    function main()
    {
    if ( getBarState() == BARSTATE_ALLBARS ) {
    returnArray = new Array(1);
    returnArray[0] = close(0);
    setDefaultBarThickness(4, 0);
    setDefaultBarStyle(PS_DASH, 0);
    setPlotType(PLOTTYPE_FLATLINES, 0);
    setDefaultBarFgColor(Color.lime, 0);
    }
    if ( getCurrentBarIndex() == -20 ) {
    var highColor = Color.RGB(255, 153, 0);
    returnArray[1] = close(0);
    setDefaultBarThickness(4, 1);
    setDefaultBarStyle(PS_DASH, 1);
    setPlotType(PLOTTYPE_FLATLINES, 1);
    setDefaultBarFgColor(highColor, 1);

    var j;
    for ( j = 1; j < 50; j ++ ) {
    setBar(Bar.Value, -j, 1, returnArray[1] );
    setBar(Bar.FgColor, -j, 1, Color.black );
    setBar(Bar.Thickness, -j, 1, 4 );
    setBar(Bar.Style, -j, 1, PLOTTYPE_FLATLINES );
    }
    }

    return returnArray;
    }

    Comment


    • #3
      Does it mean the number of indicator lines has to be determined before the first time returnArray is returned?

      Comment


      • #4
        clearpicks
        To my knowledge you need to return an array of the appropriate length to use setBar() to adjust the value and/or properties of any returned item beyond the first one. In your script the returned array has a length of 1 up to bar index -20 so prior to that bar index you can't adjust the properties of its 2nd element as it does not exist
        Alex


        Originally posted by clearpicks
        Does it mean the number of indicator lines has to be determined before the first time returnArray is returned?

        Comment

        Working...
        X