Announcement

Collapse
No announcement yet.

not sure what's happening with getCurrentBarIndex in main()

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

  • not sure what's happening with getCurrentBarIndex in main()

    hello all,

    in the following example, "hello 0" prints once as expected.
    however, if i remove the /* */ commented section, "hello 0" prints 6 times (i'm executing from a daily chart).

    also, the commented section is not working. it's supposed to print the last n closes (2 in this example).

    thank you in advance,
    phil

    -------------------------

    function preMain() {
    setPriceStudy(true);
    setStudyTitle("test");
    debugClear();
    }

    function main() {
    var indcurr = getCurrentBarIndex();
    if (indcurr < 0) {
    return;
    }
    debugPrintln("hi "+indcurr)
    /*
    var vC = getValueAbsolute("Close", 0, 2, "IBM,D");
    for (i = 1; i <= 2; ++i) {
    debugPrintln("i="+i+" indcurr="+indcurr + " close="+vC(-i));
    }
    */
    return;
    }

  • #2
    Hi,

    I assume you mean "hi 0" prints...

    Given the below code, "hi 0" will print once for each tick that comes in for the current bar. Could be once (and will be only once if historical data and markets are closed) could be many many times. All depends on how active the symbol is you are looking at.

    Could this explain what you are seeing, or am I missing your point?

    Garth
    Garth

    Comment


    • #3
      It was after the market closed and there were no more ticks.

      Without the /* */ portion, it only prints once, as expected.

      When the comment section is removed, that's when it prints 6 times. I don't understand why it would do that.

      Thanks

      Comment


      • #4
        What symbol and interval were u using?

        Garth
        Garth

        Comment


        • #5
          Hello Phil,

          I made a few corrections to your code. It should work as you intended now. As Garth was pointing out to you, main() gets called every time there is a trade. That's why your "hi, 0" was getting printed multiple times. You'll need to add some code logic around that statement if you want that to print only once. One way to do that is to check for a bar state of BARSTATE_NEWBAR. You can see an example of that below.

          The second problem was your use of getValueAbsolute(). With the bar offset of 0 and number of bars at 2. It was trying to give you the current price and the close for tomorrow, which is invalid. So vC was being returned as null. Think of the offset as the starting bar and the number of bars as the direction to go get your array of data. Notice I changed the number of bars to -2. Now getValueAbsolute() will give you the current price (index of 0) and then yesterday's close for IBM. You now have an array that looks like this:

          vC[0] = today's close
          vC[1] = yesterday's close

          Also notice that I changed your for loop. Arrays are 0 based so now you for loop will loop twice and i will equal 0 the first time, and 1 the second, which corresponds to the numbers of the array elements above. To refer to one of the array elements in your debugPrintln() statement, use vC[#] instead of vC(#). Hope this helps.

          PHP Code:
          function preMain() {
          setPriceStudy(true);
          setStudyTitle("test");
          debugClear();
          }

          function 
          main() {
              var 
          indcurr getCurrentBarIndex(); 
              if (
          indcurr 0) {
                  return;
              }
              
          //debugPrintln("hi, a trade just occured at bar index of: "+indcurr)

              
          var vC getValueAbsolute("Close"0, -2"IBM,D");
              
          //debugPrintln(vC);
              
          for (02; ++i) {
                  if (
          getBarState() == BARSTATE_NEWBAR) {
                      
          debugPrintln("i=" " indcurr="+indcurr " close= " vC[i]);
                  }
              }

          return;

          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Thanks. Dumb mistake. Using () instead of [] was causing it.

            Comment

            Working...
            X