Global vars in EA - good?

 

In my EA, I am examining MACDs for the last few bars.

Originally I had these defined within the start() area.

However, I have need to create a function that actually does the logic and places the order (Order_It)

In previous programming experience, it is not a good idea to use global variables. However, since the EA is executed once per tick, it seems that a few global MACDs might be beneficial rather than passing them all the time.

Where do I place the calculation and definitions if I want them to be global and available to the function as well?

In the init()?

 

G

Before worrying about globals, consider static vars that hold their value or better still, look at arrays

https://book.mql4.com/variables/arrays

Arrays are static by default, can be set up in the init() and hold their values across ticks and bars until you change them

Good luck

-BB-

 

no not inside any function()

place at outermost/top level outside of all functions - you then have EA global scoping

fwiw, parameter passing not gonna be overhead in the scheme of things.

I favor not using global scopped identifiers. But - is always going to be subjective I guess, but for sure 'data hiding' is neat idea allowing the black box type of coding... etc, etc...

Must consider just why you wish to make global, yes?

1. if thinking about size/speed implications, again - this not issue imho, maybe once upon a time but h/w so fast, yes?

2. if one function is gonna be using globals, why put global? eg, best practice [by some :] would consider globals as last resort placement.

if more than one function requires access well then, maybe a reason. But again... globals might get accessed by some code not even contemplated would be accessing - let the nightmares begin -lol

3. if interested in persistentcy ie, static attribute added to variable declaration statement, can put this in function() and you get a 'localized' scoping which retains last assigned value after function() exits so is available on next entry (ie, not stack based like normal function() declarations are)

Remember, declaring at EA global scope - you have achieved same idea as static as used in a function(), in other words not really need static attribute on EA global declarations.

4. effectively, 'localized' scoping could be seen as where the declaration occurs. If outermost/top level then read 'localized' as all EA code. If function() level via static type name ..; then read 'localized' as only function() code.

anyway, some thoughts for you...

 
BarrowBoy wrote >>

G

Before worrying about globals, consider static vars that hold their value or better still, look at arrays

https://book.mql4.com/variables/arrays

Arrays are static by default, can be set up in the init() and hold their values across ticks and bars until you change them

Good luck

-BB-

Well, I'll be bu**erd... (i gotta go get something very strong indeed... ;)

From a programming/compSci generic local function() perspective - I find this massively hard to believe outside of MQL paradigm.

This is worrying for me... lol - as a matter of course, I would expect transient function() declarations to be stack based

That has always been norm afaik.

Surely this is all based on MQL ???

Not expect C/C++/Pascal/... eg, any block structured language.

Please re-educate me then IF MQL is identical to all other block structured languages in this respect OR indeed, I need brain clean up myself - lol

2008.06.24 16:09:03 13512 GBPUSD,M1: removed
2008.06.24 16:09:03 13512 GBPUSD,M1: uninit reason 0
2008.06.24 16:09:03 13512 GBPUSD,M1: in array(), bOnceOnly, ia[0..4]=01234
2008.06.24 16:09:01 13512 GBPUSD,M1: in array(), !bOnceOnly, ia[0..4]=01234
2008.06.24 16:09:01 13512 GBPUSD,M1: loaded successfully

#property copyright ""
#property link      "https://forum.mql4.com/13512"

bool bOnceOnly=false;

int start()
{

    array();
    Sleep(2000);
    array();
    
    return(0);
}

//------------

void array()
{
    int i,ia[5];
    
    if(!bOnceOnly)
    {
        bOnceOnly = true;
        for(i=0;i<5;i++) ia[i] = i;
        Print("in array(), !bOnceOnly, ia[0..4]=", ia[0],ia[1],ia[2],ia[3],ia[4]);
    }        
    else
        Print("in array(), bOnceOnly, ia[0..4]=", ia[0],ia[1],ia[2],ia[3],ia[4]);
}
 

Appreciate the info.

This was the hardest part for me to understand and I want to make sure that I have it correctly:

The EA is fired off with every tick and goes through the start() function (and init(), etc) with each tick. (??)

I have programmed for 25 years without using globals (ok, once :-) ). The only reason I thought of it now is because of the one-time execution per tick.

I typically just pass what is needed from the main() to the various functions/procedures. For readability, I am considering naming the variables the same in the start() function and passing them to the Order_it function with the same names. Does that make anyone grimace?

 

g,

good for you dude! globals are the pits... but now 'n then I just give in to the dark side and use 'em...

but let's wait for reply to my code + results post.

may be just for arrays, but I still am all at sea here over BB's post. Have traced through the article and it's links and is what BB says BUT, I sure would like to hear this confirmed by support.

Please support would you make comment on all this.

 

I typically just pass what is needed from the main() to the various functions/procedures. For readability, I am considering naming the variables the same in the start() function and passing them to the Order_it function with the same names. Does that make anyone grimace?

Same names make sense imho, anyways - the locals are scoped to function() only so not 'seen' outside.

For me, I always use same names, that way I not have to be creative with names >1 time!

ticket=ticket=... so why have ticket1, ticket2,...

I just posted on a C programming forum about my 'issue' regards the MQL array concept and how C people see all this.

Is not biggie for many I guess, but I not understand this so must pursue.

 

Quite frankly I do not see the issue with globals. When used correctly it simplifies the logic of the program, makes it use less memory and speed up things by not requiring to recopy things to use them.

They just need a bit of discipline on part of the programmer.


IMBO: One should learn to use his tools and not be afraid of reluctant to use the best one for each situation. Lets keep religion out of programming.

 

Fine by me... if you consider disciplined use of globals what does it for you, great - that is totally your opinion and right, not bother me one bit.

Regarding religion - you are way off base here. Nothing to do at all with 'any particular brand of programming religion'.

Look, none of this is relevant.

If you took the time to completely read my post I really do want to know about this subject - and if for you, it is just good enough to accept, that is your right, yes?

Just as it is my right to ask questions and not blindly assume I know it all.

I have been made aware of something which for me needs answering, ok?

 

religion and politics are two reasons I program :-)

I stay out of both (no flames on responsibility here - just trying to move it along).

Am I right in saying:

The EA is fired off with every tick and goes through the start() function (and init(), etc) with each tick. (??)

 
gnovak wrote >>

religion and politics are two reasons I program :-)

I stay out of both (no flames on responsibility here - just trying to move it along).

Am I right in saying:

The EA is fired off with every tick and goes through the start() function (and init(), etc) with each tick. (??)

Thank you. All respect to you :o))

Partial right. Each data tick for the chart pair, EA start() is called.

Only start()

init() called when EA attached to chart or when chart environment alters eg, periodocity and also when recompile EA that attached to chart - have looksee at UninitializeReason()

maybe init() analogy is an .exe's 'once off' startup code (err, like the types I used to work with decades ago - lol)

data ticks are asynchronous regards clocktime - could be many or few milliseconds between, guess is function of FX trading...

maybe this helps?

not forget the mql4 book is good resource too!

guess I better re-read more often to refresh as obviously not 'with the program here'!

Reason: