1) Copy the source codes in the above link and complied it as a mqh named TradeContext2.mqh and placed in include file folder.
I think I read something about not being able to have default values in Function declarations in pre-compiled included files . . . . did I remember correctly ?
int GetTradeContext(int MaxWaiting_sec = 30) // <---
From here: https://docs.mql4.com/basis/preprosessor/import
"Since the imported functions are out of the module to be compiled, the compiler cannot check correctness of parameters passed. This is why, to avoid runtime errors, it is necessary to declare the compliance of types and order of parameters precisely. The parameters passed to imported functions (both from EX4 and from DLL modules) cannot have values by default. "
I think I read something about not being able to have default values in Function declarations in pre-compiled included files . . . . did I remember correctly ?
- Your reported errors make no sense. Are you running an indicator? Indicators can not wait and can not trade.
- Did you install in \program files* on Vista/Win7 so it can't create the global variable save file?
- You can not have default values in #import functions. #include is the same as being in the source file.
Does your Broker allow EAs ? or are you running this in the Strategy Tester ?
Hi RaptorUK. Surely my broker allows EAs and I did not run it in the Strategy Tester.
- Your reported errors make no sense. Are you running an indicator? Indicators can not wait and can not trade.
- Did you install in \program files* on Vista/Win7 so it can't create the global variable save file?
- You can not have default values in #import functions. #include is the same as being in the source file.
Hi WHRoder, so happy to have your reply! For your questions, (1) I didn't run it as indicator. (2) My OS is still WinXP and MT4 was installed in WinXP\Program files.
First of all, was I correct to put your source as mqh and compiled in \Include folder? I did have 3 warnings when compiling it:
1) Start function not found and cannot be run.
2) Function "GetTradeContext" is not referenced and will be removed from exp-file
3) Function "RelTradeContext" is not referenced and will be removed from exp-file
I will try to put your semaphore source code directly in the program to test again. If it's successful, possibly it's my mistake. But then how can I import this semaphore functionality to every of my EA without directly put the source codes inside?
Really strange to have such error codes indeed. From MQL4 reference, GlobalVariableSetOnCondition should produce ERR_STRING_PARAMETER_EXPECTED (4062) or ERR_GLOBAL_VARIABLE_NOT_FOUND (4058). I've checked to see whether what GetLastError() got was not the most recent error code at all but it was not possible, as the source code in the link clearly shows.
Hi RaptorUK. Surely my broker allows EAs and I did not run it in the Strategy Tester.
But it's not my first EA to do auto trading in this platform. I've run many before.
But it's not my first EA to do auto trading in this platform. I've run many before.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi all,
I've just started writing my own logic to handle error #146 'Trade Context is busy' based on some of the logics found here. However, I came across an error when the command GlobalVariableSetOnCondition was performed to change the value of the semaphore.
My message print out logged "GetTradeContext:GlobalVariableSetOnCondition(TradeIsBusy,1.0,0.0)-Error #4055" or "GetTradeContext:GlobalVariableSetOnCondition(TradeIsBusy,1.0,0.0)-Error #4105".
I guessed this was due to my poor logic so I gave up mine and tried to use existing logic given in https://www.mql5.com/en/forum/121625 to test it again. Same error occurred. My program and procedures were as follows:
1) Copy the source codes in the above link and complied it as a mqh named TradeContext2.mqh and placed in include file folder.
2) Amend my EA program accordingly as follows:
But my TCvalue was always = -2. Message "Waiting time (30 sec) exceeded!" was shown accordingly.
Appreciated if anyone can help on this. I supposed it's an easy logic to me with all fellow experienced programmers generously suggesting relevant sources here but so far I couldn't make my own semaphore. -_-