Home > Forex, Forex Trader, Forex Trader + Metatrader, Metatrader, MQL, MQL4, MQL4 Language Reference, MT4, Trader > MQL4 Forex Trading Function Reference With Sample Code

MQL4 Forex Trading Function Reference With Sample Code

Have you ever wanted to learn about the MT4 forex trading functions without needing to learn Russian? In this series all MQL4 trading functions are described in detail. MQL4 trading functions are broken down by functional category, including the most important functions: OrderSend, OrderModify, OrderClose, OrderSelect as well as the many other important and useful trading functions and constants. Sample code is provided by category, including functional routines that may be dropped directly into your code for OrderSend, OrderClose and OrderModify – three of the hardest to implement functions in MQL4.

MQL4 Trading Functions Lists all MQL4 trading functions alphabetically and by functional category.

MQL4: OrderClose, OrderCloseBy, OrderDelete, OrderModify, OrderSend

MQL4: OrderSelect, OrdersHistoryTotal and OrdersTotal

MQL4: Specific Order Information  Includes reference for MQL4 functions:

  1. Andrius
    October 28, 2011 at 8:01 pm

    I have visited your links, and I have a question on “CloseAll…” script which starts with
    for (int i = 0; i < OrdersTotal(); i++) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    I wonder if it works correctly, and if it does then why:
    E.g., you have 10 trades to close. OrdersTotal() would return 10 at the beginning, while indexing of total orders pool strarts by zero, right? The main question is about the expression you use inside the cycle operator for keeping it on task, that is, about "i<OrdersTotal()". The value of the variable i increases by 1 each time at the end of the loop. On the other side (literally, on the right side of the expression), the value of OrdersTotal() decreases by 1 if one order per loop gets closed. So i climbs up 0,1,2,3… while OrdersTotal() steps down 10,9,8,7… When 5 trades out of the total 10 have already closed, then i and OrdersTotal() are both equal to 5. It means the expression "i<OrdersTotal()" turns false at this stage, therefore cycling ends, and half of all orders still remain open.

    • October 30, 2011 at 5:49 pm

      Andrius, Thanks for your comment.

      I’ve had good experience with the CloseAll script in its current form closing out all orders, though I may do extra double checking on subsequent loops in some of my code, and I can see your point. I think the reason it usually does in fact close out all trades is that there typically isn’t an instant update on the OrdersTotal() amount as one might expect. If the loop can complete before the OrdersTotal() value changes, then all the orders should close out. The proper way to avoid this potential problem would be to set up the for loop would be something like the following:

      for (int i = OrdersTotal()-1; i >= 0; i–) {

      This should avoid the problem of shooting past or skipping some of the open orders. But as a practical matter, I suggest you test both on a demo account on your specific broker to determine which actually works better as MT4 is full of quirks that play out differently on different brokers (and I haven’t tested the code above).

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: