A prototype / tutorial for connecting unmanaged code in MT4 (MQL) to managed code (C#) in modern .NET (6+) using DNNE.
The goal of this project was to create a simple, reuseable and generic C# library project who's dll can be loaded into MQL in order to extend MT4 to take advantage of any modern .NET solution (.NET 6+). No actual foreign exchange trading strategies will be included in this project, it's just a prototype showing how to do the "plumbing" in order to enable your own ideas using C# and modern .NET. Note: Passing strings from MQL to C# is supported, but from C# to MQL is not currently supported.
- Download or clone the repository into Visual Studio.
- Make sure Debug and Release condigurations are set to x86.
- Restore/update DNNE from NuGet.
- Compile the project and publish it to a folder with the settings below (see section on DNNE below for details).
- Copy YourProjectName.dll, YourProjectNameNE.dll and YourProjectName.runtimeconfig.json to the /MQL/Libraries folder (see section on MT4 below for details).
- Normally, for MT4, you would copy any extra dlls (from added projects or NuGet packages) to the MT4 instance's installation folder (ex: C:\Program Files (x86)\MT4 DEV), but with DNNE loading the files, you must instead place all the dll's in the libraries folder.
- Copy the MQL file DNNETest.mq4 to /MQL/Experts.
- Open the MQL Editor and compile the EA.
- Add EA to running chart by dragging it from the Navigator window.
MetaTrader 4 is an electronic trading platform for foreign exchange traders by MetaQuotes. It has a simple built in programming IDE (MetaQuotes Language Editor, press F4 inside MT4) with is own language (MQL) which has a C++ like syntax. MQL allows traders to automate actions and strategies as well as connecting to external tools such as MatLab, R or AI. MT4 itself is available for free from any number of Forex brokers, just open a demo account and download the client. Note: Since version 600, MT4 has implemented antidebug software stopping external debugging.
- You can program visual clues as "Indicators", or automate trading using "Expert Advisors" (EA). This project is an EA, but can be called the same way from an indicator.
- To allow autotrading and usage of external code in MT4, go to Tools, Options, Expert Advisors and enable "Allow automated trading", "Allow DLL imports".
- If calling web apis from MQL, check "Allow WebRequests for listed URL" (+ the URLs to be used).
- MT4 only accepts "x86" runtime compilations (so no "AnyCPU")
- Normally, the initial external file such as your main C# dll must be placed in the MT4 instance's "Data Folder"/MQL4/Libraries/ folder, while any additional files (external projects, NuGet packages etc.) would be placed in the MT4 instance's installation folder to be found. HOWEVER, using DNNE to load files means all external files will be loaded from the same directory as your starting point; so put ALL files in /MQL4/Libraries/ folder.
- The included MQL sample "DNNETest.mq4" is an EA and must be placed in the "Data Folder"/MQL4/Experts/ folder and compiled there.
- Your compiled code can now be run by dragging the file from the Navigator window onto the active trading window.
- MQL uses the "Expert" window for logging, rather than the normal "Journal" window.
- MQL strings are unicode.
- MQL dates uses Unix time (1/1/1970).
Dot Net Native Exports (DNNE) is an open source project being developed by Aaron Robinson (a "product owner" of .NET InterOp at Microsoft). Source code is available at https://github.com/AaronRobinsonMSFT/DNNE, and it's installable from NuGet.
- Note that DNNE must be set to "win-x86" in .csproj, along with some other properties referred to as DNNE.props in the guide.
- Due to DNNE using compiler settings, the project must be published to a folder, don't just take the files from the /bin directory.
- With current settings, your code in YourProjectName.dll is executed by DNNE in YourProjectNameNE.dll (as an adapter) while the .NET version is wired up in YourProjectName.runtimeconfig.json. To merge files, see DNNE guide.
- Correct .NET Runtime (the release AND must be x86 version) must be installed on the MT4 instance' machine.
Great thanks to Jerzy Brzozowski ([@GroM] https://www.github.com/GroM), Andrei Gireada, Jimmy Luo, [@ThaDaVos] (https://github.com/ThaDaVos) and [@AaronRobinsonMSFT] (https://github.com/AaronRobinsonMSFT) who did all the work. I just asked questions and pestered people for help.