Announcement

Collapse
No announcement yet.

more getBarState() confusion

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

  • more getBarState() confusion

    Greetings,

    I was hoping someone could help clear up a little confusion I'm having.

    Lets take the following snippet of code.

    PHP Code:

    function preMain()
    {

    }

    function 
    main()
    {
        if (
    getBarState() == BARSTATE_NEWBAR)
        {
            
    debugPrintln("hi new bar");
            return 
    close();
        }

    My understanding of the preceeding code is that in every new bar, a debug message will be printed for us, and the close() price will be plotted on our chart.

    So, after loading it on the chart, it seems to plot all of the previous bars correctly; But doesn't update the chart when new bars occur. The formula output box seems to still print our message when a new bar occurs though. Why doesn't our returned close() value get drawn on the chart?

    Is there something I'm missing? There must be...

    Thanks in advance,
    Joshua C. Bergeron
    Last edited by jbergeron; 02-10-2003, 03:19 PM.

  • #2
    I, too, have noticed this behavior. I think, correct me if I'm wrong, that the BARSTATE_NEWBAR will only be true at the precise time of the transition to a new interval, i.e. 0 s and 30 s on a 30 s timeframe. If, however, the tick (trade) occurs outside this time, even though it is actually on a new bar, the condition will not evaluate to true.

    -CB

    Comment


    • #3
      here is how I think getBarState() works



      watch es h3 after hours on a 1 min chart to get a good idea.

      Comment


      • #4
        Interesting. Thanks for the information you guys. Another question for you now

        So what then, is the proper way to actually have things process at the beginning of the bar, exactly?

        Also, in the example below, why would it print the debug output, but not draw the line on the chart?


        Thanks,
        Joshua C. Bergeron

        Comment


        • #5
          [So, after loading it on the chart, it seems to plot all of the previous bars correctly; But doesn't update the chart when new bars occur. The formula output box seems to still print our message when a new bar occurs though. Why doesn't our returned price() value get drawn on the chart?
          If the debug statement prints, then the close() value should be reflected on the chart. I have never seen a case where this hasn't been true, but I also don't have many formula's that only plot on NEWBARS, though I have many that only do calculations (or parts of calculations) on NEWBARS

          If, however, the tick (trade) occurs outside this time, even though it is actually on a new bar, the condition will not evaluate to true.
          This is not how it is supposed to work. The state of newbar will be saved until the next tick comes in. Since ticks don't always come in for each bar during off hours, you will sometimes see plots getting skewed during the night sessions. This is a known issue for some releases (maybe all, I'm not sure if it was fixed yet or not).

          I will run the code you posted and let you know what I see come tuesday.
          Last edited by gspiker; 02-10-2003, 03:27 PM.
          Garth

          Comment


          • #6
            I dont know the answer to the first question, maybe a philosphy major will chime in.

            The second question confuses me, cu this is what I see...

            and the close() is there in the lower pain
            Attached Files

            Comment


            • #7
              Okay, well I didn't have a screenshot of my example from earlier today, (I think the problem seems to happen more during intraday). My after hours one seems to *almost* work.

              But if you look at the attachment (the strategy was loaded at 15:42PM) There are several bars where the value returned is <None>.

              I took the screenshot with my cursor on one of those particular bars so you could see. Why does this happen? (in case the attachment doesn't show up in the posting, go to http://www.w4ll.st/weird/weird.png

              This is the exact code that was running on the chart:

              PHP Code:
              function log_msg(Message)
              {
                  var 
              today = new Date;
                  
                  var 
              hours today.getHours();
                  var 
              minutes today.getMinutes();
                  var 
              seconds today.getSeconds();
                  
                  if (
              hours 10)
                      
              hours "0"+hours;
                  if (
              minutes 10)
                      
              minutes "0"+minutes;
                  if (
              seconds 10)
                      
              seconds "0"+seconds;
                      
                  var 
              now hours+":"+minutes+":"+seconds;
                  

                  
              debugPrintln(now+" / "+Message);
              }

              function 
              preMain()
              {


              }

              function 
              main()
              {
                  if (
              getBarState() == BARSTATE_NEWBAR)
                  {
                      
              log_msg("Hi, new bar!");
                      return 
              close();
                  }   


              Also, as long as we're talking about this stuff, I wanted to double check the following as well:

              PHP Code:
              ...

              if (
              getCurrentBarIndex() == 0)
              {
                  if (
              getBarState() == BARSTATE_NEWBAR)
                  {
                       return 
              close();
                  }

              This example should process on the bar, at every new bar (IE, won't process previous bars.) Correct?

              Anyway, this setup has the same problem as the top issue- I get bars that have <none> returned as the value.

              Any input / help / suggestions, very appreciated.

              I guess the reason I'm asking all this is because I am writing a strategy that will be run realtime during the day. It computes values on the tick, and on the bar, draws stuff on the background of the chart, sends orders out, etc. But lots of weird stuff has been going on.

              And even on a minimal script like the one pasted above, the problems persist for me. I have a feeling most of my problems are due to the same issue...

              Any input? Thanks again.

              Joshua C. Bergeron

              Comment


              • #8
                save close() as a variable and check to see if it is null for some reason. This will help to narrow down the issue:

                var nClose = close()
                if (nClose == null){
                debugPrintln(" Close returned null ");
                return;
                }
                Garth

                Comment


                • #9
                  I would add a 2nd statement to at least return something if the bar state was not a new bar. It could be that subsequent ticks on the same bar would result in the main() not returning anything, and the engine may take that as a null return and put up <none> as the last value returned.

                  PHP Code:
                  if (getBarState() == BARSTATE_NEWBAR)
                      {
                          
                  log_msg("Hi, new bar!");
                          return 
                  close();
                      } else {
                          return 
                  low()/2;
                      } 

                  Comment


                  • #10
                    Good idea Dion, but I might change it a bit. Create a global variable (nClose) and save the close() value there on each NEWBAR. Then have a single return outside of the conditional that is just return(nClose);

                    PHP Code:
                    var nClose;

                    main(){

                    if (
                    getBarState() == BARSTATE_NEWBAR)
                        {
                            
                    log_msg("Hi, new bar!");
                            
                    nClose close();
                        }

                    return(
                    nClose); 
                    Garth

                    Comment


                    • #11
                      Greetings,

                      Thanks for the input guys. Could you elaborate a little bit on what you mean DionLoy?

                      I went ahead and tried this:

                      PHP Code:
                      function main()
                      {
                          if (
                      getCurrentBarIndex() == 0)
                          {
                              if (
                      getBarState() == BARSTATE_NEWBAR)
                              {
                                  var 
                      close();
                                  if (
                      == null); 
                                  {
                                     
                      log_msg("broked: "+n);
                                     return;
                                  }
                                  
                      log_msg("new bar: "+n);
                                  return 
                      n;
                              }   
                          }

                      The results make my brain hurt. I get lines like this in the output box:

                      17:39:08 / broked: 837

                      By the way, I'm using 7.2 (Build 544)

                      Thanks,
                      Josh

                      Comment


                      • #12
                        I think what Dion was getting at was that for those bars that were not NEWBARS, it could be possible that the fall through on the program did the equivelent of a return(null); and it could be that the last bar plotted is the value that is getting returned.

                        Therefore, you will want to do some return for the fall through case (when the NEWBAR conditional isn't matched).

                        Dion's suggestion was a return value of low()/2, which would produce a result but one that was sufficently out of the normal range that it would be identifiable yet close enough to the range that it wouldn't skew the charts.

                        Mine is just to return the value you really want.

                        I would pick one of these, and try that. Also the fact that you are getting a null return from close (hence the "Broked" message) might be good to explore. You might try saving the getCurrentBarIndex() as a variable and print that in the "Broked" debug message.
                        Garth

                        Comment


                        • #13
                          Ahh, yes, I see.

                          It seems to be working okay now with the suggestions you made. It is charting correctly when returning the value of the global variable outside of the getBarState() call (and assigning it from within the call). Seems a bit quirky though? Maybe it's just me

                          Thanks for your help guys.

                          Joshua C. Bergeron

                          Comment

                          Working...
                          X