Hello,
Maybe this is a stupid question, a beginners mistake maybe. I try to write an EA. In the first step I want to set an integer. How can I hold this integer when a new tick comes up? I only want to let change the value of this integer by run an if-function. Is this possible?
thanks.
hold a value of an integer when a new tick comes up
-
henkknot
- Trader
- Posts: 11
- Joined: Mon Jul 25, 2016 11:16 am
- tomele
- Administrator
- Posts: 1208
- Joined: Tue May 17, 2016 3:40 pm
- Location: Germany, Forest of Odes, Defending the Limes
hold a value of an integer when a new tick comes up
Hi.
Declare it as a static variable.
Declare it as a static variable.
Happy pippin, Thomas 
It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so. (Mark Twain)
Keep the coder going: Donate
It ain't what you don't know that gets you into trouble.
It's what you know for sure that just ain't so. (Mark Twain)
Keep the coder going: Donate
-
henkknot
- Trader
- Posts: 11
- Joined: Mon Jul 25, 2016 11:16 am
hold a value of an integer when a new tick comes up
Thanks Tomele for your advice. I searched the forum for "static variable" and found a lesson from Steve who said "Globally declared variables retain their value until changed in code or by restart."
That's my solution I think.
Thanks a lot for your help! Its very nice to be able to rely on so much knowledge'.
Henk.
That's my solution I think.
Thanks a lot for your help! Its very nice to be able to rely on so much knowledge'.
Henk.
- SteveHopwood
- Owner
- Posts: 9904
- Joined: Tue Nov 15, 2011 8:43 am
- Location: Misterton - an insignificant village in England. Very pleasant to live in.
hold a value of an integer when a new tick comes up
Yes. If you need a variable to be used in a number of functions then you declare it outside of a function area. I declare mine at the top of my code so I know where to look for it, but it can go anywhere so long as it is not inside a function.henkknot » Wed Jan 25, 2017 8:15 am wrote:Thanks Tomele for your advice. I searched the forum for "static variable" and found a lesson from Steve who said "Globally declared variables retain their value until changed in code or by restart."
That's my solution I think.
Thanks a lot for your help! Its very nice to be able to rely on so much knowledge'.![]()
Henk.
You declare a variable as static if you only need it for the individual function but it needs to retain its value from tick to tick. Here is the sort of construct I use over and over again when an indi only needs reading at the open of each new candle:
Code: Select all
static datetime OldHtfHgiTime = 0;//Declare a variable that holds its value from tick to tick
if (OldHtfHgiTime != iTime(Symbol(), TrendTimeFrame, 0) )//Only read the indi at the open of the new candle
{
OldHtfHgiTime = iTime(Symbol(), TrendTimeFrame, 0);//Resets OldHtfHgiTime to the start time of current candle
//Code to read the indi
}//if (OldHtfHgiTime != iTime(Symbol(), TrendTimeFrame, 0) )
Read the effing manual, ok?
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.
Afterprime is the official SHF broker. Read about them at https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?p=175790#p175790.
I still suffer from OCCD. Good thing, really.
Anyone here feeling generous? My paypal account is always in the market for a tiny donation. pianodoodler@hotmail.com is the account.
To see The Weekly Roundup of stuff you guys might have missed Click here
My special thanks to Thomas (tomele) for all the incredible work he does here.