NJ4X API – library of access to MetaTrader server for Java and .Net
Terminology
| Advisor (EA) | Advisor (Expert Advisor) is mechanical trading system (МТS) |
| NJ4X | Java and .Net interface for access to MT4/5 terminal, previous name – JFX API |
| NJ4X Server | Socket server (part of Java and .Net interface for access to MT4/5 terminal), which receives connections from MT4/5 terminal and initializes NJ4X strategies |
| NJ4X Strategy | Trading strategy, realized via NJ4X API library. Usually it is presented by java-class, inherited from com.jfx.strategy.Strategy or .Net object, inherited from nj4x.Strategy. |
| MT4/5 Server | MetaQuotes Software Corp. trading server. MT4/5 server receives trading orders and other requests from trading terminals for further execution |
| MT4/5 Terminal | Client MetaTrader 4 terminal is a trader workplace, which allows to work on Forex, CFD and Futures financial markets. Java and .Net access interface (NJ4X) launches MT4/5 terminal in background mode and uses it for access to MT4/5 Servers |
| MQL4/5 | Programming language by MetaQuotes Software Corp for development of advisors, which interact with MT4/5 trading servers. |
| NJ4X Terminal Server | Utility that launches MT4/5 terminals in background mode on request from mechanical trading system, which uses the NJ4X library. |
Overview
NJ4X API is a Java and .Net library for accessing MetaTrader servers using standard MT4/5 terminal. It helps mechanical trading system developers to avoid using MQL4/5 language and to code in their preferred environments. MT4/5 terminal acts as a mediator between MT4/5 Server and the NJ4X program.
NJ4X library allows Java or .Net programs to interact with multiple MT4/5 servers at once. Some benefits are:
- Java/.Net advisors can be developed with any IDE
- Multiple trading accounts can be automated and managed simultaneously.
- Complex trading systems can be well organized. MQL is not suitable for that.
NJ4X API step by step usage guide
- Create Visual Studio project
- Add NJ4X and NLog nuget packages
- Add reference to System.Configuration package:
- Edit App.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>
<appSettings>
<!-- Your nj4x_activation_key can be obtained at https://www.nj4x.com/downloads under the licensed user logon -->
<add key="nj4x_activation_key" value="209084108"/>
<add key="nj4x_mt5_activation_key" value="3546292279"/>
<add key="terminal_host" value="127.0.0.1"/>
<add key="terminal_port" value="7788"/>
<!-- -->
<add key="broker" value="MetaQuotes-Demo"/>
<add key="account" value="130240008"/>
<add key="password" value="4ieubwl"/>
<!-- -->
<add key="nj4x_server_host" value="127.0.0.1"/>
<add key="nj4x_server_port" value="7778"/>
</appSettings>
</configuration>
using System;
using System.Configuration;
using nj4x;
using nj4x.Metatrader;
namespace nj4x_p1
{
class Program
{
static void Main(string[] args)
{
// Create strategy
var mt4 = newStrategy();
// Connect to the Terminal Server
mt4.Connect(
ConfigurationManager.AppSettings["terminal_host"],
int.Parse(ConfigurationManager.AppSettings["terminal_port"]),
newBroker(ConfigurationManager.AppSettings["broker"]),
ConfigurationManager.AppSettings["account"],
ConfigurationManager.AppSettings["password"]
);
// Use API methods ...
Console.WriteLine($"Account {mt4.AccountNumber()}");
Console.WriteLine($"Equity {mt4.AccountEquity()}");
//
Console.ReadLine();
}
}
}
for (int i = 0; i < 10; i++)
{
double bid = mt4.Marketinfo( symbol: "EURUSD", type: MarketInfo.MODE_BID);
double ask = mt4.Marketinfo("GBPUSD", MarketInfo.MODE_ASK);
Console.WriteLine($"EURUSD bid={bid}");
Console.WriteLine($"GBPUSD ask={ask}");
Task.Delay(100).Wait();
}
try
{
var ticket = mt4.OrderSend(
symbol: "EURUSD",
cmd: TradeOperation.OP_SELL,
volume: 0.1, price: bid,
slippage: 2,
stoploss: 0, takeprofit: 0,
comment: "my order",
magic: 0, expiration: MT4.NoExpiration
);
Console.WriteLine($"New order: {ticket}");
}
catch (MT4Exception e)
{
Console.WriteLine($"Order placing error #{e.ErrorCode}: {e.Message}");
}