- "Could not establish connection to terminal server: 127.0.0.1:7788" ? I have that problem in my JFXExample.java.
- You need to start NJ4X Terminal Server. One way to do that is to use
.Net users can start NJ4X Terminal Server from a Windows "Start>All Programs>NJ4X API" menu.
- "No connection to server" ..? Ok, I started Terminal Server. So now the message has changed: Exception in thread "main" Terminal client disconnect... java.io.IOException: No connection to server: com.jfx.net.TerminalServer$ClientWorkerThread$NoSrvConnection: :443
- Now you are experiencing Forex Server connection problem, which is due to the expired/wrong configs.
Here is how to update client terminal config files into the NJ4X Terminal Server or how to configure an additional Broker to work with:
- 1. Start your client terminal and select "File"->"Open Data Folder" menu item.
2. Copy all *.srv files from the
"config"dir to the Terminal Server
3. Use name of the srv file as an argument to the
If it is
"ATCBrokers-Demo.srv"then connection string would look like
jfxExample.connect("127.0.0.1", 7788, new Broker("ATCBrokers-Demo"), "12345678", "abcdef");
- Can another strategy written in a native query language be loaded at startup by specifying it in a config file somewhere?
- Sure. There are ea and chr Terminal Server directories (i.e.
C:\Users\[USER]\jfx_term\chr) You can put additional ex4 strategy files to
...\jfx_term\eadir and they will be copied to JFX-managed directories.
...\jfx_term\chrshould be used for chart templates instantiating custom native strategies.
- Can the terminal when executed be visible?
- There is a
-DSW_HIDE=falseparameter (to start Terminal Server with) in order to show all app-managed terminals. To set this parameter you need to modify
run_terminal_server.bat, which already contains commented lines for different configurations.
- Do you know why some brokers won't allow us to connect through NJ4X? We have seen some brokers that give us invalid user/password combination errors with perfectly valid credentials.
- Be sure that your *.srv files for those brokers are up to date. They should be located under the
c:\Users\[USER]\jfx_term\srvdirectory. Note: the minimal Windows version supported by NJ4X is Windows XP SP3.
- How can I debug a strategy? -Xdebug?
- Like any other java/.Net code: use your favorite IDE to put breakpoints
- How can I run Terminal Server on WINE (Linux)?
- (Thanks to FutureScalper, who provided this answer) Just a few minutes ago, I was successful in running Terminal Server on WINE. To do this, I created a WINE configuration as follows, specifically creating a 32-bit only configuration: WINEARCH=win32 WINEPREFIX=~/.wine32 winecfg Then I installed 32-bit Oracle Java 7 JRE Then I copied over my generic MetaTrader tree into WINE's "Program Files" folder (note this is a purely 32-bit WINE configuration. I used the regedit to assign jfx_activation_key system-wide. This is using wine regedit & and then in HKEY_CURRENT_USER/Environment in the WINE-managed Windows registry associated with the WINEPREFIX=~/.wine32 I ran the NJ4X terminal server, and it prompted me to select the terminal.exe and I navigated to the MT4 installation. I copied my Brokerage.srv files into the jfx_term/srv folder. (Using winscp from my windows development system) I noted that Terminal Server was now logging into the jfx_term/log folder correctly !!! This was good news, and, indeed it turned out that it was working. Then I ran my client software, and the connection was perfect !!! So I am unsure whether using the WINEARCH=win32 is required, or not. But it worked for me, when configuring the WINEPREFIX as ~/.wine32 (an arbitrary name, for the "wine bottle") Anyway, it may be helpful to write up a short configuration guide for Linux WINE users, but it looks like my problem is resolved.
- How can I test my strategy in java with client terminal?
- Testing of JFX EA goes much in the same way as testing of any other Expert Advisor.
You may find "Forex Java trading strategy testing" and "Forex .Net trading strategy testing" videos at http://nj4x.com/video for details.
Here are the steps:
1. Start client terminal of your choice.
[jfx_path]\examples\expertsto respective experts dir of your client terminal installation. Follow
*_trader_config_1.jpg - *_trader_config_3.jpgscreenshots (found in examples/experts directory) to configure your client terminal.
- 2. At client terminal open "Tester" window from the toolbar. You may also contact built-in client terminal help, section "Auto Trading>Strategy Testing" for details.
3. Meanwhile you should start your java application to listen to the JFX EA connections, it is described in
4. Follow JFX EA setup screenshots
jfx_test_2.jpg - jfx_test_5.jpg(the "strategy" parameter of the EA should be your JFX strategy fully qualified class name)
- 1. Start client terminal of your choice. Copy
- How have you gotten around the issue of storing a client's logon details? I can imagine this is a security issue and not sure if there is an innovative way to handle it.
- NJ4X API does not keep any client's logon details. You just need to provide them at runtime to
Connect()method, afterwards NJ4X Terminal Server starts terminal.exe, which takes care about authentication.
- I was confused in thinking that jfx instantiated the TradeIt class defined in the parameters of the jfx EA. What parameter is used for that?
"strategy"parameter of jfx EA is used to specify the name of java class, inherited from the basic
Strategyclass, and implementing your own trading algorithms.
- Where does the class file for my strategy need to be located or does it use the machine class path?
- It should be located in the classpath of your java application, which is usually started as a separate (from client terminal)
- Does the Terminal Server need to be running if JFX Expert Advisor attached to client terminal symbol manually?
- No, it does not. in this case Terminal Server is not required.
- I assume that each broker connection would require a separate terminal instance, is that correct? Say I have 100 clients with one broker, but 50 with another?
- Right, for each connected user/broker we need separate client terminal process. In your case it is 150 terminals.
- Is it possible to run the client terminal on some Windows host, an the Java (.Net) API client application on some other Linux host?
- You are absolutely right. It is possible because the communication between NJ4X API client and terminal.exe runs via TCP/IP (one can additionaly configure UDP protocol layer to increases the performance).
- Is there a method to get the directory where the terminal is run from? The directory where you call below as .jfx_terminal directory.
- It is
- Where do all the class files, jar files etc need to be located?
- In any place of any machine accessible over TCP/IP from the client terminal running machine.
How it works
- Can JFX EA reference a class within a jar file directly?
- JFX EA has a number of parameters, one of them is called "strategy", and it should be filled with a fully qualified java class name of your Strategy (inheritor) java class, which in turn can reference any other class within a jar file. In case you write .Net nj4x application, jfx.ex4 EA "strategy" parameter should contain .Net class name and assembly name, e.g. "my.trading.Strategy,trader.exe"
- Can NJ4X framework execute the java (.Net) program more than once a second or longer than a second?
- Yes, you can take complete control once you are inside the
Coordinate()method, and there are no time restrictions for your business logic. Additionally, you can override
CoordinationIntervalMillis()method in order to define any suitable period NJ4X framework will call
- Can it determine what other EA's (not Java/.Net based) are doing?
- NJ4X Java/.Net application can get list of orders (market or historical), get current account equity - this is the only way to determine what other EA's are doing. Check the nj4x forum for the PositionListener functionality.
- Do I have to load an EA into the terminal.exe as the API server part? Is it just an ex4, or do you also require DLLs?
- There are two options (DLL is required in both cases as it is part of JFX EA):
1 - when client terminal is driving: You start client terminal and manually drop jfx.ex4 expert advisor to the chart. Expert Advisor connects then to your java application. This process is detailed in "NJ4X - Forex Java and .NET API - readme.pdf" document, available for download.
2 - when Java/.Net application is driving: You are coding your application to connect to the broker(s) using forex account credentials. It is as simple as
NJ4X Terminal Server starts client terminal in background to serve market requests from NJ4X application. It keeps separate terminal directory structure at your computer under the current user home directory (e.g. c:\Users\brian\.jfx_terminals or C:\Documents and Settings\brian\.jfx_terminals).
- Does this java api send trades directly to the forex server or through the client terminal?
- It is trading through the standard client terminal.
- Does this perform Client Terminal emulation?
- Yes. NJ4X API allows java/.Net application to use almost all original query language functionality available to the native client terminal's strategies. Strategy class descendants override
Coordinate()method which is called periodically by NJ4X framework in order to perform trading tasks. This is similar to how client terminal is calling native strategy's
start()method on each new tick.
- How are arrays passed between java strategy class and EA in Client Terminal?
- There is no need to pass Bid, Ask, High, Low etc. arrays to java application. Instead one can use
strategy.marketInfo()method to get current data or methods like
iOpen, iClose, iHigh, iLow- to retrieve historical data. JFXStreamerExample.java class contains history fetching example (there is also StrategyTest.sc / LoadHistoryTest in tests project of NJ4X for .Net).
- I understood that if attaching jfx EA to the symbol chart manually then a main method is not required. JFX expert advisor calls the init java method and client terminal start calls the coordinate java method of the Strategy class I have created. Is that correct?
- No, it is not. JFX EA does not start any java (.Net) application itself, it just connects to the existing up-and-running one over the network.
- I've found that when it calls coordinate(), it never returns until I am done processing.
- You can stay in the
Coordinatemethod as long as you need. Normally lifecycle of the coordination is short, and you return from
Coordinateas soon as possible. NJ4X framework will execute
Coordinateagain in a configured period (see coordinationIntervalMillis method) afterwards.
- Is an Client Terminal required?
- Yes, it is, you need to install it and copy to NJ4X Terminal Server's directory, NJ4X TS runs client terminals in background upon the application requests.
- What is the purpose of the Strategy.coordinate() method? When is it called by the NJ4X framework?
Strategy.coordinate()is called by NJ4X framework once per
coordinationIntervalMillis()-defined period. It is 60 seconds by default.
Initially it was an analog of the "start" method of a native strategy (expert/advisor) - so it was called when new tick arrives.
Now its purpose is to do your main trading strategy tasks, like monitoring existing positions, rising new market orders etc.
To get real-time ticks use TickListener functionality (more details are in examples and at http://nj4x.com/forum page)
- What is the relationship between the Terminal Server and the generated client terminal(s)?
- Terminal Server manages terminals in terms of creating installation directories (i.e. c:\Users\[USER]\.jfx_terminals\[BROKER]_[ACCOUNT]), starts or stops terminals according to the NJ4X (Java or .Net) application requests.
- I forgot to ask what is the latency of the communication protocol? How long (latency) does it take to submit a trade request and get a response?
- It takes 0.2 millisecond in average for a single round trip between jfx application and terminal (JFXExampl.java contains sample code used to get latency figures below)
21:48:30.438> Latency test...
21:50:03.225> total latency test duration: 92787 millis for 300000 requests
21:50:03.225> Total Req Error Req Min_Req Time Max_Req Time Avg_Req Time cnt: <5ms 10 25 50 100
21:50:03.225> Cnt 300000 ErrCnt 0 Min 0 Max 30 Avg 1 cnt: 299993 2 3 2 0
- I have a problem with strategy tester performance - it is extremely bad. Do you have any suggestions what it can be connected with?
- NJ4X enables 5000 calls per second between app and a single terminal, its more than enough for real-time trading, but tests can suffer as long as native experts are working about 100 times faster.
This is because of network communication between terminal and NJ4X app.
- 1. For tests put all staff (Java/.Net and terminal) to the same machine
- 2. If you do not need to debug your strategy, UDP transport can be used (
-Djfx_use_udp=true(.Net/App.config: "nj4x_use_udp"), see examples)
- 3. Remove redundant forex calls, optimize your code.
- 4. Temporarily enable extended traces by (
-Djfx_server_port=17342(.Net/App.config: "nj4x_server_port")) and check out [Term_Dir]/logs/socket.log and [Term_Dir]/experts/logs
- What research have you done on resources required to run significant number of client terminals?
- Single terminal takes about 10-20MB of RAM, 5MB+cumulative history size on HD and 0.01-1.00% of CPU after the start.
The research has been performed to run normally 40 terminals on the minimalistic configuration virtual box of 1GB+1CPU. 40 terminals consumed 80% of RAM and 10% of CPU.
Update: Test run of 100 client terminals (build 432)
One of the customers have used another configuration for its tests, and it was really hard to reach the limits (~1500 terminals):
Processor Intel Xeon X5650 @ 2.67GHz 2 processors Memory 36GB HD Dell Perc SCSI H700 1.80 TB Net Broadcom BCM5709C NeteXtreme II GigE OS Windows Server 2008 Standard SP2 64bit
- Can I control multiple client terminals connecting to multiple brokers from the same Java (.Net) API application?
- Of course, this is basic "feature" of NJ4X API.
- Can I open multi symbol for a single terminal? (I don't care about ticks from the market)
- Sure, you can have multiple orders opened for any symbol. Please test all functionality you need before making a purchase. Demo version has no limitations except api call delays.
- Can my strategy java class be a jar file?
- Sure, you can build a jar file with your strategy and accompanying classes and point
java.exeto use a classpath containing that jar file, e.g. if it is
tradeit.jar, then run your java application using something like
java.exe -cp %CLASSPATH%;c:\[path]\tradeit.jar Main
- Does the NJ4X Client support multiple brokers in a single JVM?
- Yes, it does. Please see
JFXExample.javafor more details.
.Net users may find it interesting to download nj4x_copier_demo.msi, which contains account copier demo C# source project.
- How to shutdown terminal session ? I meant to shutdown/exit the terminal application - thus stopping all EA's in that terminal. I don't see any way to exit the NJ4X api from user input. Just ctrl-c to stop the application.
- You can use
a) You can call Strategy.Disconnect(true/*immediately*/) in order for the client terminal to be disposed from memory.
b) There is JFX_TERM_IDLE_TMOUT_SECONDS environment variable you can define on the Terminal Server machine.
It is number of seconds the client terminal remains in memory after nj4x client disconnection. You can set JFX_TERM_IDLE_TMOUT_SECONDS to e.g. 10, in order terminal.exe to be kept in memory for 10 seconds (by default it is set to 6 hours).