stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

hold a value of an integer when a new tick comes up
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5056
Page 1 of 1
Author:  henkknot [ Tue Jan 24, 2017 8:06 pm ]
Post subject:  hold a value of an integer when a new tick comes up

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.
Author:  tomele [ Tue Jan 24, 2017 8:23 pm ]
Post subject:  hold a value of an integer when a new tick comes up

Hi.

Declare it as a static variable.
Author:  henkknot [ Wed Jan 25, 2017 8:15 am ]
Post subject:  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'. :clap:

Henk.
Author:  SteveHopwood [ Wed Jan 25, 2017 10:22 am ]
Post subject:  hold a value of an integer when a new tick comes up

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'. :clap:

Henk.
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.

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) )

:xm:
All times are UTC Page 1 of 1