Saturday 9 February 2013

How to Use the MarketInfo MQL4 function?

This how to MQL4 article covers the MarketInfo() function and touches on some other useful functions including, Symbol()OrderSelect(), OrderSymbol(), OrderClose(), and NormalizeDouble(). In addition this article discusses the constants MODE_BID, Bid, MODE_ASK, Ask, MODE_DIGITS, Digits and NULL.
Let's say you wish to create an expert advisor or indicator that references data from another chart. How would a trader go about doing that in MQL4? The MarketInfo() function has many useful request identifiers. (Please consult the help within MetaEditor for a complete list). But first read on! MarketInfo() is defined as follows:
double MarketInfo(string symbol, int type)

To reference the current chart's data, simply insert NULL or Symbol() where it says symbol, such as MarketInfo(NULL, ...) or MarketInfo(Symbol(), ...).
string Symbol()

If you'd like to use MarketInfo() to reference other forex charts within Metatrader, there are several ways to do this and they are context specific. Let's assume you wish to close out a group of trades on a different symbol. For instance in the scripts Close All Orders This Symbol Only, and Close All Trades All Symbols trades are closed out via a loop. Here is a snippet from the second link.

bool closed = false;
   for (int i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
        .............
      if (OrderType() == OP_BUY && (MagicNumber == OrderMagicNumber() || MagicNumber == -1)) {
        closed = OrderClose( OrderTicket(), OrderLots(), 
        NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),MarketInfo(OrderSymbol(),MODE_DIGITS)), Slippage,        White);
      }
.................

Note that within the close out for loop, OrderSelect is called. This effectively opens up some opportunities to use built-in MQL4 functions to reference information about trades that have originated from charts other than the current chart. 
bool OrderSelect(int index, int select, int pool=MODE_TRADES)

The OrderClose function calls for a ticket number, the number of lots to close, a closing price, a slippage amount, and a color. Notice the presence of the MarketInfo function in the parameter that refers to a closing price. 
bool OrderClose(int ticket, double lots, double price, int slippage, color Color=CLR_NONE)

In this case functions are nested to ensure that data is formatted in the correct way. The first term of the MarketInfo function is OrderSymbol which references the selected order's symbol. This handy function provides the symbol term that is needed to fill the first term of MarketInfo. The bid price is referenced by using the MODE_BID request identifier. MODE_BID is a constant with the value of 9. 

It is not hard to see how this MQL4 example could be easily adapted to close out a long trade by using MarketInfo along with the MODE_ASK request identifier. That block of MQL4 code would look something like this:    
if (OrderType() == OP_SELL && (MagicNumber == OrderMagicNumber() || MagicNumber == -1)) {
        closed = OrderClose( OrderTicket(), OrderLots(), 
        NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),MarketInfo(OrderSymbol(),MODE_DIGITS)), Slippage, White);
}

The NormalizeDouble function is called to ensure the proper decimal places are used to round the price before order submission. I have noticed that Metatrader brokers tend to be VERY picky about the formatting of orders with the OrderClose function, thus the need for extra special care by using the NormalizeDouble function. The help file declares NormalizeDouble as follows:
double NormalizeDouble( double value, int digits)

In this case the market price that will be used to close the open trade is referenced either by MODE_BID or MODE_ASK. Thus the bid or ask price will be used as returned from the MarketInfo function. Metatrader brokers will reject orders if the incorrect price is used in an OrderClose function. Ensure that MODE_BID is used to close a long trade, and MODE_ASK is used to close a short trade. Referencing Close[0] or any other price will often kick out a dreaded error usually in the range of 135-138 (see your MetaEditor help file under "Error codes") or some similar error that prevents trade execution in the Metatrader terminal, so it pays to double check your MQL4 code.

The second term of NormalizeDouble requires the number of Digits. Because this routine may be referring to a symbol that may be different than the local variable Digits, the correct value is retrieved by making another call to MarketInfo as below:
MarketInfo(OrderSymbol(),MODE_DIGITS)
If the symbol is a Japanese Yen pair and the broker is using pippettes, then the function above will return 3 for Digits. If pips are used it will return 2. For non-JPY pairs the return values will be 5 or 4 respectively for Digits

For more information about how to close all open trades, or how to close all open orders for the selected symbol only, please see the referenced scripts, or read the tutorial for a full blow-by-blow walkthrough of how to write a close all script in MQL4.

These are just two short examples of how to use the MarketInfo function to reference data that may or may not be on a different chart. There are many other possible uses for this function (see the MetaEditor help file under "MarketInfo" for more information about request identifiers) but the form of the MarketInfo function is similar to the form discussed in this short tutorial.

3 comments:

  1. Hey Ya'll,

    Below is a list of the most recommended forex brokers:
    1. Most Recommended Forex Broker
    2. eToro - $50 min. deposit.

    Here is a list of the best forex instruments:
    1. ForexTrendy - Recommended Probability Software.
    2. EA Builder - Custom Indicators Autotrading.
    3. Fast FX Profit - Secret Forex Strategy.

    I hope you find these lists beneficial.

    ReplyDelete
  2. GPS robot [10 times increase of the deposit amount]

    I just finished a webinar with Mark and his partner, Antony, two days ago and it was AMAZING.

    During the webinar Mark and Antony shared their secrets to success and answered questions about their new release of the GPS Forex Robot that is coming out TODAY!

    ReplyDelete
  3. QUANTUM BINARY SIGNALS

    Get professional trading signals delivered to your mobile phone daily.

    Start following our signals right now & gain up to 270% a day.

    ReplyDelete