Remotely controlled Expert Advisor

Not so long ago, Astra mentioned on his blog about an ingenious tool, which is Dropbox. He also mentioned that it can be used as a base for building a remotely controlled EA.

In this entry, I will show you sample solution, which will allow you to set EA parameters from a remote computer without any need to use things such as Remote Desktop, but using Dropbox.

Introduction

Lets assume a sample EA code:

extern double param1;
extern string param2;

int init() {
 // ...
}

int start() {
 // ...
 // we assume the code uses param1 and param2
}

What is the idea behind this solution?
It’s very simple: MQL lets us read files from the experts\files directory, so it is enough to synchronize files directory or any of its subdirectories with Dropbox and then read the settings file regularly and use this settings.

Synchronizing

Making files directory or any other selected subdirectory synchronized is very simple. You only have to write the following command in the command line (Vista/Win7):

mklink /J "D:\My Dropbox\MT4Files" "C:\Program Files\MetaTrader\experts\files"

Of course, we assume that D:\My Dropbox is a Dropbox directory, C:\Program Files\MetaTrader is a MetaTrader directory and that we use NTFS file system.

If you use Windows XP, which doesn’t have mklink command, download the Junction Tool (syntax description is under the link).

After executing this command, Dropbox should immediately start synchronizing the files directory. Before reading further I recommend to check if this really happens – either from Dropbox site, where you can see all files or from another computer, which is synchronized with Dropbox.

Importing settings

Importing settings is quite easy. You only hve to use the following function:

void importSettings(string file) {
   int f=FileOpen(file,FILE_READ|FILE_CSV,"=");
   if(f==-1) return;

   while(!FileIsEnding(f)) {
      string name=FileReadString(f);

      if(name=="param1") param1=StrToDouble(FileReadString(f));
      if(name=="param2") param2=FileReadString(f);

   }
}

Parameters can be added as shown above, but one thing is important: I advice to always use FileReadString function and then convert to the type of a parameter – just because my observations show that such way of doing it guarantees minimal number of bugs, which come from using functions like FileReadDouble (to be honest I don’t know why).

Remote control at work

If we have finished modifying our EA by adding the import function, we can now run it.

In files diretory we create a configuration file, for instance settings.txt, and we fill it with the values of our parameters:

param1=test
param2=2

Then we call importSettings from the start function:

int start() {
    importSettings("settings.txt");
    Comment("param1: "+param1+"\nparam2: "+param2);
    // ...
}

Now you only have to modify the settings file and the EA will automatically read new settings – that’s why it doesn’t matter on which computer (with Dropbox connected) we make the changes – Dropbox will send them on a computer with the EA so as to let it use them.

Share with your friends:

Send us your comments

Name (required)

Email (required)

Message

  • Facebook