Weird error (#4105, #4055) occurred in 'Trade Context' error handling logic

 

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:

#include <TradeContext2.mqh>

//Variable for trade context
int              TCvalue;


int start()
{
    // My buy condition logic
    // ...
    // wait until the trade context is free and then occupy it (if an error occurs, leave it)
    TCvalue = GetTradeContext();
    Alert("TCvalue=",TCvalue);
    if (TCvalue < 0) 
       return(false); 
    // Refresh market info for sending orders
    RefreshRates();
    // open a position
    if(OrderSend(...) < 0) 
      { 
        Alert("Error opening position # ", GetLastError()); 
      }
    // set the trade context free
    RelTradeContext();
    return(0);
}

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

 
Does your Broker allow EAs ? or are you running this in the Strategy Tester ?
 
hftsang:


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

 
RaptorUK:

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 ?

I haven't re-checked this, but my comments in https://www.mql5.com/en/forum/135983 say that default values are fine in an include file. And, if they weren't, the code wouldn't compile; you wouldn't get as far as a run-time error.
 
  1. Your reported errors make no sense. Are you running an indicator? Indicators can not wait and can not trade.
  2. Did you install in \program files* on Vista/Win7 so it can't create the global variable save file?
  3. You can not have default values in #import functions. #include is the same as being in the source file.
 
RaptorUK:
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.

WHRoeder:
  1. Your reported errors make no sense. Are you running an indicator? Indicators can not wait and can not trade.
  2. Did you install in \program files* on Vista/Win7 so it can't create the global variable save file?
  3. 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.

 
hftsang:

Hi RaptorUK. Surely my broker allows EAs and I did not run it in the Strategy Tester.

https://www.mql5.com/en/forum/138371 not all Brokers allow EAs.
 
RaptorUK:
https://www.mql5.com/en/forum/138371 not all Brokers allow EAs.

But it's not my first EA to do auto trading in this platform. I've run many before.
 
hftsang:

But it's not my first EA to do auto trading in this platform. I've run many before.
Ah OK, you didn't mention that. It was just a thought. :-)
Reason: