Nasty BUG! Calling OrderSelect from a library will not leave you with the order selected in the main file!
source code is uploaded here: 'More BUGs: Time[0] does not return correct time when testing in tester!', howver in order to overcome this probelm I removed all of the 5 functions listed
above from TradingLib and put them into the main faile of the expert (DLobotomy).
If you want to reproduce this you'll need to cut and past them back into TradingLib.
...
MoveToTopSell(_MagicNumber); while(OrderSelect(SelectNextSellPosition( _MagicNumber ), SELECT_BY_TICKET) { } // this replaces selectfirstsellposition in the library bool MoveToTopSell( int iMagicNumber ) { s_iCurrBuyPosition = OrdersTotal()-1; }
I never came up against this problem, but if it is the case, you can change your function to return ticket number or -1 instead true/false and then just call orderselect again in main file, like this: (sorry the code came out on top, oops).
irusoh1, returning the ticket number is a good idea, thank you. It may provide me with a
workaround for the problem. I'll give it a try.
However it would be really great if MetaQuotes fixed this, cause the whole idea of libraries is of course to encapsulate and jide finicky, hard-to-maintain code and be able to easily reuse it when needed without having to copy it around...
However it would be really great if MetaQuotes fixed this, cause the whole idea of libraries is of course to encapsulate and jide finicky, hard-to-maintain code and be able to easily reuse it when needed without having to copy it around...
I don't know if MQ have a workaround for that, but you have to remember that libraries are not as much a copy code but more are like self styled DLLs and therefore do not communicate their state implicitly back to the main body.
If you want to have access to variables assuming they are there you need to put whole functions into mqh file which will then just copy code into your program just like it was part of the source.
If you want to have access to variables assuming they are there you need to put whole functions into mqh file which will then just copy code into your program just like it was part of the source.
Oh dear, that's a bummer, I have lot's of order selection code in header files, I wonder what has actually been happening...
irusoh1:
I don't know if MQ have a workaround for that, but you have to remember that libraries are not as much a copy code but more are like self styled DLLs and therefore do not communicate their state implicitly back to the main body.
If you want to have access to variables assuming they are there you need to put whole functions into mqh file which will then just copy code into your program just like it was part of the source.
Oh interesting - I thought libraries in MQL are like in C - they become part of
your program. I did not think of them as DLLs. I now understand why you and Rosh
suggested that I should move this code to a mqh file. I don't know if MQ have a workaround for that, but you have to remember that libraries are not as much a copy code but more are like self styled DLLs and therefore do not communicate their state implicitly back to the main body.
If you want to have access to variables assuming they are there you need to put whole functions into mqh file which will then just copy code into your program just like it was part of the source.
However, what confuses me is that you are saying "if you want to have access to variables.." - but that's not what I am doing. I am calling OrderSelect in the library (which I imagine sets certain internal global data structs in the MQL/MT4 framework) and I thought that these structs will remain the same when I return from the library. In particular I thought that once an order is selected - this is THE ONE and ONLY selected order in your mql application. I thought of this like an OS file handle - if you open a file in a lib or DLL it will still be open when control returns to your main executable. Is that not how OrderSelect works in MQL? (Obviously not - because I ran a coupe of tests with yours/Rosh's suggestion to put the problematic function into the mqh header - but I am curious to understand how it all works behind the scenes :) )
Is there some link/article I could read about using libraries in MQL and what I can and cannot expect from code in a library, so I can avoid problems like this in the future?
thank you.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
, in the main file of the expert results in an invalid Ticket number (in my case the Ticket number was 4 more than the real ticket regardless of how many orders I have).
The library functions SelectFirst.. and SelectNext are given below, need to put them in a library and add to a header file. When I put these functions into the main expert file it works properly.