Saturday, 9 February 2013

How To Write a Close All Script in MQL4? (4 of 4)


MT4
contains a simple method for referencing the prices and data of different symbols in MQL4. This function is the MarketInfo() function. The MarketInfo() function has many uses, all of which will not be explored in this article. MarketInfo() can be used to retrieve the bid and ask prices of  the currently selected symbol as MT4 iterates through a list of orders and attempts to close them. This is a useful feature that is needed to make a CloseAll routine work correctly.

One additional gotcha with regards to MT4 is that sometimes rounding errors can cause problems that prevent orders from closing. The OrderClose() function is very particular about the type of data it receives, particularly the prices used, including the number of decimals in those prices. When referencing prices from a symbol other than the current symbol, it is good practice to round the symbol's price to the correct number of decimal places. A symbol's decimal places or Digits can be retrieved via the MarketInfo() function from the currently selected order symbol as follows:
MarketInfo(OrderSymbol(), MODE_BID); 
MarketInfo(OrderSymbol(), MODE_ASK);

To finish off these lines of MQL4 code, the NormalizeDouble() function can be used to round a price to a specified number of Digits, and in the case of MarketInfo(OrderSymbol(), MODE_DIGITS); to retrieve the digits from the selected order's settings. The code to close all orders for all symbols can be generalized as follows:

void CloseAll(int Slippage, int MagicNumber) {
bool closed;
for (int i = OrdersTotal(); i >=0; i--) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    while(IsTradeContextBusy()) Sleep(100);
    RefreshRates();
    if (OrderType() == OP_BUY && MagicNumber == OrderMagicNumber()) { 
        closed = OrderClose(OrderTicket(),OrderLots(),
         NormalizeDouble(MarketInfo(OrderSymbol(),
         MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);
    }
    if (OrderType() == OP_SELL && MagicNumber == OrderMagicNumber()) { 
        closed = OrderClose(OrderTicket(),OrderLots(),
         NormalizeDouble(MarketInfo(OrderSymbol(),
         MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)),Slippage,White);
    }
}
}
Just as with the CloseThis routine, the CloseAll routine may be called as follows using 7 pips of slippage and a Magic Number of zero:
CloseAll(7, 0);

Using the MQL4 code contained in this sample, you should now be able to write your own CloseAll or CloseThis routine. While not all advanced topics have been covered in this short tutorial, by making use of the MT4 help files and the other MQL4 code samples on this site and others, you should be able to build or adapt your very own Metatrader order closing routine!

If you would like to download the code for this tutorial, you may find the links here:
Close All Orders This Symbol Only MQL4 Script for Metatrader               Close All Trades All Symbols MQL4 Script for Metatrader 4

No comments:

Post a Comment