Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 863 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Remember a Variable Between Ticks / Loops #482735
03/20/21 14:58
03/20/21 14:58
Joined: Mar 2021
Posts: 4
Metzenovich Offline OP
Guest
Metzenovich  Offline OP
Guest

Joined: Mar 2021
Posts: 4
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?

Re: Remember a Variable Between Ticks / Loops [Re: Metzenovich] #482736
03/20/21 15:37
03/20/21 15:37
Joined: May 2015
Posts: 390
Czech Republic
G
Grat Offline
Senior Member
Grat  Offline
Senior Member
G

Joined: May 2015
Posts: 390
Czech Republic
define in global?

Re: Remember a Variable Between Ticks / Loops [Re: Metzenovich] #482737
03/20/21 15:50
03/20/21 15:50
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Global variables or static variables

Re: Remember a Variable Between Ticks / Loops [Re: Metzenovich] #482741
03/22/21 06:38
03/22/21 06:38
Joined: Mar 2021
Posts: 4
Metzenovich Offline OP
Guest
Metzenovich  Offline OP
Guest

Joined: Mar 2021
Posts: 4
Thanks!

Removing the "if(is(INITRUN)){" part and just leaving the static variables worked.


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1