Remember a Variable Between Ticks / Loops

Posted By: Metzenovich

Remember a Variable Between Ticks / Loops - 03/20/21 14:58

Hi,

How can I make a variable persist between run() loops?

What I want to accomplish is keeping track of the movement of an oscillating indicator.

For instance, when it crosses from below 80 to above 80 (potential long) or above -80 to below -80 (potential short), I want to be able to track previous one so I know whether it's just bouncing around at the top or bottom (ranging), or if it has actually gone from the bottom to the top (a bigger reversal).

I tried this:
Code
function run(){
  if(is(INITRUN)){
    static string myStatus = 'none';
  }

  if(myStatus != 'none'){
    // do something
  }

  if(price() > 1.234){
    myStatus = 'triggered';
  }

}


But it errors out on the "myStatus != 'none'" line. So either the "static string myStatus = 'none'" isn't ever getting run, or it isn't persisting between loops.

I thought of using setvar() and getvar() to store it in a file, but that seems like it would be pretty inefficient.

Another possibility is if there is a built-in global variable that is for something I'm not using that won't affect anything and storing it in that (whether it's a string, int, etc).

What is the correct / best way to accomplish this?
Posted By: Grat

Re: Remember a Variable Between Ticks / Loops - 03/20/21 15:37

define in global?
Posted By: AndrewAMD

Re: Remember a Variable Between Ticks / Loops - 03/20/21 15:50

Global variables or static variables
Posted By: Metzenovich

Re: Remember a Variable Between Ticks / Loops - 03/22/21 06:38

Thanks!

Removing the "if(is(INITRUN)){" part and just leaving the static variables worked.
© 2024 lite-C Forums