Announcement

Collapse
No announcement yet.

Problem with getBarStateInterval()?

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

  • Problem with getBarStateInterval()?

    I debugged a problem with my code which came down to the fact that getBarStateInterval() only returns correct results the first time it is called within an iteration of main().

    The code below (run on a daily chart) shows this. I would expect the output to be "ok1 ok2 ok1...". But instead only "ok1 ok1..." is output.

    I got around the problem by initially assigning the bar state value to a variable which is then used at several times in the code. But I thought I would pass this on in case it's a bug.

    function preMain() {

    setPriceStudy(false);
    setStudyTitle("Test2");
    setCursorLabelName("tst", 0);
    setComputeOnClose();

    }

    function main() {

    if (getBarStateInterval("D") == BARSTATE_NEWBAR) {debugPrintln("ok1")}
    if (getBarStateInterval("D") == BARSTATE_NEWBAR) {debugPrintln("ok2")}

    return null;
    }

    function postMain() {
    }

  • #2
    ieng
    You can have multiple getBarStateInterval() calls in a script (see this thread for an example) however they cannot be to the same interval.
    Alex

    Comment


    • #3
      ieng,

      This is because of the behavior of getBarStateInterval("D").


      getBarStateInterval() behaves similarly to getBarState() but behaves slightly different due to it's multi-time frame capacity. Specifically, getBarState() returns a value associated with "BARSTATE_ALLBARS" when the chart is initially loaded. However, getBarStateInterval() does not have an equivalent to a "BARSTATE_ALLBARS" value. For example, getBarStateInterval("D") is 'undefined' when the chart is initially loaded (I saw this when I loaded it into a 5 minute chart).

      I believe this matches up with what you saw and helps you on your code.

      Comment


      • #4
        I don't see how an undefined state during BARSTATE_ALLBARS for getBarStateInterval("D") explains why it does not work for subsequent bars.

        If the reason is as Alexis explained, and it's not a bug, then the behavior should be documented.

        Thanks for your help.

        Comment


        • #5
          ieng,

          It seems to me that both Alex and I were working on trying to help you answer your question at the same time and I was unaware of his post. As for my response, it looks like I misread your question (perhaps because of my poor eyesight). I just didn't see why anyone would ask for (getBarStateInterval("D") == BARSTATE_NEWBAR) twice in a row like you did.
          Originally posted by ieng
          I don't see how an undefined state during BARSTATE_ALLBARS for getBarStateInterval("D") explains why it does not work for subsequent bars.

          If the reason is as Alexis explained, and it's not a bug, then the behavior should be documented.

          Thanks for your help.

          Comment

          Working...
          X