Directory of RSS feeds
Statistics

RSS feeds in the directory: 2798

Added today: 0

Added yesterday: 0

Business / Finance

MQL4: Write a trade copier for MetaTrader 4

Portal Forex trader 21.04.2017 at 15:00

Forex trading strategies, expert advisors, indicators, video training trade

Good afternoon, friends!

Probably you need to copy the transaction from one of your terminal to another. For example, if you need to open the same position on different accounts or multiple brokers. Manually this work takes time, which you can save if entrust this routine task to the copier. In addition, the copier can be written by yourself.

Today we will learn how to create a simple trade copier for MT4. The process of copying the article is implemented through a shared file directory, so the same approach can be used to transmit any information between several terminals MT4/MT5 on a single PC.

the General idea

that's the Idea. All the terminals installed on the computer, have a common directory. In a shared directory, the Provider creates the file with all open positions. Terminal client reads data from a file and opens the desired position.

This approach is chosen because of its versatility and ease of implementation. But this is not a universal solution because in some situations the desired speed and large capacity, and the files can not provide. However, to copy the normal trade this would be sufficient.

the Main problem of all copiers – sync conditions. First and foremost, we need to somehow link a warrant to a customer order, open the provider. To do this, as the unique order ID (magic number) we will use the order ticket provider. The order ticket may change if was closed part of the position, and this case also need to be processed.

struct Params

{

int account;

char symbol[SYMBOL_LEN];

int ticket;

int magic;

int type;

double volume;

double sl;

double tp;

double equity;

};

so the wizard writes all active positions in the file. The client then reads the data and creates its own copy of the file in the local directory. Thus, to work we need to create functions to read and write, with the ability to access shared and local catalogs.

In this implementation, the EA goes into an infinite loop until I get access to the file. Sharing the shared file is not provided. So we will get rid of the defective data that the customer may mistakenly be taken for a trade signal.

void write(string name,Params &a[],bool local=false)

{

int h;

do

{

h=local ? FileOpen(name,FILE_WRITE|FILE_BIN) :

FileOpen(name,FILE_WRITE|FILE_BIN|FILE_COMMON);

if(GetLastError()!=0)

Sleep(DELAY);

}

while(h==INVALID_HANDLE);

FileWriteArray(h,a);

FileClose(h);

}

void read(string name,Params &a[],bool local=false)

{

int h;

do

{

h=local ? FileOpen(name,FILE_READ|FILE_BIN) :

FileOpen(name,FILE_READ|FILE_BIN|FILE_COMMON);

if(GetLastError()!=0)

Sleep(DELAY);

}

while(h==INVALID_HANDLE);

FileReadArray(h,a);

FileClose(h);

}

the names of the files are created in the initialization function. The name of the shared file is the same for all, as the name of the local backup uses the account number. Immediately call the backup method, which creates a local entry (yet empty) positions in the terminal folder.

void create()

{

account=AccountNumber();

backupName=IntegerToString(AccountNumber());

sharedName="shared";

backup();

}

basically, the method of “backup” creates a backup file through which we will then verify the changes in open positions.

void backup()

{

write(backupName,shared,true);

}

there is Also the option to read from a file, which returns the number of stored positions.

int backup(Params &a[])

{

read(backupName,a,true);

return ArraySize(a);

}

There are methods for sharing of positions. “pull” to retrieve data on the positions of the common file, “share” to save changes to a shared file. In the “share” feature support multiple providers. To eliminate the conflict of multiple master accounts, trying to access one file, each provider identificireba the account number.

int pull(Params &a[])

{

read(sharedName,a);

return ArraySize(a);

}

void share()

{

orders, Params[],temp[];

read(sharedName,orders); // read shared positions

int szO=ArraySize(orders);

int szP=ArraySize(positions);

int szTemp=ArrayResize(temp,0,szO+szP);

// copy all the "wrong" order in a temporary array

for(int i=0;i