hold a value of an integer when a new tick comes up

Ask your basic questions confidently here. Flaming is not allowed. Sympathetic help is the order of the day.
Post Reply
henkknot
Trader
Posts: 11
Joined: Mon Jul 25, 2016 11:16 am

hold a value of an integer when a new tick comes up

Post by henkknot »

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.
User avatar
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

Post by tomele »

Hi.

Declare it as a static variable.
Happy pippin, Thomas :-BD

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

Post by henkknot »

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.
User avatar
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

Post by SteveHopwood »

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:
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.
Post Reply

Return to “EA's for Dummies.”