Announcement

Collapse
No announcement yet.

Limiting a function to the last bar only

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

  • Limiting a function to the last bar only

    If I wanted to apply a function to only the last bar of a chart , how should I go about that?

  • #2
    Using the code below, when first loaded vClose for the last bar is displayed and not any previous bars (which is what I want). However when the chart moves on to the next bar the existing value of vClose remains for what is then the second to last bar. How can I get it to only display vClose for the last bar on the chart?


    Code:
    var vClose = Close(0);
    var nBarIndex;
    nBarIndex = getCurrentBarIndex();
    if (nBarIndex == 0) {
    return vClose;
    }

    Comment


    • #3
      See if:

      if (isLastBarOnChart() == true)
      {
      ...
      }

      does what you want.

      Comment


      • #4
        Thanks SteveH. This only does the same as the code I used though - i.e. the plots formed from historical bars still remain.

        Originally posted by SteveH
        See if:

        if (isLastBarOnChart() == true)
        {
        ...
        }

        does what you want.

        Comment


        • #5
          Hi Wat Tyler,

          I don't know if this is what you need but I think it might or at least a variation of it.

          PHP Code:
          var nState null;
          function 
          main() { 
              var 
          nState
              
          nState getBarState(); 
              if (
          nState == BARSTATE_NEWBAR) { 
              var 
          vClose Close(0);
              return 
          vClose;
          } else{
               
          vClose 0;
              return;

          wayne

          Comment


          • #6
            Wat,

            The simple answer is you can't. What you can do however is modify the value of the study as returned for previous bars.

            So, using waynecd's code (which unfortnately won't quite do what you require) you would use the 'setBar' command as per this example to set the return value of the study, in this case close, to 0 as the study moves from left to right through the historical data.

            Then, moving forwards, as each new bar starts on the right hand side of the chart the return value of the study for the previous bar will be set to 0.

            PHP Code:
            var nState null;
            function 
            main() {
                
            nState getBarState();
                if (
            nState == BARSTATE_NEWBAR) {
                    
            vClose 0;
                    
            setBar(Bar.Value, -10); 
                }
                else {
                    var 
            vClose close(0);
                }
                
                return 
            vClose;

            HTH.

            Regards
            Last edited by sandpiper; 05-22-2009, 03:10 PM.

            Comment


            • #7
              Many thanks Sandpiper and Waynecd.
              The setBar() function looks like it will deal with the issue as you've indicated.

              Wat

              Comment

              Working...
              X