Standard Libraries

Download PDF

MQL5 Standard Library is written in MQL5 and is designed to facilitate writing programs (indicators, scripts, experts) for end users. Library provides convenient access to the most of the internal MQL5 functions.

Trade libraries are located in the following path: 
MQL5\Include\Trade\ and can be added using the #include directive.

Example
                              #include <Trade\trade.mqh>
                              #include <Trade\PositionInfo.mqh>
                            

The above classes can be considered as the basic ones as the majority of Expert Advisors can be programmed using only these two classes (libraries).

  • The first one is designed to placed and modify orders.
  • The second one serves to obtain information on already existing positions.
At times, another library can prove useful:
                          #include <Trade\OrderInfo.mqh>
                        

It contains functions for working with orders, if, say, our strategy requires the use of pending orders. Here is an example of a trade request made using the library:

At times, another library can prove useful:
                          CTrade m_Trade;
                          m_Trade.Sell(lot,symbol_name,price,sl,tp,comment);
                        

There is a total of 6 parameters here, only one of which is mandatory (the order size - that is the first parameter).

lot is the size of the order to be placed;

symbol_name is the symbol (currency pair) the order applies to (if none is specified, the current symbol of the Expert Advisor is used);

Price is the opening price (since this is the function for opening an active order, its price might be unspecified in which case it will automatically be obtained directly from the price chart);

sl is the price at which the order will close, should the price not be to our advantage (it may be left out if the strategy does not imply the use of stop loss);

tp is the price at which the order will close, should the price take the required direction, i.e. it takes profit (it may be left out if the strategy does not imply the use of take profit);

comment is the comment to the order, e.g. specifying the reason for placing the order.

There is a number of ways to close a position:

1. to close the whole position

                                CPositionInfo m_Position;
                                m_Position.Select(symbol_name);
                                m_Trade.PositionClose(symbol_name);
                            

2. to close the position by placing a reverse order of the same size

                                CTrade m_Trade;
                                m_Trade.Buy(lot,symbol_name,price,sl,tp,comment);
                            

3. by using a more complicated method whereby all open positions are first searched through to single out the one that meets the required parameters (symbol, type, magic number,position identifier, etc.) to be further closed. However, we would not give any example of these case due to its difficulty for beginners.