EA Program Execution

 

I have been working on my first EA for a few weeks now, and from the start I wanted the EA to execute some code immediately after starting the MT4 program, before entering the start() function.

I thought that if I put the code in the init() function, that would work.

What I found was that, although the code would execute, calls to funtions for retrieving account information, like OrdersTotal(), returned values of 0 when this was not true. If I recompiled the EA, the same function calls would then return the correct values.

I'm not sure what to conclude from this but it looks like the init() function needs to complete once before account information functons will return the correct values.

So anyway, my question is, can any code somehow be executed after the init() function and before the start() function?

 

whay not add this

int start()

{

static bool bFirstTime = true;

if (bFirstTime)

{

...... execute 1st time commands

bFirstTime = false;

}

 

metatraders trade pool functions have proven to be problematic for me as well. i notice there are problems after compiling...especially when using many functions in your code. that is my major complaint with mt!

 

Wrong Conclusion

BC Brett:
I'm not sure what to conclude from this but it looks like the init() function needs to complete once before account information functons will return the correct values.

OK, I just ran another test to see if init() really did need to complete before the account functions would work or if it was just MT4 needed more time to finish its startup launch.

I added the statement sleep(1000) as the first line in the init() function, recompiled, shutdown MT4, and then restarted it. Now the account function calls in the init() funtion are returning the correct values.

Conclusion: MT4 starts running the EA before it finishes retrieving account information on startup. The sleep(1000) function gives it more time to complete downloading account information. But if anyone can answer my original question ...

BC Brett:
can any code somehow be executed after the init() function and before the start() function?
 

How about IndicatorCounted() == 0?

elihayun:
int start()

{

static bool bFirstTime = true;

if (bFirstTime)

{

...... execute 1st time commands

bFirstTime = false;

}

Very clever elihayun! I think this works also:

int start()

{

if (IndicatorCounted() == 0)

{

...... execute 1st time commands

}

The first time through start(), IndicatorCounted() == 0.

After the first time through, IndicatorCounted() == Bars-1.

 
BC Brett:
Very clever elihayun! I think this works also:

int start()

{

if (IndicatorCounted() == 0)

{

...... execute 1st time commands

}

The first time through start(), IndicatorCounted() == 0.

After the first time through, IndicatorCounted() == Bars-1.

It will work but you count on something the if your computer will be very very fast maybe it wont work. My solution is not relies on any assumptions

Reason: