Simplest connection between MQL5 and .NET C#

Objectives:
1. Send a random double array from MT5 to a Chart in .NET Framework
2. Send messages and enums, representing events, back from .NET to MT5 for flow control

Step 1 — Create a new Windows Form App (.NET Framework)
Step 3- Set the platform target exactly like MT5 install (either x86 or x64, not Any CPU)
Step 4 — Design your Form with a Refresh Button, Close Button and a Chart (remove the Initialized Series in the chart Series collection property and add a Title)

Step 5 This is the main interconnection class (Important): MQL5 does not natively recognize complex types like structures, classes, etc. If you write a static Method and it is not seen in the MetaEditor , after you imported the DLL and compiled the script, try using only basic argument types (float, int, double, string, etc) ; the same basic types MT5 recognizes.

Step 5 (Important): Pay attention to the Namespace name to be exactly like the Assembly (DLL file must be named exactly like the namespace). The class can be static or not, but methods definitively should be static.

As it can be seen in the code above, the Method ShowForm(name) creates the Form on the fly.
Note: If you named your Form class other than Form1, change the code accordingly

Step 6 — Create Click Methods Button Refresh, Button Close and public method LoadValues ( )

Step 6 : The Form code (Nothing special here!) : Methods are just updating status variables in the static class MQL5CSharp — which are a message back variable to be read from MT5 also an enum representing events — and updating the Chart object with values from MQL5CSharp.values list.

Now, with everything in place, compile the DLL in Visual Studio and copy it to Meta Editor Libraries folder. (If don’t know where it is, open MetaEditor and right click the Libraries folder)

Compiled DLL shall be placed inside Libraries Folder

Now, in Meta Editor, create a new script and type the code below.

Step 7 — Write a MQL5 Script

Step 7 (Important): Needless to say the Enum order in MQL5 should be exactly like the order in C#. And, change the Sleep(1000) to Sleep(100) for faster results;

Step 7 : After writing #import “GuiMT.dll” and compile, you should see the static functions in the DLL if you type MQL5CSharp:: (anywhere in the code editor), the pop-up window below will appear; yes, there are extra Methods not seen in the pictures above which I’ve placed in the Github repository source file.

As it can be seen in the Script, after initialization, Form1 is shown after MQL5Csharp::ShowForm(“Form1”), and the while loop keeps reading enumEvents from .NET, until the close Button event is raised (Enum number 3), which if received, will terminate the script.

There are more methods in the source files on Github (e.g: Max, Min,Sum, GetFiboArray, etc) to demonstrate the usage of Linq with arrays passed from MT5. I suggest you play with it altering the source code.

Always remember to compile the script every time you change the DLL source code, or else the new Methods will not appear. If you compile and the new Method still does not appear, check the parameters (as said before, only basic types are recognized).

Conclusion: After opening MT5 and loading the Script CSharp, a window will appear with randomly generated values from MT5 seen in a Graph. If you press the refresh button on the form, new values will be generated, printed in the MT5 console and the graphic will be updated. The message printed in MT5 console was generated in C#. Pressing the RED close button will end the script.

GitHub repository

Full code repository : https://github.com/camilochaves/MQL5_CSharp

End notes: The solution presented on this article, will integrate MQL5 and C#, in a much simpler version than the one found on https://www.mql5.com/pt/articles/5563 using the GuiController.DLL from Vasiliy Sokolov.

I hope you enjoy! If you have suggestions comment on this article.
Have you cloned the repository and made some improvements? Send a pull request.

In the repository, it can also be seen a forced connection to MQL5 using DLLExport attributes. With this library you can pass complex objects (structs, etc).

.NET Software Developer, Electrical Engineer, Physicist