Simple MT4 - Java bridge

 
Hi All,

I'm a JAVA programmer and I use MT4 since one year. Although MQL is very simple and easy to learn, some things are really painful to code in this language. A few weeks ago a thought about a simple integration from logic-code written in Java into my metatrader EA. I searched the internet and this forum for a solution and I found some interesting Tools (MT4-Java API https://forum.mql4.com/27050, Java Class to DLL converter, ... ) BUT all have their pro and cons and so I decided to write my own MT4-Java integration.

I had the following four MUST HAVE requirements for my solution:

1. Simple Installation
2. Debugging capabilities for JAVA code
3. Speed

4. No network communication between MT-Terminal and another process


One thing which was not important for me, was the need for calling MT4 functions (indicator, trading functions) from within my Java code. For technical analysis I use the open source library from www.ta-lib.org and order handling is still done in the MQL-code. The Javacode simply informs the MQL code when it’s time to BUY or to SELL.

All what you need is MetaTrader, an installed JVM (1.6) and the attached ZIP file which contains the MQL code and the DLL for the JVM container .

Copy the files from the ZIP to your MT installation directory (keep the directory structure) and edit the mt4j.properties file and adjust the path to your jvm.dll file. This is all to run the mt4Sample EA.

The Sample implementation doesn’t do anything. It’s just a skeleton to show you what’s possible. Read the comments inside the files MT4BasicClient.java and SampleEA.java. The JAVA sources you can find inside the mt4jSample.jar. Your own class must implement the MT4BasicClient interface or must extend the class AbstractBasicClient. Don’t forget to add the directory or JAR file with your own classes to the classpath (see mt4j.properties).

Your Java Classes are loaded and executed by a JVM which runs inside the terminal Process. This will give you optimal performance. I did some performance tests and counted how often processTick() was executed during a MT backtest. The average calls per Second was 400.000 calls/second, which I think is fast enough for my needs.

The JVM was configured to create a debug port at 8001 so you can use your favorite IDE and debug your components.

All resources to try out my little MT4-Java bridge are attached to this post.

Have fun :-)
Cyberflohr
Files:
mt4j_v0.1.zip  17 kb
 

Nice. Thanks. However I cannot find https://forum.mql4.com/270509 - it shows an error. Also, can you please post your code. Java and mt4 is something that I want to use but need to see the code.

 

I've corrected the link: https://www.mql5.com/en/forum/121466

You can find the MQL and Java code in the ZIP file.

 

this is an excellent tool

somehow I can not get it to work. can you help me.

when I run the EA it crashes mt4

I am using windows 7 64bit, Java 7.0.1, MT4 ver4.00 build 409 25 oct 2011

java is installed here C:\jre\bin\client\jvm.dll

mt4 is installed here C:\MT4\

windows gives the following error message

Problem signature:

Problem Event Name: BEX

Application Name: terminal.exe

Application Version: 4.0.0.409

Application Timestamp: 0028f000

Fault Module Name: MSVCR100.dll

Fault Module Version: 10.0.30319.1

Fault Module Timestamp: 4ba1dbbe

Exception Offset: 0008ae6e

Exception Code: c0000417

Exception Data: 00000000

OS Version: 6.1.7601.2.1.0.256.1

Locale ID: 2057

Additional Information 1: a0db

Additional Information 2: a0db43c64a51ee21486e76f538ab825d

Additional Information 3: 5a50

Additional Information 4: 5a50e927a493324cabb3491f7239cdd1


Read our privacy statement online:

http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409


If the online privacy statement is not available, please read our privacy statement offline:

C:\Windows\system32\en-US\erofflps.txt

 
Sorry for the delayed reply - but somehow i'll not be notified on new posts in this thread.


There are two possible reasons why your MT4 terminal crashed

1. You use a 64bit JVM

This will not work, because MT4 is a 32bit application. You must install a 32bit JVM on your Win7-64bit, which can be used together with MT4.

JVM Version check should not show "64 Bit"

C:\jre\bin\java -version

Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)

2. Java 7

Till now I've tested my programm only with Java6. Maybe there are some changes in Java7 which trigger the crash.

 

New Version 0.2 is available which will fix the above terminal crash.

Files:
mt4j_v0.2.zip  17 kb
 

Hi cyberflohr,

I'm novice to MT but have some experience with Java so the bridge is very interesting tool for me. I have performed all the installation step you have mentioned, yet wasn't able to launch the example. I got following in the experts log:

20:53:13 mt4jSample EURUSD,H4: loaded successfully

20:53:13 mt4jSample EURUSD,H4 inputs: IntProperty=99; BoolProperty=true; DoubleProperty=1.23; StringProperty="helloWorld";

20:53:17 mt4jSample EURUSD,H4: cannot load library 'mt4j.dll' (error 126)

20:53:17 mt4jSample EURUSD,H4: expert stopped

20:53:17 mt4jSample EURUSD,H4: initialized

20:54:27 mt4jSample EURUSD,H4: loaded successfully

20:54:27 mt4jSample EURUSD,H4: cannot load library 'mt4j.dll' (error 126)

20:54:27 mt4jSample EURUSD,H4: expert stopped

20:54:27 mt4jSample EURUSD,H4: deinitialized

20:54:27 mt4jSample EURUSD,H4: uninit reason 1

20:54:27 mt4jSample EURUSD,H4: removed

What can be the cause of the problem?

 
white123right:

Hi cyberflohr,


20:53:17 mt4jSample EURUSD,H4: cannot load library 'mt4j.dll' (error 126)


What can be the cause of the problem?

Hi

Error 126 to my understanding means that MT4 cannot find the dll file. Assuming that you are calling it by it's correct name it needs primarily to be in the /experts/libraries/ folder. It can go elsewhere but this is your best location.

Hope this helps.

Cheers

Brooky

 

Hi Cyberflohr,

I followed you r instruction to install the mt4jv0.2 and got no any error reported in MT4 platform. However from the SampleEA.java I find that there is a method which seems to be run when the EA initialization according to the description in MT4BasicClient.java

code in SampleEA.java:

@Override

public void init() {

System.out.println("init()");

}

description in MT4BasicClient.java:

/**

* Called by the MT4 client during init phase.

*/

From these code I think there shall be something to be printed out while initialization. but U didn't see the "init()" message get printed in MT4 platform. Do I understand the code correctly?

Besides, can you give another demo to place an order?

thank you very much, I really appreciate your code. :D

 
brooky29:

Hi

Error 126 to my understanding means that MT4 cannot find the dll file. Assuming that you are calling it by it's correct name it needs primarily to be in the /experts/libraries/ folder. It can go elsewhere but this is your best location.

Hope this helps.

Cheers

Brooky

correct - the mt4j.dll is not in the libraries folder.

i.e: if your metatrader installation folder is c:\program files\metatrader, then the files for the mt4j bridge must be at the following locations.

c:\program files\metatrader\experts\mt4jSample.ex4
c:\program files\metatrader\experts\mt4jSample.mq4
c:\program files\metatrader\experts\libraries\mt4j.dll
c:\program files\metatrader\experts\libraries\mt4j.properties
c:\program files\metatrader\experts\libraries\mt4jSample.jar

PS: check the bridge logfile for further informations about the error

c:\program files\metatrader\experts\files\mt4j.log


 
limk0032:

Hi Cyberflohr,

I followed you r instruction to install the mt4jv0.2 and got no any error reported in MT4 platform. However from the SampleEA.java I find that there is a method which seems to be run when the EA initialization according to the description in MT4BasicClient.java

code in SampleEA.java:

@Override

public void init() {

System.out.println("init()");

}

description in MT4BasicClient.java:

/**

* Called by the MT4 client during init phase.

*/

From these code I think there shall be something to be printed out while initialization. but U didn't see the "init()" message get printed in MT4 platform. Do I understand the code correctly?

Besides, can you give another demo to place an order?

thank you very much, I really appreciate your code. :D

unfortunately writing to System.out / System.err will not show up messages in your MT terminal. you must write your messages to a file (using java.io) or use a java logging framework like log4j or slf4j.

I'll prepare a more complex sample which do some trades in the next few days.

regards,
cyberflohr
Reason: