Announcement

Collapse
No announcement yet.

Limit plot to X number of bars back

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

  • Limit plot to X number of bars back

    Hi,

    The enclosed efs uses the DrawTextCtr variable to control the number of bars back used to plot the DrawTextRelative(...). This reduces overhead resources.
    It draws small colored squares at the bottom of the chart. I found this method via a forum search.

    Problem: Initially it plots as intended (LookBack of 300 bars back) but it soon starts to erase the plotted squares from left to right until only one square remains.

    I would like it to keep the last 300 squares so that as a new square is plotted, the square at the begining is erased (now 301 bars back).
    I'm sure it's a simple solution but it escapes me.

    Thanks in advance.

    Wayne

    PHP Code:
    debugClear();
    var 
    bInit false
    var 
    aFPArray = new Array(); 

    function 
    preMain() {
        
    setPriceStudy(true);
    var 
    x=0
        
    aFPArray[x] = new FunctionParameter"Interval0"FunctionParameter.STRING); 
        
    withaFPArray[x++] ) { 
            
    setName"Price Interval" ); 
            
    setDefault(  ); 
        }   
    }
    var 
    myStudy0 null
    var 
    myStudy1 null
    var 
    DrawTextCntr 0;
    var 
    LookBack 300;

    function 
    main(Interval0) { 

        var 
    myVar0,myVar1
        var 
    TempColor Color.white;


        if ( 
    bInit == false ) { 
            
    setDefaultBarFgColor(Color.blue0);
            
    setDefaultBarFgColor(Color.red1);
            if(
    Interval0 == nullInterval0 getInterval();
            if(
    myStudy0 == nullmyStudy0 sma9inv(Interval0) ); 
            
    myStudy0 getSeries(myStudy0); 
            if(
    myStudy1 == nullmyStudy1 sma13inv(Interval0) ); 
            
    myStudy1 getSeries(myStudy1); 
            
    bInit true
        }
        var 
    Close0 close(0);
        var 
    myVar0 myStudy0.getValue(0); 
        var 
    myVar1 myStudy1.getValue(0); 

        if(
    Close0 myVar0 && myVar0 myVar1TempColor Color.green;//Price is above MA0 & MA0 is above MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.red;//Price is above MA0 & MA0 is below MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.RGB(218,218,165);//Price is below MA1 & MA0 is below MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.RGB(255,230,195);//Price is below MA1 & MA0 is above MA1

        
    drawTextRelative(0,1,"n",TempColor,null,Text.VCENTER Text.RELATIVETOBOTTOM |Text.CENTER|Text.BOLD,"Wingdings",10,"n0"DrawTextCntr++);
        if (
    DrawTextCntr LookBackDrawTextCntr 0;

       return new Array( 
    myStudy0myStudy1 ); 

    Attached Files

  • #2
    Re: Limit plot to X number of bars back

    waynecd
    You are incrementing drawTextCntr on every tick whereas you should do it only once per bar
    Alex


    Originally posted by waynecd
    Hi,

    The enclosed efs uses the DrawTextCtr variable to control the number of bars back used to plot the DrawTextRelative(...). This reduces overhead resources.
    It draws small colored squares at the bottom of the chart. I found this method via a forum search.

    Problem: Initially it plots as intended (LookBack of 300 bars back) but it soon starts to erase the plotted squares from left to right until only one square remains.

    I would like it to keep the last 300 squares so that as a new square is plotted, the square at the begining is erased (now 301 bars back).
    I'm sure it's a simple solution but it escapes me.

    Thanks in advance.

    Wayne

    PHP Code:
    debugClear();
    var 
    bInit false
    var 
    aFPArray = new Array(); 

    function 
    preMain() {
        
    setPriceStudy(true);
    var 
    x=0
        
    aFPArray[x] = new FunctionParameter"Interval0"FunctionParameter.STRING); 
        
    withaFPArray[x++] ) { 
            
    setName"Price Interval" ); 
            
    setDefault(  ); 
        }   
    }
    var 
    myStudy0 null
    var 
    myStudy1 null
    var 
    DrawTextCntr 0;
    var 
    LookBack 300;

    function 
    main(Interval0) { 

        var 
    myVar0,myVar1
        var 
    TempColor Color.white;


        if ( 
    bInit == false ) { 
            
    setDefaultBarFgColor(Color.blue0);
            
    setDefaultBarFgColor(Color.red1);
            if(
    Interval0 == nullInterval0 getInterval();
            if(
    myStudy0 == nullmyStudy0 sma9inv(Interval0) ); 
            
    myStudy0 getSeries(myStudy0); 
            if(
    myStudy1 == nullmyStudy1 sma13inv(Interval0) ); 
            
    myStudy1 getSeries(myStudy1); 
            
    bInit true
        }
        var 
    Close0 close(0);
        var 
    myVar0 myStudy0.getValue(0); 
        var 
    myVar1 myStudy1.getValue(0); 

        if(
    Close0 myVar0 && myVar0 myVar1TempColor Color.green;//Price is above MA0 & MA0 is above MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.red;//Price is above MA0 & MA0 is below MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.RGB(218,218,165);//Price is below MA1 & MA0 is below MA1
        
    if(Close0 myVar0 && myVar0 myVar1TempColor Color.RGB(255,230,195);//Price is below MA1 & MA0 is above MA1

        
    drawTextRelative(0,1,"n",TempColor,null,Text.VCENTER Text.RELATIVETOBOTTOM |Text.CENTER|Text.BOLD,"Wingdings",10,"n0"DrawTextCntr++);
        if (
    DrawTextCntr LookBackDrawTextCntr 0;

       return new Array( 
    myStudy0myStudy1 ); 

    Comment


    • #3
      Alex,

      Thanks.

      I changed line 59 right above the return statement to:

      PHP Code:
          if(getBarState() == BARSTATE_NEWBAR) if (DrawTextCntr LookBackDrawTextCntr 0
      But it still does it.

      Is that what you meant?

      Wayne

      Comment


      • #4
        waynecd
        That is not what I said.
        You are incrementing the DrawTextCntr variable inside the drawTextRelative() command which is executed on every tick.
        You need to instead increment it inside a conditional statement that checks for BARSTATE_NEWBAR (same as the one you show in your example). Then inside the drawTextRelative() command you just use the DrawTextCntr variable
        Inside that same BARSTATE_NEWBAR conditional statement you can then also have the lines of code shown in your example since they only need to be evaluated/executed once per bar
        Alex


        Originally posted by waynecd
        Alex,

        Thanks.

        I changed line 59 right above the return statement to:

        PHP Code:
            if(getBarState() == BARSTATE_NEWBAR) if (DrawTextCntr LookBackDrawTextCntr 0
        But it still does it.

        Is that what you meant?

        Wayne

        Comment


        • #5
          Alex,

          That worked perfectly. From your last explanation I see how computing on every tick can affect some of my other scripts.

          I really appreciate the explanation.

          Wayne

          Comment


          • #6
            Wayne
            You are welcome
            Alex


            Originally posted by waynecd
            Alex,

            That worked perfectly. From your last explanation I see how computing on every tick can affect some of my other scripts.

            I really appreciate the explanation.

            Wayne

            Comment

            Working...
            X