The post is devoted to the technique of using .dll resources in MetaTrader4, and more specifically in mql4.
This technique allows the MT4 analytics and functionality to be added with virtually unlimited capabilities, ranging from importing the results of complex calculations (IMSL, MatLab) to writing your own infrastructure that uses MT4 as an adapter to a broker.
Writing .dll with VS
Resources on the Internet say that it is necessary to create MFC .dll, which, firstly, is not available in all versions of VS, and secondly, complicates the code of redundant MFC initializations.
')
As a workout, we will write two functions:
1. The function returns the maximum from the double array.
2. The procedure sorts the array.
Create a project
In MVS: File -> New Project -> Win32 App.
In the window that opens: Next -> ApplicationType: DLL -> Finish
Next we denote the name of the project FxDll .
Immediately add the
FxDll .def file to our project and write the name of two future functions there:
LIBRARY @FxDll
VERSION 0.1
EXPROTS
__getMax
__doSort
I advise you to add a double underscore before the name, so that later in MT4 you should not confuse external functions with internal ones.
We declare functions
Add to the project:
FxDll .h and declare our two functions:
__declspec(dllexport) double __stdcall __getMax(double* arr, const int size);
__declspec(dllexport) void __stdcall __doSort(double* arr, const int size );
What stands before the function looks scary, let's not touch it, this is not the subject of this article - and this is the need to export the function from dll.
Add an
FxDll .cpp file and write functions:
double __stdcall _getMax(double* arr, const int size)
{
return *std::max_element(arr, arr + size) ;
}
void __stdcall __doSort(double* arr, const int size )
{
std::sort(arr, arr + size);
}
It is clear that things are not being done this way, and you need to check for null, to wrap the exception,
and provide a mechanism for transferring this to MT4, but nevertheless it will work on valid data.
And so our part of ++ is ready.
It is necessary to knock out and put the
FxDll .dll file in the directory with the program.
/ experts / libraries
Cooking side MT4
And so we have
FxDll .dll which lies in libraries.
Create a .mqh file - it will tighten the necessary functions from the dll.
//@FxDll.mqh
#import "@FxDll.dll"
double __getMax(double arr[], int);
void __doSort(double &arr[], int);
#import
In the first function, the array is passed by value, and what happens to it outside of MT4 will not recognize.
In the second link. How this is done in MT4 I do not know, but perhaps the same meaning as in C ++.
On this description of the task is completed. We have two functions in MT4 that throw information into external resources and get the result.
Turn on the fantasy and think about what to do next.