Fixing the ''Error 146 Trade context busy' not working' code problems

 

I'm trying to implement code to fix the ''Error 146 Trade context busy' not working code problems'


If I add this line to the start of the TradeContext.MQ4 file int init() I get the follwong error:

'init' - no dll defined for the imported function


If I 'rem it out' I get the following errors:

Start function not found and cannot be run.

Function "_IsTradeAllowed" is not referenced and will be removed from exp-file

Function "TradeIsBusy" is not referenced and will be removed from exp-file

Function "TradeIsNotBusy" is not referenced and will be removed from exp-file


Thanks to any and all for any assistance (< 8)

Files:
 

Hi Doug

It looks as though you are trying to compile the file, but it isn't intended to be compiled on its own. It is intended to be included in the file of an EA using the #include directive in the EA file and it gets compiled as part of the EA. Then you don't need to alter TradeContext.mq4 to get it to complie and the functions defined within it become available for use within the EA. Section 7 of the article shows how to do this.

Cheers

Jellybean

 

one solution is:

1. remove or comment out lines 8,9,29

2. File -> Save As: TradeContext.mqh in directory ...\experts\include

3. in any .mq4 file which needs these three functions, put at the top of the .mq4 source file "#include <TradeContext.mqh>"

another solution is:

1. take the three functions via copy and paste into your .mq4 source file which needs their functionality. Let's say you paste at the bottom of the .mq4 file...

2. now they are considered local functions to the .mq4 source file... just like init(),deinit() and start() are. Remembering that the function is at the source files TOP level ie, global scope.

hth


the aim, assuming I read your need correctly, is to allow reference to these three functions. either of above will work.

 
Jellybean:

Hi Doug

It looks as though you are trying to compile the file, but it isn't intended to be compiled on its own. It is intended to be included in the file of an EA using the #include directive in the EA file and it gets compiled as part of the EA. Then you don't need to alter TradeContext.mq4 to get it to complie and the functions defined within it become available for use within the EA. Section 7 of the article shows how to do this.

Cheers

Jellybean

Hi JB,

I understand what you are saying and the concept, however there is a small mq4/ex4 that is meant to be included with this that is integrated into the applicable programs that is used to call this module / subprogram when it is needed. This comes from an article on MQL4 and as such it is quite relevant: https://www.mql5.com/en/articles/1412

It originated in Russin and thus unfortunately for me all of the documentation and comments in the coding examples in it are all in Russian.

I've been sleeping with a book on learning Russian under my pillow for quite some time now, I don't understand it hasn't, but so far it hasn't helped me much LoL (< 8)

.

I'm a big fan of 'modular' programming as once this subroutine is coded it can be called and used from and with any program that has need of it. Building up a library of such things obviously can and does make supsequent programming much quicker and easier as one isn't spending time 're-inventing the same wheel' every time.

.

I'm also cognizant of the following information from the Documentation section from here on MQL4 as the program that I want to use it for and some others I have in mind, speed and minimum CPU utilization, load and overhead etc is going to be an important factor. The following is from the first page ot the 'Documentation' section here:

.

Included file is a source text of the most frequently used blocks of custom programs. Such files can be included into the source texts of experts, scripts, custom indicators, and libraries at the compiling stage. The use of included files is more preferable than the use of libraries because of additional burden occurring at calling library functions. Included files are recommended to be stored in terminal_directory\experts\include.

.

I'm not 'there yet' but thanks for responding and for the assistance.

 
fbj:

one solution is:

1. remove or comment out lines 8,9,29

2. File -> Save As: TradeContext.mqh in directory ...\experts\include

3. in any .mq4 file which needs these three functions, put at the top of the .mq4 source file "#include <TradeContext.mqh>"

another solution is:

1. take the three functions via copy and paste into your .mq4 source file which needs their functionality. Let's say you paste at the bottom of the .mq4 file...

2. now they are considered local functions to the .mq4 source file... just like init(),deinit() and start() are. Remembering that the function is at the source files TOP level ie, global scope.

hth


the aim, assuming I read your need correctly, is to allow reference to these three functions. either of above will work.

Hi FBJ,

I believe that you have correct take on this: Please see my reply to JB which is relevant to this.

I inittialy started off with the intention of makig this into an *.mqh file, but I am very new to all of this and the article that I got this from here: https://www.mql5.com/en/articles/1412 and was working from seemed to be saying that this program was intended to be an EX4 file as well, though with the minimal knowledge that I currently posess of such things it wouldn't take much for me to misundersstand it and get confused about it. I understand what you are saying and it was my feeling as well that it should be an 'included' file and as such compiled into an *.mqh file.


I'll give this a try FBJ.

Thanks for responding and your assistance with this.


UpDate:

Working with this In the 'ProcessInitiate.mq4' file which is the one that is meant to be integrated into a program to include/call the ''Error 146 Trade context busy' not working code problems' module/(sub)routine, it refers to: #include <TradeContext.mq4> and NOT an mqh file, so I don't know what to do with it now?

In your line numbers that you are saying to 'rem out' FBJ, I'm not sure where you are starting from, if you are including the 'remark', empty lines etc.

Would you mind annotating the appropriate lines and posting them here or better yet in the MQ4 file, just 'rem them out' and put your initials after the // and then uplod it back to here.

I have uploaded the 'Process Initiate.mq4' file which is the ''interface' program' When I try to compile it, understandably it says that it 'cannot open the "TradeContext.mq4" file' which currently has 4 errors in it.


.

Thanks again,

Doug

Files:
 
As per your suggestions, in spite of the article refering to it as an *.mq4 file, when i make it into and MQH file and then try to compile the ProcessInitiate.MQ4, understandably it comes back with an error everywhere were there are the 3 periods in a row. I'll have a go at embedding the commands of the Process Initiate file into a program as it's meant to be and see how that goes. Actually I already know how it is going to go as over and above my almost total ignorance of MQL, the one I am trying to 'meld' it with isn't using 'normal' straght forward Order commands and techniques.
But they call them 'learing curves' with very good reason!

Thanks again . (< 8)

. . "Once more unto thy breach!" .
 

Doug,


I think you were making this more difficult than it actually is. I've edited the files for you so that you get a clean compile and I've annotated ProcessInitiate for you since this will become your EA and you need to see how to integrate the TradeContext logic clearly.


Place TradeContextCB.mqh in the experts/include folder.

Place ProcessInitiateCB.mq4 in the experts folder.

Compile ProcessInitiateCB.mq4

Then add your own logic to ProcessInitiateCB.mq4 bit by bit and see how you go.


NOTES:

- Header files don't NEED to be suffixed .mqh - its just good practice. Experiment and see for yourself.

- If you want to keep all your headers in the experts folder rather than experts/include, then use "filename" rather than <filename> in your include statement.

- Content of headers is placed at the top of your .mq4 at compile time - so remember the rules of declaring variables before the function which accesses them.


Happy Christmas.


CB

 

Hi CB,

Thanks for the help and the info. Now I just have to learn and figure out what a 'Header' is in this context.

You are right, like most folks new to anything, they genreally work twice as hard and take at least twice as long to produce inferior results. Alas it comes with the territory. As I said above, they don't call them ''learning curves' (or mountains)' for nothing.

The 'Learn Russian' book that I have had under my pillow for quite some time now doesn't seem to be helping me much with that, so I think I'll swap it for the MQL4 Book instead (< 8)


. . . . (8 >) . Have a good ChristMass EveryOne . (< 8)


 
DougRH4x:

Hi CB,

Thanks for the help and the info. Now I just have to learn and figure out what a 'Header' is in this context.

You are right, like most folks new to anything, they genreally work twice as hard and take at least twice as long to produce inferior results. Alas it comes with the territory. As I said above, they don't call them ''learning curves' (or mountains)' for nothing.

The 'Learn Russian' book that I have had under my pillow for quite some time now doesn't seem to be helping me much with that, so I think I'll swap it for the MQL4 Book instead (< 8)


. . . . (8 >) . Have a good ChristMass EveryOne . (< 8)


Header files are simply an aid for programmers to administer their source code. You'll appreciate their use in the same way as you've already mentioned sub-routines (services in today's speak). At compile time all the source code in the header file is inserted at the top (head) of the main source code and the whole thing is compiled as for a single program. So if you've lots of EAs which all use the same function (or the same chunk of variables), you can avoid having to copy and paste their entirety into every EA.


CB

 
cloudbreaker:

Header files are simply an aid for programmers to administer their source code. You'll appreciate their use in the same way as you've already mentioned sub-routines (services in today's speak). At compile time all the source code in the header file is inserted at the top (head) of the main source code and the whole thing is compiled as for a single program. So if you've lots of EAs which all use the same function (or the same chunk of variables), you can avoid having to copy and paste their entirety into every EA.


CB

Hi CB,

So these are actual distinct, separate sections of: code/program/services modules/mqh files as opposed to being the name at the start of a specific section in a program? Example: a segment of the program that decides on placing an order or not and then places it? It does sound very much like ‘Included MQH’ files. You say that they are placed at the top of the program (automatically) when the main program is complied. I’m wondering how they get placed in the proper part of the program for the logic flow? I imagine they are defined as variables/functions and then get called and used at any place in the program that is using them. Once they get called and included they then would perform and be a specific, dedicated ‘section’ of code with the ‘header’ name at the start of it as I initially imagined them to be.

The mqh is optional? So they are basically text files with no suffix and none of the hidden control codes etc that advanced word processors use and would just call them something like “PlaceOrder” I can see the wisdom in using an mqh suffix as it clarifies there definition and purpose and what they are applicable to and part of, which might not be obvious at a much later date and the file isn’t in our ‘immediate memory banks’ anymore; at least for me anyway.

The complete code of the header &/or mqh files gets included when the program is complied as opposed to just the filename being included and the program then looking at the specific included file and running the code from the ‘include/header’ file by calling it when the main program needs it? Does this hold true for both included files AND mqh files ? As there is a different name and ‘definition’ of them, I’m wondering just what the difference between an ‘mqh’ file and a ‘header’ file is ?

Thanks again CB,

Doug

 
Reason: