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

 

Thanks Phy.


CB

 
DougRH4x wrote >>

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)

Hi Doug

Note that the issues you mentioned in your 1st post is in fact not errors, but rather warnings.

If your compiler finds a function that is not used in your code you will get this message.

I use these functions "as is" from the original author. It is the perect solution for error 146. Since I have implemented it I have not had any 146 errors.

Steps to get it working:

1) Copy the files to your include folder

2) Copy the files to the header of your ea: //this is so your ea knows what you are talking about when you call these functions

#include <TradeIsBusy.mq4>
#include <TradeIsNotBusy.mq4>

3) Before ANY trade function e.g.OrderSend(); OrderClose(); OrderDelete(); OrderModify() place this line of code:

if(TradeIsBusy() < 0) return(-1);

4) Now place your trade function

5) Record outcome (or error)

6) Now release the context again

TradeIsNotBusy();

7) Now deal with any error

Works like a charm

A few lessons I learnt in the process of gettiing it working:

1) When the global variable is called for the first time is is not available. to overcome this:

put this in the deint() function:

int deinit()
{
//----
//make sute that this EA does not leave operations hanging when it closes by setting to 0
TradeIsNotBusy();

return(0);

}

Now just restart the ea.

2) I like to know what the ea is doing so I print everything to a log file including the status of the trade context. this way I know when the ea asked for the trade context and when it released it.

Good luck

whocares

 
phy:

Read

https://docs.mql4.com/basis/preprosessor/include

until you understand.

Then read

https://docs.mql4.com/basis/preprosessor/import

until you understand .

Hi Phy,

Thanks for the info. I've read them over and understand them.

I was trying to understand CB's info of the difference between an 'Included' file and a 'Header'. So is it accurate to say that functions brought into and utilized into a program from the libraries is a 'Header' and the *.mqh files that get compiled into the ex4 file are obviously 'included' files. I figured the later one out all by myself with my keen powers of observation and lightening quick intellect as they are stored in the 'Include' folder! LoL (< 8) Though I note that in CB's information that 'Headers' per say to not have to have an 'mqh' file extension or any file type/extension name at all? It is weird for a file not to have any 'file type' extension at all.

I saw in the start of the OnLine 'Documentation' section on MQL4 here that they say that and included file gets processes significantly faster than a library file which is relevant for me in this case as this is a high speed adjustable Martingale scalper that spits out trades like crazy.

Also there was an automatic posting as there often is on web pages here of the following article that I have bookmarked and will go over: https://www.mql5.com/en/articles/1456

Transferring an Indicator Code into an Expert Advisor Code. Indicator Structure


Thanks for your help Phy, have a great ChristMass (< 8)

 
I actually didn't say there was a difference between included files and header files. To my mind (and I may be wrong) the "h" in the .mqh extension stands for header. And header files (.mqh or whatever you name them) are added to the source code prior to compile using the include directive.
 
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

cloudbreaker:

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.

Happy Christmas.

CB

Hi CB et al,

I definitely was making it more complicated than it was. I think what threw me off was what must be an error in the article where they refer to the TradeContext as an mq4 file and not an mqh.

I have it working thanks. The code/logic for placing the trade is fairly lengthy and involved, though I don’t have enough experience for an accurate reference really. Initially I put the ‘included’ function call for the ‘Trade Context’ routine on the line immediately preceding the placing of the trade. But there it seemed to bypass and break out of the logic routine of staying in the same section and placing more orders. So I put it in front of the start of the two logic paths for placing long and short orders and it is working great thanks.

Have a better one,

Doug

 

Howdy all,

I want to thank everyone for all of your considerable help.

Now that I have it placing trades that way it is supposed to be, I can carry on with what I started to do: get it to close out losing trades. Though this is an adjustable Martingale EA and does makes lots of profitable trades and does have routines for closing out losing trades, it hardly ever uses it and hangs onto almost all of the losers that just get larger and larger until it has completely drained the account.

Have a great and safe HolyDay Season one and all.

. . . (8 >) . Live Long and Prosper . (< 8)

Thanks,

Doug

 
cloudbreaker:
I actually didn't say there was a difference between included files and header files. To my mind (and I may be wrong) the "h" in the .mqh extension stands for header. And header files (.mqh or whatever you name them) are added to the source code prior to compile using the include directive.

Makes sense to me CB. THough it did elict of response from Phy that helped me to learn and understand just a little bit more of all of this. The 'h' for header is a good memory aid.

What I can't figure out is what your new avatar attached to your profile is? I'll bite (as long as it isn't poisonous and /or in motion (< 8) What is it?

 

Ha ha. The avatar. Hmm. You'll find the answer in another thread which has been somewhat active of late. :-)


Best,

CB

 
cloudbreaker:

Ha ha. The avatar. Hmm. You'll find the answer in another thread which has been somewhat active of late. :-)


Best,

CB

Will the REAL Cloud Breaker' please stand up! though in your case maybe it should be: Will the real cloud breaker drop down though the cloud covering so that we can identify you!


Have a nice weekend (< 8)

 

I’m starting to get Pavlovian gratification when the complier comes back with that nice black notice:

0 error(s), 0 warning(s)

If I get to the point where I get conditioned enough that I get an endorphin charge from it, I might become quite prolific in MQL.

I don’t advise taking any bets on it though!

Regards,

Doug

Reason: