Accessing Market Data in MQL5

February 28, 2010
By Bogdan Baltatu, MQLmagazine editor

[Versiunea romaneasca] [MQLmagazine.com in romana] [English edition]

In this article I’ll present the functions that access market data in MQL5, and I’ll compare tot the function that was doing this in MQL4.

In MQL4 there was a unique function that was the holder of all the market information, and that was MarketInfo(). In MQL5 the MarketInfo() function vanished, being replaced by SymbolInfoDouble(), SymbolInfoInteger(), SymbolInfoString(), SymbolInfoTick(). The name of the function is being formed from 3 words: the first two are ‘SymbolInfo’ and the last word is the one showing what the function returns, ‘Double’, ‘Integer’ or ‘String’.

SymbolInfoDouble()

The function has two prototypes:

1
2
3
4
double  SymbolInfoDouble(
   string  name,      // symbol   
   int     prop_id     // identifier of the property     
);

The second prototype is:

1
2
3
4
5
bool  SymbolInfoDouble(
   string   name,      // symbol    
   int      prop_id,    // identifier of the property     
   double &double_var // here we assume the property value
);

The function returns true if the function is successful (if it places the returned value in a variable sent as parameter).

Identifier Description MQL4 Equivalent
SYMBOL_BID Bid – Best buy offer (level II) or price to sell at (level I) MODE_BID
SYMBOL_BIDHIGH Maximal Bid of the day None
SYMBOL_BIDLOW Minimal Bid of the day None
SYMBOL_ASK Ask – Best sell offer (level II) or price to buy at (level I) MODE_ASK
SYMBOL_ASKHIGH Maximal Ask of the day None
SYMBOL_ASKLOW Minimal Ask of the day None/td>
SYMBOL_LAST Price of the last deal None
SYMBOL_LASTHIGH Maximal Last of the day None
SYMBOL_LASTLOW Minimal Last of the day None
SYMBOL_POINT Mathematic point. For instance, if a quotation has 4 digits, the point is 1/10000. MODE_POINT
SYMBOL_TRADE_TICK_VALUE The value of a tick, on the standard lot, in the currency of the account. For instance is 1 at EURUSD, if the lot is 100000, with 5 digits, or 10, for 4 digits. For forex, is CONTRACT SIZE * TICK SIZE, considered in the second currency in the pair and converted in the account currency. For futures, is CONTRACT SIZE * POINT * TICK SIZE (should be also converted in the currency of the account, but this may be missing…) MODE_TICKVALUE
SYMBOL_TRADE_TICK_SIZE The size of a tick. For forex, it is the same as POINT. For other markets, it differs. For instance it is 0.25 for corn futures, because that is the minimum quoting step. MODE_TICKSIZE
SYMBOL_TRADE_ CONTRACT_SIZE The size of one lot (the standard lot). Usually, for forex is 100000 units (for some brokers, beware, it’s 10000). Differs for futures. MODE_LOTSIZE
SMBOL_VOLUME_MIN Minimal volume for a deal MODE_MINLOT
SYMBOL_VOLUME_MAX Maximal volume for a deal MODE_MAXLOT
SYMBOL_VOLUME_STEP Step of the volume (the acceptable volume is a multiple of the step, starting with the minimal volume) MODE_LOTSTEP
SYMBOL_SWAP_LONG Long swap value MODE_SWAPLONG
SYMBOL_SWAP_SHORT Short swap value MODE_SWAPSHORT

SymbolInfoInteger()

Functia are acelasi prototip ca functia SymbolInfoDouble().

Tabelul de echivalenta a functiei SymbolInfoInteger() in MQL4 este:

Identifier Description Type MQL4 Equivalent
SYMBOL_SELECT Symbol is selected in Market Watch bool None
SYMBOL_VOLUME Volume of the last deal long None
SYMBOL_VOLUMEHIGH Maximal day volume long None
SYMBOL_VOLUMELOW Minial day volume long None
SYMBOL_VOLUMEBID Volume for current Bid long None
SYMBOL_VOLUMEASK Volume for current Ask long None
SYMBOL_TIME Time of the last quote datetime MODE_TIME
SYMBOL_DIGITS Number of decimals after the point (or base 10 logarithm of the reverse of POINT) int MODE_DIGITS
SYMBOL_SPREAD Spread value in points int MODE_SPREAD
SYMBOL_TICK_BOOKDEPTH Ticks storing depth long None
SYMBOL_TRADE_CALC_MODE Contract profit and margin calculation mode ENUM_ SYMBOL_ CALC_ MODE None
SYMBOL_TRADE_MODE Order execution type ENUM_ SYMBOL_ TRADE_ MODE None
SYMBOL_TRADE_STOPS_LEVEL The minimum distance between the current price and the Stop level int MODE_STOPLEVEL
SYMBOL_TRADE_FREEZE_LEVEL Order freeze level in points. If the execution price lies within the range defined by the freeze level, the order cannot be modified, cancelled or closed. int MODE_FREEZELEVEL
SYMBOL_TRADE_EXEMODE Deal execution mode ENUM_ SYMBOL_ TRADE_ EXECUTION None
SYMBOL_SWAP_MODE Swap calculation model ENUM_ SYMBOL_ SWAP_ MODE MODE_SWAPTYPE
SYMBOL_ROLLOVER3DAYS The day the swap is triple ENUM_ DAY_OF_ WEEK None

The ENUM_SYMBOL_CALC_MODE type describes the calculus mode for margin and profit, as follows:

Identifier Description Formula
SYMBOL_CALC_MODE_FOREX Forex Margin: Lots*Contract_Size/Leverage;
Profit: (close_price-open_price)*Contract_Size*Lots
SYMBOL_CALC_MODE_CFD CFD Margin: Lots *ContractSize*MarketPrice*Percentage/100; Profit: (close_price-open_price)*Contract_Size*Lots
SYMBOL_CALC_MODE_FUTURES Futures Margin: Lots *InitialMargin*Percentage/100
Profit: (close_price-open_price)*TickPrice/TickSize*Lot
SYMBOL_CALC_MODE_CFDINDEX Indice CFD Margin: (Lots*ContractSize*MarketPrice)*TickPrice/TickSize;
Profit: (close_price-open_price)*Contract_Size*Lots
SYMBOL_CALC_MODE_CFDLEVERAGE CFD cu levier Margin: (Lots*ContractSize*MarketPrice*Percentage)/Leverage;
Profit: (close_price-open_price)*Contract_Size*Lots;

The ENUM_SYMBOL_TRADE_MODE type describes what kind of operations are allowed.

Identifier Meaning
SYMBOL_TRADE_MODE_DISABLED Trade is disabled fro the symbol
SYMBOL_TRADE_MODE_LONGONLY Allowed only long positions
SYMBOL_TRADE_MODE_SHORTONLY Allowed only short positions
SYMBOL_TRADE_MODE_CLOSEONLY Allowed only position close operations
SYMBOL_TRADE_MODE_FULL No trade restrictions

The ENUM_SYMBOL_TRADE_MODE type describes what manner is the execution.

Identifier Meaning
SYMBOL_TRADE_EXECUTION_REQUEST Execution by request.
SYMBOL_TRADE_EXECUTION_INSTANT Instant execution
SYMBOL_TRADE_EXECUTION_MARKET Market execution

While Deviation remains a crucial parameter for execution, the difference between the three modes remains to be seen.

The ENUM_SYMBOL_SWAP_MODE describes how swaps are applied.

Identifier Meaning Notes
SYMBOL_SWAP_MODE_DISABLED Swap mode disabled (no swaps)
SYMBOL_SWAP_MODE_BY_POINTS Swaps in points
SYMBOL_SWAP_MODE_BY_MONEY Swaps in money, its value is specified in the symbol properties in the deposit currency Probably swap in the account currency, for the standard lot
SYMBOL_SWAP_MODE_BY_INTEREST Swaps in yearly percents (bank mode – 360 days in a year) The formula is pretty clear, but which is the currency?
SYMBOL_SWAP_MODE_BY_MARGIN_CURRENCY Swaps in money, its value is specified in the symbol properties in the margin currency Probably swap in the margin currency, for the standard lot.

However, MetaQuotes didn’t indicate also the calculus formulas. The swap was a real mystery in MT4, and the formulas where found out by trial and error (except for type 2, which remained undiscovered by me in the 2007 article on swaps). Seems however this time swaps will be easier to extract, without the headaches from MT4.

The ENUM_DAY_OF_WEEK type has the week days, as the name states.

SymbolInfoString()

The function has the same prototype SymbolInfoDouble.

The MQL4 equivalence table of the SymbolInfoString() function:

Identifier Description MQL4 equivalent
SYMBOL_CURRENCY_BASE Basic currency of a symbol None
SYMBOL_CURRENCY_PROFIT Profit currency None
SYMBOL_CURRENCY_MARGIN Margin currency. For forex, it’s the first currency in the pair. None
SYMBOL_BANK Feeder of the current quote None
SYMBOL_DESCRIPTION Symbol description None
SYMBOL_PATH Path in the symbol tree None

SymbolInfoTick()

The SymbolInfoTick() function has the following prototype.

1
2
3
4
bool  SymbolInfoTick(
   string    symbol,     // symbol
   MqlTick&  tick        // reference to a MqlTick structure
   );

The function fills an MqlTick structure with the data of the current quote.