Announcement

Collapse
No announcement yet.

Strange results when setcomputeonclose() is not set to true.

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

  • Strange results when setcomputeonclose() is not set to true.

    I have a formula that produces the right results when setComputeOnClose() is set to true, but generates unexpected numbers for the current bar and the bars forward when setComputeOnClose() is not included in preMain. In the latter case, reloading the formula will get the right results for historical bars up to the reloading point, but results for current and future bars after the reloading point will again be calculated incorrectly.

    I think it may have something to do with the fact that the global accumulative variable that I use in the formula is calculated with different functions (defined at the end of the same script) in different states, which can occur in the same bar.

    Has anyone met the same problem before? Thanks in advance for any suggestions of a solution or reference.

  • #2
    Rambo
    What you describe suggests that the issue is in the logic of the script however without seeing the code it is not possible to determine what the error is and offer a solution to correct it
    Post the complete code [or attach the formula] and someone may be able to assist you
    Alex

    Comment


    • #3
      codes

      Alex:
      Thanks for the reply. The following is a striped down version of the codes that generate the mentioned problem. Thank you.

      function preMain () {
      setPriceStudy(true);
      setShowTitleParameters(true);
      }

      var ubar;
      if (ubar==null){ubar=0;}

      function main() {


      if (ubar>100){ubar=0};
      if ( low()>low(-1 )){ubar1();}
      else if ( open()>open(-1 )){ubar2();}
      else if ( high()>high(-1 )){ubar3();}
      else if ( close()>close(-1 )){ubar4();}
      drawText(ubar+"", TopRow2, Color.RGB(255,0,0));

      return;

      }


      function ubar1()
      {
      ubar=ubar+1;
      }

      function ubar2()
      {
      ubar=ubar+1;
      }

      function ubar3()
      {
      ubar=ubar+1;
      }

      function ubar4()
      {
      ubar=ubar+1;
      }

      Comment


      • #4
        Rambo
        The reason you are getting different results in real time and after a reload is that in real time the conditional statements in the main function are evaluated on every tick and are calling the functions on every tick whereas on a reload they are being evaluated once per bar only [as the script runs through historical bars] and call the functions once per bar only.
        To avoid this you need to either add a setComputeOnClose() statement in the preMain() function or enclose all the conditional statements inside a BARSTATE_NEWBAR condition using the getBarState() function and shift all the bar indexes back by 1 (ie low() needs to be low(-1), etc).
        Alex

        Comment


        • #5
          Thanks

          Hi, Alex:
          Many thanks for your help.

          Rambo

          Comment


          • #6
            Rambo
            You are most welcome
            Alex

            Comment

            Working...
            X