Exchanging quotes between MT4 terminals

 

I know MT4 can export quotes as a DDE server to other programs, is there any way to have MT4 import quotes from other MT4 terminals?

Only the current bid and ask prices are relevant, history isn't.

 
 
BStrayer:

I know MT4 can export quotes as a DDE server to other programs, is there any way to have MT4 import quotes from other MT4 terminals?

Only the current bid and ask prices are relevant, history isn't.

You can use named pipes.
 
Global vars are not accesible from other instances of MT4 are they?
 

As far as I understand each terminal has it's own environment and it's own global vars.

It is not very good solution, but you can use files to exchange data. For ex., one ea writes data on terminal 1, another reads on terminal 2.

 
BStrayer:
Global vars are not accesible from other instances of MT4 are they?

no, they aren't.


This problem can be solved in the following ways.

  1. implement a TCP client server protocol for letting one (or more) client MT4 communicate with one server MT4
  2. implement your client server protocol through windows messaging
  3. implement your client server protocol through named pipes
  4. implement some kind of shared memory where many instances on the same machine can access the same variables
  5. implement some kind of poor man's shared memory through many instances accessing the same file on the harddisk.
  6. implement some kind of poor man's messaging by creating individual files in a folder that will form some kind of message queue (the sender will create one file for each message and the receiver will delete each file after processing it).
The last ones are used often in the MQL4 scene because although it is the ugliest of all possible hacks it seems to be quite simple to get something up and running with minimal effort (minimal studying and understanding of more advanced APIs, quick and dirty) and it is easy to debug but it also has its pitfalls (beware of race conditions when reading the file while it is still being written, etc). I am using method 6 myself to easily send market order commands from MT4 to my proprietary Oanda interface which runs as a completely separate process.
 
7bit:

no, they aren't.

Yes they are. You are confusing global variables which exist outside of all functions in an EA with Global variables which exist across EA instances. There is a suite of functions to handle these variables. Please READ the link I posted above and also refer to the alphabetic list of functions at the top of this forum for those functions beginning GlobalVariable.........() .

CB

 
cloudbreaker:

Yes they are.

No they aren't.

Global variables belong to one installation of MetaTrader and they cannot be used for communication across multiple installations. Not even for multiple running instances of the same terminal.exe from the same installation because they are only saved to disk when terminal.exe exits.

The OP wants to communicate with other mt4 terminals, not between 2 EAs running in the same terminal. Please READ the OP's question and also the posting I answered.

 
7bit:

no, they aren't.


This problem can be solved in the following ways.

  1. implement a TCP client server protocol for letting one (or more) client MT4 communicate with one server MT4
  2. implement your client server protocol through windows messaging
  3. implement your client server protocol through named pipes
  4. implement some kind of shared memory where many instances on the same machine can access the same variables
  5. implement some kind of poor man's shared memory through many instances accessing the same file on the harddisk.
  6. implement some kind of poor man's messaging by creating individual files in a folder that will form some kind of message queue (the sender will create one file for each message and the receiver will delete each file after processing it).
The last ones are used often in the MQL4 scene because although it is the ugliest of all possible hacks it seems to be quite simple to get something up and running with minimal effort (minimal studying and understanding of more advanced APIs, quick and dirty) and it is easy to debug but it also has its pitfalls (beware of race conditions when reading the file while it is still being written, etc). I am using method 6 myself to easily send market order commands from MT4 to my proprietary Oanda interface which runs as a completely separate process.

In terms of speed I guess 3 and 4 would be the best options, 5 could work on a ram-disk instead of a harddisk.

Hadn't thought about race condition problems yet though, that would complicate things a lot. Do you know of any examples or articles on shared memory in MT4? I couldn't find any.

The amount of MT4 terminals needing to access each other's data is 2 or possibly 3 by the way, no more than that.

 

I don't think you need a RAM disk. Windows' disk cache (which is by default activated for all non-removable media) will cache the write and make it at the same time available for all read operations which will then be served directly from the cache. The written data should be available immediately after flush().

flush() will write to the cache, not the disk, so it is fast. The disk cache is one level deeper and transparent to all userland file functions (The disk cache behaves like it *is* the disk towards all layers above it, if the cache is hot then even a 3.5'' floppy feels and behaves like a RAM disk). A real physical sync to disk is slow and can only be enforced with some other API functions (and might even need root privileges) and is usually only done during idle time or before unmounting a media.

I use a system where an MT4 EA will create a ticket file (with a timestamp plus some randomness as filename) for each transaction that has to be mirrored on oanda. It is a text file with two lines and looks like this:

EUR/USD
-20000

this means sell 20000 units (0.2 lot). The other program will permanently poll this folder and immediately open each file that appears there and process it and then delete it. Occasionally it happens that it will find a file that has already been created but not yet filled with data. I simply count the line endings after opening and if it has not exactly 2 line endings in it i close it and continue the polling loop. It will be picked up again in the next iteration and then it will be complete.

I could achieve the same if I simply created the file elsewhere and then move the complete file to the queue folder in one atomic action but MQL4 does not have any functions to move or rename existing files and I intentionally did *not* want to use imported file functions from the windows API.

if you want to transfer quotes you would append new data to the same csv file on every tick and there also might be conditions where the last line is not yet completely written while you are trying to read it already. You must somehow make it able to detect whether a new line of data that has been read is really complete and retry reading the same line until it is.

Shared memory can also create race conditions but you can implement some kind of locking mechanism (a lock that must be acquired before and released after accessing any of the shared data). You will most likely exceed the possibilities of MQL4 (or at least make it unnecessarily complicated) with the more sophisticated approaches and should do this with an external DLL written in a more powerful language since you will need more complicated data structures than just generic ints and doubles.

 

Hi

I need help on how to write MT 4 formula for MS Excel worksheet in multiple time-frames to be able to see what a currency pair has done in the last 12 hours, 24 hours, last week and month in the for of heat-map visually just like it is presented on this website http://finviz.com/forex_performance.ashx so that you know the strength and weakness of different pairs over a period of time, be able to know which one is trending, consolidating and stall.

Thanks.

Reason: