Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

Trade Classes from MT5 for MT4 - library for MetaTrader 4

Views:
6324
Rating:
(35)
Published:
2022.04.27 11:54
Updated:
2022.05.06 08:20
\MQL4\Include\Trade\
OrderInfo.mqh (43.87 KB) view
PositionInfo.mqh (15.92 KB) view
SymbolInfo.mqh (71.44 KB) view
Trade.mqh (163.76 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

In the process of developing a program for copying positions between accounts, it was necessary to develop essentially the same code for MT5 and MT4. The differences were only in the functions of performing trading operations. Therefore, several classes from the MT5 standard library were rewritten to work in MT4.

When using this library, it will ideally be possible to compile a program from MT5 with a compiler in MT4 so that it can work in MT4.

But there are two important limitations:

  • The program must not use any classes and libraries from MT5 that are not in MT4. For example, if CHashMap is used, then you will either have to abandon it, or also transfer it to MT4
  • All trading operations must be performed only through the CTrade class, all operations for obtaining market information - through the CPositionInfo, COrderInfo, CSymbolInfo classes

In this version, there may be some class methods that could not be found   when testing as in need of correction.

The file TradeLibraryMT5Example.mq4 is an example of a simple EA that compiles and runs on both MT5 and MT4.

The following code is used to open pending orders:

CTrade m_trade;

...

res = m_trade.OrderOpen(m_symbol, ORDER_TYPE_BUY_LIMIT , m_lot, lowPrice, lowPrice, 0 , 0 , ORDER_TIME_GTC , 0 );
res = m_trade.OrderOpen(m_symbol, ORDER_TYPE_SELL_LIMIT , m_lot, highPrice, highPrice, 0 , 0 , ORDER_TIME_GTC , 0 );

The function of calculating the total profit of all open market positions can be implemented as follows:

//+------------------------------------------------------------------+
//| Calculate total profit for all market orders                     |
//+------------------------------------------------------------------+
double GetProfit() {
   int total = PositionsTotal();
   double profit = 0;

   CPositionInfo p;

   for(int i = total - 1; i >= 0; i--) {
       if(p.SelectByIndex(i)) {
         ulong magic = p.Magic();
         string symbol = p. Symbol();
         if(magic != m_magicN || symbol != m_symbol) {
             continue ;
         }

         profit += p.Profit() + p.Commission() + p.Swap();
      }
   }

   return profit;
}

The function of closing all open positions and pending orders can be implemented as follows:

//+------------------------------------------------------------------+
//| Attempt to close all pending and market orders                   |
//+------------------------------------------------------------------+
bool TryCloseAll() {
   bool res = true ;
   
   int total;
   double profit = 0;
   ulong ticket;
   ulong magic;
   string symbol;
   
   total = OrdersTotal();

   COrderInfo o;

   for(int i = total - 1; i >= 0; i--) {
       if(o.SelectByIndex(i)) {
         magic = o.Magic();
         symbol = o.Symbol();
         if(magic != m_magicN || symbol != m_symbol) {
             continue ;
         }
         ticket = o.Ticket();

         res &= m_trade.OrderDelete(o.Ticket());
      }
   }

   total = PositionsTotal();
   
   CPositionInfo p;

   for(int i = total - 1; i >= 0; i--) {
       if(p.SelectByIndex(i)) {
         magic = p.Magic();
         symbol = p. Symbol();
         if (magic != m_magicN || symbol != m_symbol) {
             continue ;
         }
         ticket = p.Ticket();

         res &= m_trade.PositionClose(ticket) ;
      }
   }

   return res;
}


SmoothStep (generalized) SmoothStep (generalized)

SmoothStep (generalized) - metatrader 4 version

SmoothStep SmoothStep

SmoothStep (metatrader 4 version)

High Volume Bars High Volume Bars

Colors bars when their volume has exceeded more than a standard deviation, or a multiple of it. Volume is commonly used as confirmation for a break of a significant level.

Constant Range Channel Constant Range Channel

A simple indicator plotting a channel with a constant range