Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

Views:
5993
Rating:
votes: 61
Published:
2016.10.10 15:31
Updated:
2018.06.06 09:47
\MQL5\Experts\\MQL5\Include\
MT4orders.mqh (76.24 KB)view
\MQL5\Scripts\

This library allows to work with the orders in MQL5 (MT5-hedge) in the same way as in MQL4.

That is, the order language system (OLS) becomes identical to MQL4. At the same time, it is still possible to use the MQL5 order system in parallel. In particular, the standard MQL5 library will continue to fully operate.

It is not necessary to choose between the order systems. Use them in parallel! When translating MQL4 -> MQL5, there is no need to touch the order system at all.

It is sufficient to add a single line at the beginning:

#include <MT4Orders.mqh> // if there is #include <Trade/Trade.mqh>, add this line AFTER that

Similar actions (adding one line) in your MQL5 codes allow to add the MT4 OLS to the MT5 OLS, or completely replace it.

The author had created this feature for himself, therefore, he deliberately had not applied the same idea of "one-line" transfer for timeseries, graphical objects, indicators, etc.

This work covers only the order system.

The task of possibility to create a complete library for allowing the MQL4 code to work in MetaTrader 5 without changes had not been considered.

As an example, multiple MT4/5 Expert Advisors (from the codebase) of different types have been used: scalpers, news trading experts, martingales, multi-currency experts, etc.

Here (parallelism of the MT4 and MT5 OLS) is how they can be used in the MT5 tester (or demo):

// MQL4&5-code

#define OnTick MTstart
#define start  MTstart

#ifdef __MQL5__

// Select the required MT5 EA
// #include "Lucky.mq5"               // https://www.mql5.com/en/code/1107
// #include "Puria.mq5"               // https://www.mql5.com/en/code/138
// #include "Ilan_1_6_Dynamic_HT.mq5" // https://www.mql5.com/en/code/12220
// #include "Multik.mq5"              // https://www.mql5.com/en/code/158
// #include "VLT_Trader.mq5"          // https://www.mql5.com/en/code/1163
// #include "Exp_PFE_Extr.mq5"        // https://www.mql5.com/en/code/16044

#endif // __MQL5__

#include <MT4Orders.mqh> // if there is #include <Trade/Trade.mqh>, add this line AFTER that

#include <MQL4_to_MQL5.mqh> // ONLY for this example

// Select the required MT4 EA
// #include "Lucky.mq4"          // https://www.mql5.com/en/code/7464
// #include "E-news-lucky6.mq4"  // https://www.mql5.com/en/code/7672
// #include "cm_ea_news.mq4"     // https://www.mql5.com/en/code/10511
// #include "Spreader_v2.mq4"    // https://www.mql5.com/en/code/14500
#include "Ilan1_4_strict.mq4" // #property strict (MQL5), original - https://www.mql5.com/en/code/8492

#include "OrdersToString.mqh"

#undef OnTick

void OnTick( void )
{
  ::MTstart();

  if (MQLInfoInteger(MQL_VISUAL_MODE))
    Comment("Trade:" + OrdersToString() + "\nHistory:" + OrdersToString(MODE_HISTORY)); // can be viewed in visual mode of the tester

  return;
}

If earlier it was possible to test the MT4 experts on real ticks in the MetaTrader 4 tester, now the MT5 tester has the multi-currency capabilities. That is, the multi-currency MT4 experts can now be tested directly in the tester!

An additional example is a statistical script, collected from multiple MT4 samples:

// MQL4&5-code

#include <MT4Orders.mqh> // if there is #include <Trade/Trade.mqh>, add this line AFTER that

#include <MQL4_to_MQL5.mqh> // ONLY for this example

#ifdef __MQL5__
  #property script_show_inputs

  void OnStart( void )
  {
    ::OnStartScript();  // TradingSystemRating_Script_Ad.mq4

    ::WriteDealsInfo(); // b-SharingDoW.mqh

    // Report_strict.mq4
    ::init();
    ::start();
    ::deinit();

    MessageBox("Look in MQL5\\Files");

    return;
  }
#endif // __MQL5__

#define OnStart OnStartScript

#include "TradingSystemRating_Script_Ad.mq4" // https://www.mql5.com/en/code/12040

#include <b-SharingDoW.mqh> // https://www.mql5.com/en/code/8090
sinput int MAGIC = 0;       // MagicNumber of the evaluated orders (b-SharingDoW.mqh)

// Disregard the Warnings - script has been written in 2006...
#define i ii // avoid the Warnings part
#include "Report_strict.mq4" // #property strict (MQL5), original - https://www.mql5.com/en/code/8126

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/16006

Last comments | Go to discussion (10)
SysFX
SysFX | 14 Apr 2018 at 01:29

Sorry for the late reply :(

The issue is that 'MODE_HISTORY' trade information is basically unusable because:

1) when a trade closes, the ticket number changes ...you can easily test this by opening a buy / sell trade with a TP and SL - if the trade opens as ticket #2, that trade will become ticket #3 in the history pool after the SL or TP triggers

2) the magic number is lost

3) the ticket comment is lost

So, if the EA code needs to check whether a trade closed with a profit or loss, it's impossible because there's no information available to identify the trade.

I'm hoping that you can tweak things to retain the magic number and make the trade comment behave like MT4 - maybe you can find a way to retain the ticket number too.

fxsaber
fxsaber | 14 Apr 2018 at 09:10
SysFX:

Sorry for the late reply :(

The issue is that 'MODE_HISTORY' trade information is basically unusable because:

1) when a trade closes, the ticket number changes ...you can easily test this by opening a buy / sell trade with a TP and SL - if the trade opens as ticket #2, that trade will become ticket #3 in the history pool after the SL or TP triggers

2) the magic number is lost

3) the ticket comment is lost

So, if the EA code needs to check whether a trade closed with a profit or loss, it's impossible because there's no information available to identify the trade.

I'm hoping that you can tweak things to retain the magic number and make the trade comment behave like MT4 - maybe you can find a way to retain the ticket number too.

Example

#include <MT4Orders.mqh>

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

#define PRINT(A) Print(#A + " = " + (string)(A))

void OnInit()
{
  long Ticket;
  
  PRINT((Ticket = OrderSend(_Symbol, OP_BUY, 1, Ask, 0, Bid - 100 * _Point, Bid + 100 * _Point, "Hello World!", 12345)));
  
  if (OrderSelect(Ticket, SELECT_BY_TICKET))
    PRINT(OrderClose(OrderTicket(), 0.3, OrderClosePrice(), 0));
}

void OnDeinit( const int )
{
  const int Total = OrdersHistoryTotal();
  
  for (int i = 1; i < Total; i++)
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
    {
      OrderPrint();
      
      PRINT(OrderTicket());
      PRINT(OrderMagicNumber());
      PRINT(OrderComment());
      PRINT(OrderTicketOpen());
    }
}


Result

2018.03.25 00:00:00   instant buy 1.00 EURUSD at 1.23527 sl: 1.23414 tp: 1.23614 (1.23514 / 1.23527)
2018.03.25 00:00:00   deal #2 buy 1.00 EURUSD at 1.23527 done (based on order #2)
2018.03.25 00:00:00   deal performed [#2 buy 1.00 EURUSD at 1.23527]
2018.03.25 00:00:00   order performed buy 1.00 at 1.23527 [#2 buy 1.00 EURUSD at 1.23527]
2018.03.25 00:00:00   (Ticket=OrderSend(_Symbol,OP_BUY,1,Ask,0,Bid-100*_Point,Bid+100*_Point,Hello World!,12345)) = 2
2018.03.25 00:00:00   instant sell 0.30 EURUSD at 1.23514, close #2 (1.23514 / 1.23527)
2018.03.25 00:00:00   deal #3 sell 0.30 EURUSD at 1.23514 done (based on order #3)
2018.03.25 00:00:00   deal performed [#3 sell 0.30 EURUSD at 1.23514]
2018.03.25 00:00:00   order performed sell 0.30 at 1.23514 [#3 sell 0.30 EURUSD at 1.23514]
2018.03.25 00:00:00   OrderClose(OrderTicket(),0.3,OrderClosePrice(),0) = true
2018.03.26 01:04:40   take profit triggered #2 buy 0.70 EURUSD 1.23527 sl: 1.23414 tp: 1.23614 [#4 sell 0.70 EURUSD at 1.23614]
2018.03.26 01:04:40   deal #4 sell 0.70 EURUSD at 1.23614 done (based on order #4)
2018.03.26 01:04:40   deal performed [#4 sell 0.70 EURUSD at 1.23614]
2018.03.26 01:04:40   order performed sell 0.70 at 1.23614 [#4 sell 0.70 EURUSD at 1.23614]
final balance 10000046.11 EUR
2018.03.26 23:59:59   #3 2018.03.25 00:00:00 buy 0.30 EURUSD 1.23527 1.23414 1.23614 2018.03.25 00:00:00 1.23514 0.00 0.00 -3.16 Hello World! 12345
2018.03.26 23:59:59   OrderTicket() = 3
2018.03.26 23:59:59   OrderMagicNumber() = 12345
2018.03.26 23:59:59   OrderComment() = Hello World!
2018.03.26 23:59:59   OrderTicketOpen() = 2
2018.03.26 23:59:59   #4 2018.03.25 00:00:00 buy 0.70 EURUSD 1.23527 0.00000 1.23614 2018.03.26 01:04:40 1.23614 0.00 0.00 49.27 tp 1.23614 12345
2018.03.26 23:59:59   OrderTicket() = 4
2018.03.26 23:59:59   OrderMagicNumber() = 12345
2018.03.26 23:59:59   OrderComment() = tp 1.23614
2018.03.26 23:59:59   OrderTicketOpen() = 2

Forum on trading, automated trading systems and testing trading strategies

EA Identification

Carl Schreiber, 2015.11.21 13:46

the broker are free to change the comment. E.g. some replace the comment by [tp] or [sl] if the order has been closed by profit target or stop loss!!


PS Russian discussion thread.

fxsaber
fxsaber | 21 Oct 2018 at 20:13
fxsaber
fxsaber | 26 Nov 2018 at 15:57
fxsaber
fxsaber | 6 Dec 2018 at 09:04

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Библиотеки: MT4Orders

fxsaber, 2018.12.05 19:43

// Demonstration of how to manually send trade orders to the Visualizer.

#include <MT4Orders.mqh> // https://www.mql5.com/ru/code/16006

#define Bid SymbolInfoDouble(_Symbol, SYMBOL_BID)
#define Ask SymbolInfoDouble(_Symbol, SYMBOL_ASK)

bool IsModify()
{
  static long PrevTime = 0;
  
  const long NewTime = FileGetInteger(__FILE__, FILE_MODIFY_DATE);
  const bool Res = (PrevTime != NewTime);
  
  if (Res)
    PrevTime = NewTime;
    
  return(Res);  
}

bool CreateFile()
{
  uchar Bytes[];
      
  return(FileSave(__FILE__, Bytes) && IsModify());
}

string GetCommand()
{
  uchar Bytes[];
  FileLoad(__FILE__, Bytes);
  
  return(CharArrayToString(Bytes));
}

bool OrdersScan( const int Type )
{
  for (int i = ::OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderType() == Type))      
      return(true);    
    
  return(false);  
}

bool SendCommand( const string Command, const double Lot = 1, const int Offset = 100 )
{
  bool Res = false;
  
  if (Command == "open buy")  
    Res = (OrderSend(_Symbol, OP_BUY, Lot, Ask, 0, 0, 0) > 0);
  else if (Command == "open sell")  
    Res = (OrderSend(_Symbol, OP_SELL, Lot, Bid, 0, 0, 0) > 0);
  else if (Command == "open buylimit")  
    Res = (OrderSend(_Symbol, OP_BUYLIMIT, Lot, Ask - Offset * _Point, 0, 0, 0) > 0);
  else if (Command == "open selllimit")  
    Res = (OrderSend(_Symbol, OP_SELLLIMIT, Lot, Bid + Offset * _Point, 0, 0, 0) > 0);
  else if (Command == "close buy")  
    Res = OrdersScan(OP_BUY) && OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
  else if (Command == "close sell")
    Res = OrdersScan(OP_SELL) && OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0);
  else if (Command == "close buylimit")  
    Res = OrdersScan(OP_BUYLIMIT) && OrderDelete(OrderTicket());
  else if (Command == "close selllimit")
    Res = OrdersScan(OP_SELLLIMIT) && OrderDelete(OrderTicket());
    
  return(Res);
}

bool TesterManual()
{
  static const bool IsVisual = MQLInfoInteger(MQL_VISUAL_MODE) && CreateFile();
  
  return(IsVisual && IsModify() && SendCommand(GetCommand()));
}

void OnTick()
{
  TesterManual();
}


i-CAiChannel i-CAiChannel

The Envelopes indicator with the use of the i-CAi indicator algorithm.

i-CAi_Digit i-CAi_Digit

The i-CAi indicator displays the most recent value as a price label with the the possibility to round the channel levels up to a required number of digits.

i-CAiChannel_System_Digit i-CAiChannel_System_Digit

The indicator implements a breakthrough system using the i-CAiChannel channel.

Exp_i-CAi_StDev Exp_i-CAi_StDev

Trading system based on the signals of the i-CAi_StDev indicator.