I can't seem to find anything in the docs and searches on the internet don't answer my Q.
I'd hate to leave it up to the user to go into the EA Properties dialog and configure whether their broker has FIFO requirements, but that's looking like my only option right now.
ACCOUNT_FIFO_CLOSE
https://www.mql5.com/en/docs/constants/environment_state/accountinformation#enum_account_info_string

- www.mql5.com
ACCOUNT_FIFO_CLOSE
https://www.mql5.com/en/docs/constants/environment_state/accountinformation#enum_account_info_string
Thanks, but this is MQL4 forum, not MQL5
then you need to compare 2 positions open and close time
I actually check the index of the trade when looping through OrdersTotal to make sure it is the first trade among open trades for that symbol, but your solution works well, too, although I think just comparing the OrderOpenTime should suffice. It's worth mentioning that FIFO goes beyond the order of which the trade was opened, from what I've read, but I haven't tested it, yet. Regardless, it shouldn't be difficult to accommodate for the caveats.
Anyway, I'm trying to avoid FIFO violations altogether. On top of resulting in the trade being unable to close, the broker can lock your account if you have too many of them and that's something that no one wants and can/should be avoided.
My solution is to allow the user to configure in the EA's Property dialog whether FIFO is required.
If they make a mistake or forget to set it, and try to close a trade that results in a FIFO error, then return the error code (ERR_TRADE_PROHIBITED_BY_FIFO), check for the error code and set the variable "FIFO Required" to true. Subsequent calls to close the trade should then run through your code's FIFO check before calling OrderClose, at least until the user switches symbols or timeframes.
A solution I'm considering, that doesn't require the user to do anything, is to use a Global Variable and check for its existence. It won't exist at first but, if a FIFO violation occurs, the GV will be created with the name of the server, and then your code will see its existence and know to check for whether the close request violates FIFO.
Hopefully, this helps someone out who's programming for MT4.
- 2011.06.01
- More from Forex Ninja
- www.babypips.com
In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading), while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing and order count:
-
For non-FIFO (non-US brokers), (or the EA only opens one order per symbol), you can simply count down, in an index loop, and you won't miss orders. Get in the habit of always counting down.
Loops and Closing or Deleting Orders - MQL4 programming forum (2012) -
For In First Out (FIFO rules — US brokers), and you (potentially) process multiple orders per symbol, you must find the earliest order (count up), close it, and on a successful operation, reprocess all positions (from zero).
CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16 (2019)
MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1.11 (2019)
and check OrderSelect in case other positions were deleted.
What are Function return values ? How do I use them ? - MQL4 programming forum
Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().
Why? Just close orders in a FIFO compliant way.
A few reasons:
1. There's no way to tell whether the account is restricted by FIFO, so the user may very well be in the right to close the trade in a non FIFO compliant way.
2. The EA uses chart objects for exits, so the user may make a mistake in thinking X trade should close, but Y trade closes, instead, and get unexpected results.
Your proposed solution is fine for automated trading, though. In the future, I'll revisit it to make it work with my EA but, for right now, the way I'm doing it works fine.

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I can't seem to find anything in the docs and searches on the internet don't answer my Q.
I'd hate to leave it up to the user to go into the EA Properties dialog and configure whether their broker has FIFO requirements, but that's looking like my only option right now.