Coding for both MQL4 and MQL5

 

would it be possible to use 

https://www.mql5.com/en/docs/basis/preprosessor/conditional_compilation

#ifdef, #include, etc. 

to have one codebase for both mq4 and mq5 variants of an EA?

the main logic is the same but might call different order functions and other such functions which are different in MQL5 than 4. I mean lets say I have Orders4.mqh and Orders5.mqh headers and I call a ListOpenTrades() function that is present in both of those files and use the conditionals to decide to include the correct mqh file for the version of metatrader.

I have no problem with porting an EA from 4 to 5 but what I'm trying to avoid is having to update 2 versions of the same EA every time I make a change or add a feature.

hmm I just  found

https://www.marketcalls.in/metatrader/mql4-mql5-cross-compiling-metatrader.html

does this mean that all MQL5 functions work in MQL4? I think that's what I read... so all the MQL5 order related functions work also in MT4?

Documentation on MQL5: Language Basics / Preprocessor / Conditional Compilation (#ifdef, #ifndef, #else, #endif)
Documentation on MQL5: Language Basics / Preprocessor / Conditional Compilation (#ifdef, #ifndef, #else, #endif)
  • www.mql5.com
Conditional Compilation (#ifdef, #ifndef, #else, #endif) - Preprocessor - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
ycomp: does this mean that all MQL5 functions work in MQL4? I think that's what I read... so all the MQL5 order related functions work also in MT4?

Not at all. The trading and styles are completely different.
          Migrating from MQL4 to MQL5 - MQL5 Articles № 18 (2010)
          Transferring Indicators from MQL4 to MQL5 (2010)
          MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5 (2016)
          Market Condition Evaluation based on standard indicators in Metatrader 5 - Trading Systems - MQL5 programming forum - Page 222 #2218 (2017)
          Problem with drawing indicators in mql5 - General - MQL5 programming forum #2 (2021)

 

It is possible, but MT5 has some functionality that just cannot be replicated in MT4.

I often use macros to make different functionality between MT4 and MT5, for example:

#ifdef __MQL4__

   void DoTradeInMT4(...) { }

   #define TRADE_FUNCTION DoTradeInMT4

#else //or endif & ifdef mql5

   void DoTradeInMT5(...) { }

   #define TRADE_FUNCTION DoTradeInMT5

#endif

//...

TRADE_FUNCTION(...)

In this case I just make the name into a macro (they should have the same parameter types, or at least they should work in both compilations), but you can also make macros without functions if you're careful not to use them like they are one. This macro doesn't have paremeters so the parenthesis are just put after it (not as a parameter of the macro)

You'll need to learn the differences between how each platform operates though (for example MT4 uses long for tickets and MT5 uses ulong) and if there are limitations that you can't recreate in your EA

You can also add includes inside of the conditional macros, or specific objects in one of them (like those in CTrade library)

 
Manuel Alejandro Cercos Perez #:

It is possible, but MT5 has some functionality that just cannot be replicated in MT4.

I often use macros to make different functionality between MT4 and MT5, for example:

In this case I just make the name into a macro (they should have the same parameter types, or at least they should work in both compilations), but you can also make macros without functions if you're careful not to use them like they are one. This macro doesn't have paremeters so the parenthesis are just put after it (not as a parameter of the macro)

You'll need to learn the differences between how each platform operates though (for example MT4 uses long for tickets and MT5 uses ulong) and if there are limitations that you can't recreate in your EA

You can also add includes inside of the conditional macros, or specific objects in one of them (like those in CTrade library)

Thanks, for my use case converting from MQL4 to 5 of some fairly simple (< 1000 lines of code, well including porting of my libraries I guess  < 2000) and not needing to use asynchronous orders.. I ported it all in about 5 hours yesterday by cheating, using the MT4 Orders library. And it seems to work.

So now it should be a straightforward job to insert preprocessor conditionals into the MQL5 code to make it MQL5 & 4 compatible.

 

hmm well that was easy

for anyone wondering what I did in case they want to port MQL4 to MQL5 hedging and don't care much about using asynchronous orders. 

How I made the Universal Version

  1. I wrote my EA in MQL4
  2. I ported it to MQL5
  3. I converted my MQL5 version to a UNIVERSAL version. Basically just editing the .mq5 and associated .mqh includes so that they contained #ifdef __MQL5__ and #else (for the MQL4)
  4. I copied the .mq5 and .mqh files to MT4 and renamed the .mq5 to .mq4

How I ported to MQL5 

  1. Copied the .mq4 to .mq5
  2. Commented everything out and started uncommenting, I guess this is the typical way to port anything. I left the order code commented out the longest.
  3. as I uncommented, I would syntax check it (kind of like compiling but without the compile step - anyhow if you use the metaeditor you can just compile it unless it can do the syntax check thing also) to make sure my code was ok. If I found any MQL4-only code I would convert it to MQL5.. how I did that was to encapsulate it into a function and add this function to a myMQL5.mqh include. This makes conversion to the universal version a piece of cake because mostly I just had to edit that mqh file and put ifdef/else in every function there.
  4. inserted the include for the MT4 Orders library from 2016 into the .mq5. Made sure to copy all dependent files for it from the MT5 terminal source code. Oh well I guess that is not a normal step for most people, I develop my metatrader stuff in a separate non terminal folder with VS Code as I find it convenient, especially due to the git integration.

 
ycomp: would it be possible to use https://www.mql5.com/en/docs/basis/preprosessor/conditional_compilation

#ifdef, #include, etc. 

to have one codebase for both mq4 and mq5 variants of an EA? the main logic is the same but might call different order functions and other such functions which are different in MQL5 than 4. I mean lets say I have Orders4.mqh and Orders5.mqh headers and I call a ListOpenTrades() function that is present in both of those files and use the conditionals to decide to include the correct mqh file for the version of metatrader.

I have no problem with porting an EA from 4 to 5 but what I'm trying to avoid is having to update 2 versions of the same EA every time I make a change or add a feature.

hmm I just  found https://www.marketcalls.in/metatrader/mql4-mql5-cross-compiling-metatrader.html

does this mean that all MQL5 functions work in MQL4? I think that's what I read... so all the MQL5 order related functions work also in MT4?

Yes, it is possible, and I do that all the time, for EAs and Indicators. However, not all MQL5 functionality is not available on MQL4, so sometimes you have to code for the lowest common denominator instead.

In the CodeBase I have example indicators that do that. I don't have any examples of EAs in the CodeBase, however.

Code Base

Exponential Range Average & Deviation Offset

Fernando Carreiro, 2022.06.25 16:02

An exponential moving average of the true range and the offset of its average deviation

Code Base

Constant Range Channel

Fernando Carreiro, 2022.05.11 13:13

A simple indicator plotting a channel with a constant range