Importing a library via user external input - can it be done??

 

Hi, Im trying to find a way to import a library (or .dll file) with the filename being defined externally.

For example, this is what I am trying to accomplish:

User creates a file "MY_FILE.mq4" ("MY_FILE.ex4" after compiling), saving it in the libraries folder.

User runs my EA, "supertradesEA.ex4".

In the 'Expert properties' window there is an input string where the user would manually type "MY_FILE.ex4" ... or whatever they name their file

This imports the code from the file the user created.

This is the problem that Im having:

#import requires the filename to be written and put into double quotes as in...

#import "filename.ex4" 

Despite the (" ") format, filename.ex4 apparently is not of string type per se.. therefore, I cannot use the following construct:

extern string inputFile = "TYPE YOUR FILENAME HERE"; // <--- user types this manually in 'Expert properties' window

#import inputFile 				     //--- their code is imported

If I try doing this I get the following error: " 'import' - double quotes needed " ..but of course putting double quotes around it defeats the whole concept.

..any way around this??

 

not possible in plain mql4. The language is just too limited. Although you could call LoadLibrary() from the windows API and also get the function address of an exported function you would still be unable to actually call this function because MQL4 does not have function pointers. MT4 programs are static like blocks of concrete (even C would appear like a dynamic language if compared to mql4), there is no possibility of any sort of late binding, not even through the most creative hacks.

You should implement this mechanism in a DLL that is written in a language that allows dynamically loading and calling libraries at runtime such as Object Pascal (if you prefer structure, orderliness and beauty) or C++ (if you prefer chaos, anarchy and anomaly).

 
You should implement this mechanism in a DLL that is written in a language that allows dynamically loading and calling libraries at runtime such as Object Pascal or C++.

I am fairly new to programming and am not yet familiar with some of the more sophisticated concepts. Are you saying that the DLL file should be written in a more dynamic language? How would that change the fact that the DLL file is being implemented by MQL4..a static language, as you put it.

Perhaps there is another solution to my problem. Late binding would be an overly complex solution in my case..all I need is to import a few instruction from the user to the EA. This only needs to be done once, on initialization.

Is there a way that plain text, located in a .txt file, can be opened, read, and made to appear to the compiler as code within an if() statement?

Example:

I write a text file that reads: "haveinput == true" in plain text, as it is here. I name the file "mytextfile.txt"

Then in my .mq4 file I have something like:

bool haveinput=true;

if( ~~open mytextfile.txt, read it's contents and place the character strings here as code..somehow~~ )
{
	//..do stuff
}

..to the compiler it would appear as:

bool haveinput=true;

if(haveinput == true)
{
	//..do stuff
}

is that even feasible??

-Thanks!!

 
supertrade:

I write a text file that reads: "haveinput == true" in plain text, as it is here. I name the file "mytextfile.txt"

No.

You are trying to make some sort of self-modifying code or dynamically loading or exchanging code at runtime that was not yet known at compile time. This is not possible in mql4. It is possible (more or less complicated) in ALMOST ANY other language but NOT in mql4 because mql4 is severely limited.


Maybe I should ask the question what are you trying to achieve, not what you are trying to do (you explained this already and it will not work) but instead what is the reason WHY you are trying to do this? Maybe there is a better (and entirely different) approach. For example if you just want to load configuration data for some predefined variables then it would be possible without trying to load code at runtime. You would instead only read data (not code) and assign it to some variables.

 

Ok, here goes :)

Im looking to develop a EA such that the user defines their own trading criteria, filtering criteria, etc. The EA is built in a generic way such that it can be easily adapted to employ almost any trading strategy conceivable. The user would only need to type in some simple code to define their trading criteria, then set some external input variables that allow for the use of many popular trading techniques and methods (various entry stop, stoploss, TP algorithms, as well as other tweaks etc).

The issue Im having is in regards to how a user can define their own criteria (which, at this point, requires MQL coding) all the while not having access to the source code. At present, I simply have the source code import an EX4 file (created from a .MQ4 library) that stores a simple function which defines all trading criteria. The function is called within the source code where necessary, and return TRUE or FALSE depending on whether various types of criteria have been met. This imported EX4 file would be provided to the user as a template in MQ4 format, they would only need to alter a few lines of code to redefine the EA's trading strategy entirely.

The problem is that the filename of this imported file must remain static in order for the source code to recognize it. I envision a user whipping up several version of this small template file, each defining a specific trading strategy (i.e. MACrossover.mq4, ScaplingStrategy.mq4,..., ..and so on) and using the source code-driven EA as a black-box to execute/test the specific strategy. But once you change the filename, my source code will no longer recognize the file and obviously will not import it. This means that each time the user wants to switch strategies, they need to go into the same library file, who's name must remain exactly the same, and rewrite/copy-paste new code into the template...very cumbersome!

What I would rather have them do is to create a specific version of the template file once for each strategy, give it a unique name, then simply type the filename into the 'Expert properties' input window. They can even save the input setting and switch strategies by loading those within seconds...this is much more elegant.

What do you think? Possible or beyond MQL4? Perhaps maybe MQL5??

Thanks for you help with this btw, much appreciated!

 

It is possible for one MQL4 indicator/EA to call not-known-at-compile-time MQL4 code via the iCustom instruction.

So a work-around might be to treat the not-known-at-compile-time code as an indicator, pass the EA its name as an input parameter, and pass up to 8 double values back as indicator values 0-7 (or more if the values are constant & treat 'bars age' as another level of value index).

Another way is to write a parser in MQL4 (or even a DLL in another language) and use that to parse the code, and pass in & out values.

 

This is a quite sophisticated project if you are just beginning to learn programming.


You could have a look at the mql4-python bindings, they allow the importing of python modules at runtime and calling of python functions from within mql4. Your user would then define his strategy in python. Unfortunately you cannot call back into mql4 from python, all your python functions can do is to return values or set variables that you can read after the function has been called.

All EA instances would share the same python interpreter, so you would need to encapsulate the user strategy into a class that is instantiated once per running EA (or otherwise make it thread safe). You could then import the file containing this class at runtime, create an instance of it (belonging to this EA/pair/timeframe) and call its methods. The disadvantage is: The user must have python-2.6 installed on his PC and the python wrapper dll in the libraries folder.

 
brewmanz, Thank you, that was a great idea!! I did exactly that and it works perfectly! Very simple &clever workaround!
 
7bit:

This is a quite sophisticated project if you are just beginning to learn programming.


You could have a look at the mql4-python bindings, they allow the importing of python modules at runtime and calling of python functions from within mql4. Your user would then define his strategy in python. Unfortunately you cannot call back into mql4 from python, all your python functions can do is to return values or set variables that you can read after the function has been called.

All EA instances would share the same python interpreter, so you would need to encapsulate the user strategy into a class that is instantiated once per running EA (or otherwise make it thread safe). You could then import the file containing this class at runtime, create an instance of it (belonging to this EA/pair/timeframe) and call its methods. The disadvantage is: The user must have python-2.6 installed on his PC and the python wrapper dll in the libraries folder.



Hi 7bit,
I might have similar Q, but hard to tell.
I will have blocks of C code from a software that is subroutine call-able, up to 64 input parameters, no less than 2. I need an mq4 EA dll that will do this subroutine call upon the EA's command that will fetch the 1 or 0 answer. 
Hope you can help. 
I need the basic barebones of such so that my usual mql programmer can do the overall EA job. He is very proficient at mql for years, but knows not dll. 
You or anyone of this knowledge, please, important.
I'm willing to pay a fair fee for the complete mql-dll-C specifications that can instruct an mql programmer as to what to do.
Email me: batchj2011 at gmail dot com 
Thanks
Truly,
Jerry


 
MQL limits the number of inputs that you can pass to a function. The easiest work around is to create an object with 64 properties, then pass that object as a single argument to the function.
 
texasnomad:
MQL limits the number of inputs that you can pass to a function. The easiest work around is to create an object with 64 properties, then pass that object as a single argument to the function.
How does it help with selecting libraries?
Reason: