Announcement

Collapse
No announcement yet.

Setting a Variable back to 0, after the script has loaded

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

  • Setting a Variable back to 0, after the script has loaded

    I have a variable that increments each time a condition is true
    When I load the script and the history is assessed the variable increments ok

    I want to set this variable back to zero once the script has loaded
    It will then increment from zero as the condition occurs from then on

    I've tried getBarState() to no avail

    Does anybody know of a method I can use to achieve this

    Thank you

  • #2
    PhilippeDavies/CharlieDenver
    Declare the variable globally (ie outside of any function) and then reset it to 0 at BARSTATE_ALLBARS at the beginning of the main function eg
    PHP Code:
    if(getBarState()==BARSTATE_ALLBARS){
        
    myVar 0;//this variable is global

    For more information on the getBarState() function see the related article in the EFS KnowledgeBase
    Alex


    Originally posted by CharlieDenver View Post
    I have a variable that increments each time a condition is true
    When I load the script and the history is assessed the variable increments ok

    I want to set this variable back to zero once the script has loaded
    It will then increment from zero as the condition occurs from then on

    I've tried getBarState() to no avail

    Does anybody know of a method I can use to achieve this

    Thank you

    Comment


    • #3
      Hi, y, I tried this but it just prints the next increment when the first new bar ends after the script has loaded
      i.e. myVar didn't reset



      var myVar=0;
      debugClear();


      function preMain() {
      setComputeOnClose();
      }

      function main() {

      if(getBarState()==BARSTATE_ALLBARS){
      myVar = 0;
      }


      debugPrintln("Variable "+myVar+" ,");


      if (close()>open()
      ) onAction1()
      else if (
      close()<=open()
      ) onAction2();

      }

      function postMain() {

      }
      function onAction1() {
      setPriceBarColor(Color.red);
      myVar++;
      };

      function onAction2() {
      setPriceBarColor(Color.green);
      myVar++;
      };

      Comment


      • #4
        PhilippeDavies
        As far as I can see the variable is resetting once the script is loaded.
        Change your debug statement to also include the current bar index and you will see that when you refresh the chart the variable will be 0 at the oldest bar index (which is when the script is loaded or reloaded). Then for comparison purpose remove (or comment out) the entire routine that resets the variable and refresh the chart and you will see that the variable is not reset in this case
        Alex


        Originally posted by CharlieDenver View Post
        Hi, y, I tried this but it just prints the next increment when the first new bar ends after the script has loaded
        i.e. myVar didn't reset



        var myVar=0;
        debugClear();


        function preMain() {
        setComputeOnClose();
        }

        function main() {

        if(getBarState()==BARSTATE_ALLBARS){
        myVar = 0;
        }


        debugPrintln("Variable "+myVar+" ,");


        if (close()>open()
        ) onAction1()
        else if (
        close()<=open()
        ) onAction2();

        }

        function postMain() {

        }
        function onAction1() {
        setPriceBarColor(Color.red);
        myVar++;
        };

        function onAction2() {
        setPriceBarColor(Color.green);
        myVar++;
        };

        Comment


        • #5
          Hi Alex,

          Thanks for taking the time to reply, I'm new to EFS, and haven't quite got my head round the process
          I added as you suggested

          debugPrintln("Variable "+myVar+" "+getCurrentBarIndex());

          I can see the variable and the bar index printed
          When I refresh the chart I only see the latest increment and -1, I don't see 0, 0

          I removed the setComputeOnClose() and I see bar index 0, but still myVar is not reset

          Phillip















          Phillip
          Last edited by CharlieDenver; 01-17-2014, 06:37 AM.

          Comment


          • #6
            Phillip
            As I explained in my previous post the study is loaded (or reloaded) on the oldest bar and NOT on the most recent one.
            After it is loaded (or reloaded) it iterates/executes through all the bars in the chart (once per bar on historical bars).
            If you review the suggestion I made earlier you will see that the variable is reset at the oldest bar index (if you have a dynamic time template that is probably going to be -299)
            Based on what you are saying you want the variable to remain at 0 until it reaches the most recent bar [which I reiterate is NOT when the study is loaded (or reloaded)]
            If you have removed the setComputeOnClose() statement then just create a condition that checks for bar index to be less than 0 or alternatively check for isLastBarOnChart() to be false) in which case you assign the value of 0 to your variable. This way as the formula iterates through all the historical bars it assigns a value of 0 to the variable
            Alex


            Originally posted by CharlieDenver View Post
            Hi Alex,

            Thanks for taking the time to reply, I'm new to EFS, and haven't quite got my head round the process
            I added as you suggested

            debugPrintln("Variable "+myVar+" "+getCurrentBarIndex());

            I can see the variable and the bar index printed
            When I refresh the chart I only see the latest increment and -1, I don't see 0, 0

            I removed the setComputeOnClose() and I see bar index 0, but still myVar is not reset

            Phillip


            Phillip

            Comment


            • #7
              Thank you Alex
              isLastBarOnChart() does the job, I understand now
              Phillip

              Comment


              • #8
                Phillip
                You are welcome
                Alex


                Originally posted by CharlieDenver View Post
                Thank you Alex
                isLastBarOnChart() does the job, I understand now
                Phillip

                Comment

                Working...
                X