Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, The_Judge, Grant), 898 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Variable initialization confusion #469629
11/26/17 00:28
11/26/17 00:28
Joined: Nov 2016
Posts: 55
Taipei
ytc Offline OP
Junior Member
ytc  Offline OP
Junior Member

Joined: Nov 2016
Posts: 55
Taipei
I want to keep a variable's value to next call of run(), if I ran the code below for 2 bars period

Code:
function run(){
  //1. if(is(INITRUN)) int x=0;
  //2. if(is(FIRSTRUN)) int x=0;
  //3. int x=0;
  //4. static int x=0;
  
  printf("%i",x);
  x = 1;
  printf("%i",x);
}


#1, 2 and 3 got me 0101, implying x was set again at each bar run.
#4 got me 0111, which is what I want.

I thought if(is(INITRUN)) and if(is(FITSTRUN)) would only set variable x once.

Re: Variable initialization confusion [Re: ytc] #469631
11/26/17 12:37
11/26/17 12:37
Joined: Sep 2003
Posts: 928
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 928
For keeping the variable, it must be static. Here is it explained:

http://www.geeksforgeeks.org/static-variables-in-c/

Re: Variable initialization confusion [Re: Spirit] #469647
11/27/17 12:48
11/27/17 12:48
Joined: Nov 2016
Posts: 55
Taipei
ytc Offline OP
Junior Member
ytc  Offline OP
Junior Member

Joined: Nov 2016
Posts: 55
Taipei
Thanks, I just had some confusion about the function of INITRUN and FIRSTRUN when setting variables.


Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1