Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

4 Commits

Repository files navigation

metaapi.cloud SDK for Java

Note: The Java SDK was updated long time ago and lacks important bugfixes and updates, and in order to access our service using Java SDK you need to request an explicit permission to do so via online chat. We plan to update the java SDK sometime in future. Meanwhile we recommend you to use javascript or python SDK instead.

MetaApi is a powerful, fast, cost-efficient, easy to use and standards-driven cloud forex trading API for MetaTrader 4 and MetaTrader 5 platform designed for traders, investors and forex application developers to boost forex application development process. MetaApi can be used with any broker and does not require you to be a brokerage.

CopyFactory is a simple yet powerful copy-trading API which is a part of MetaApi. See below for CopyFactory readme section.

MetaApi is a paid service, but API access to one MetaTrader account is free of charge.

The MetaApi pricing was developed with the intent to make your charges less or equal to what you would have to pay for hosting your own infrastructure. This is possible because over time we managed to heavily optimize our MetaTrader infrastructure. And with MetaApi you can save significantly on application development and maintenance costs and time thanks to high-quality API, open-source SDKs and convenience of a cloud service.

Official REST and websocket API documentation: https://metaapi.cloud/docs/client/

Please note that this SDK provides an abstraction over REST and websocket API to simplify your application logic.

For more information about SDK APIs please check esdoc documentation in source codes located inside lib folder of this npm package.

Working code examples

Please check this short video to see how you can download samples via our web application.

You can also find code examples at examples folder of our github repo or in the examples folder of the project.

We have composed a short guide explaining how to use the example code

Installation

If you use Apache Maven, add this to <dependencies> in your pom.xml:

<dependency>
<groupId>cloud.metaapi.sdk</groupId>
<artifactId>metaapi-java-sdk</artifactId>
<version>14.0.8</version>
</dependency>

Other options can be found on this page.

Running Java SDK examples

In order to run Java SDK examples, follow these steps:

  1. Make sure that you have installed Maven and its command mvn is accessible.
  2. Navigate to the root folder of the example project (where its pom.xml is located).
  3. Build the project with mvn package.
  4. Run mvn exec:java@ExampleClassName where ExampleClassName is the example to execute, e.g. mvn exec:java@MetaApiRpcExample.

Example parameters such as token or account id can be passed via environment variables, or set directly in the example source code. In the last case you need to rebuild the example with mvn package.

Connecting to MetaApi

Please use one of these ways:

  1. https://app.metaapi.cloud/token web UI to obtain your API token.
  2. An account access token which grants access to a single account. See section below on instructions on how to retrieve account access token.

Supply token to the MetaApi class constructor.

importcloud.metaapi.sdk.metaApi.MetaApi;
Stringtoken = "...";
MetaApiapi = newMetaApi(token);

Retrieving account access token

Account access token grants access to a single account. You can retrieve account access token via API:

StringaccountId = "...";
MetatraderAccountaccount = api.getMetatraderAccountApi().getAccount(accountId).join();
StringaccountAccessToken = account.getAccessToken();
System.out.println(accountAccessToken);

Alternatively, you can retrieve account access token via web UI on https://app.metaapi.cloud/accounts page (see this video).

Managing MetaTrader accounts (API servers for MT accounts)

Before you can use the API you have to add an MT account to MetaApi and start an API server for it.

However, before you can create an account, you have to create a provisioning profile.

Managing provisioning profiles via web UI

You can manage provisioning profiles here: https://app.metaapi.cloud/provisioning-profiles

Creating a provisioning profile via API

// if you do not have created a provisioning profile for your broker,// you should do it before creating an accountProvisioningProfileprovisioningProfile = api.getProvisioningProfileApi()
.createProvisioningProfile(newNewProvisioningProfileDto() {{
name = "My profile";
version = 5;
brokerTimezone = "EET";
brokerDSTSwitchTimezone = "EET";
}}).join();
// servers.dat file is required for MT5 profile and can be found inside// config directory of your MetaTrader terminal data folder. It contains// information about available broker serversprovisioningProfile.uploadFile("servers.dat", "/path/to/servers.dat").join();
// for MT4, you should upload an .srv file insteadprovisioningProfile.uploadFile("broker.srv", "/path/to/broker.srv").join();

Retrieving existing provisioning profiles via API

List<ProvisioningProfile> provisioningProfiles = api.getProvisioningProfileApi()
.getProvisioningProfiles(null, null).join();
ProvisioningProfileprovisioningProfile = api.getProvisioningProfileApi().getProvisioningProfile("profileId").join();

Updating a provisioning profile via API

provisioningProfile.update(newProvisioningProfileUpdateDto() {{ name = "New name" }}).join();
// for MT5, you should upload a servers.dat fileprovisioningProfile.uploadFile("servers.dat", "/path/to/servers.dat").join();
// for MT4, you should upload an .srv file insteadprovisioningProfile.uploadFile("broker.srv", "/path/to/broker.srv").join();

Removing a provisioning profile

provisioningProfile.remove().join();

Managing MetaTrader accounts (API servers) via web UI

You can manage MetaTrader accounts here: https://app.metaapi.cloud/accounts

Create a MetaTrader account (API server) via API

MetatraderAccountaccount = api.getMetatraderAccountApi().createAccount(newNewMetatraderAccountDto() {
name = "Trading account #1";
type = "cloud";
login = "1234567";
// password can be investor password for read-only accesspassword = "qwerty";
server = "ICMarketsSC-Demo";
provisioningProfileId = provisioningProfile.getId();
application = "MetaApi";
magic = 123456;
quoteStreamingIntervalInSeconds = 2.5; // set to 0 to receive quote per tickreliability = "regular"; // set this field to 'high' value if you want to increase uptime of your account (recommended for production environments)
});

Retrieving existing accounts via API

// filter and paginate accounts, see docs for full list of filter options availableList<MetatraderAccount> accounts = api.getMetatraderAccountApi().getAccounts(newAccountsFilter() {{
limit = 10;
offset = 0;
query = "ICMarketsSC-MT5";
state = List.of(DeploymentState.DEPLOYED);
}}).join();
// get accounts without filter (returns 1000 accounts max)List<MetatraderAccount> accounts = api.getMetatraderAccountApi().getAccounts(null).join();
MetatraderAccountaccount = api.getMetatraderAccountApi().getAccount("accountId").join();

Updating an existing account via API

account.update(newMetatraderAccountUpdateDto() {
name = "Trading account #1";
login = "1234567";
// password can be investor password for read-only accesspassword = "qwerty";
server = "ICMarketsSC-Demo";
quoteStreamingIntervalInSeconds = 2.5; // set to 0 to receive quote per tick
}).join();

Removing an account

account.remove().join();

Deploying, undeploying and redeploying an account (API server) via API

account.deploy().join();
account.undeploy().join();
account.redeploy().join();

Manage custom experts (EAs)

Custom expert advisors can only be used for MT4 accounts on g1 infrastructure. EAs which use DLLs are not supported.

Creating an expert advisor via API

You can use the code below to create an EA. Please note that preset field is a base64-encoded preset file.

ExpertAdvisorexpert = account.createExpertAdvisor("expertId", newNewExpertAdvisorDto() {{
period = "1h";
symbol = "EURUSD";
preset = "a2V5MT12YWx1ZTEKa2V5Mj12YWx1ZTIKa2V5Mz12YWx1ZTMKc3VwZXI9dHJ1ZQ";
}}).join();
expert.uploadFile("/path/to/custom-ea").join();

Retrieving existing experts via API

List<ExpertAdvisor> experts = account.getExpertAdvisors().join();

Retrieving existing expert by id via API

ExpertAdvisorexpert = account.getExpertAdvisor("expertId").join();

Updating existing expert via API

You can use the code below to update an EA. Please note that preset field is a base64-encoded preset file.

expert.update(newNewExpertAdvisorDto() {{
period = "4h";
symbol = "EURUSD";
preset = "a2V5MT12YWx1ZTEKa2V5Mj12YWx1ZTIKa2V5Mz12YWx1ZTMKc3VwZXI9dHJ1ZQ";
}}).join();
expert.uploadFile("/path/to/custom-ea").join();

Removing expert via API

expert.remove().join();

Access MetaTrader account via RPC API

RPC API let you query the trading terminal state. You should use RPC API if you develop trading monitoring apps like myfxbook or other simple trading apps.

Query account information, positions, orders and history via RPC API

MetaApiConnectionconnection = account.connect().join();
connection.waitSynchronized().join();
// retrieve balance and equitySystem.out.println(connection.getAccountInformation().join());
// retrieve open positionsSystem.out.println(connection.getPositions().join());
// retrieve a position by idSystem.out.println(connection.getPosition("1234567").join());
// retrieve pending ordersSystem.out.println(connection.getOrders().join());
// retrieve a pending order by idSystem.out.println(connection.getOrder("1234567").join());
// retrieve history orders by ticketSystem.out.println(connection.getHistoryOrdersByTicket("1234567").join());
// retrieve history orders by position idSystem.out.println(connection.getHistoryOrdersByPosition("1234567").join());
// retrieve history orders by time rangeSystem.out.println(connection.getHistoryOrdersByTimeRange(startTime, endTime).join());
// retrieve history deals by ticketSystem.out.println(connection.getDealsByTicket("1234567").join());
// retrieve history deals by position idSystem.out.println(connection.getDealsByPosition("1234567").join());
// retrieve history deals by time rangeSystem.out.println(connection.getDealsByTimeRange(startTime, endTime).join());

Query contract specifications and quotes via RPC API

MetaApiConnectionconnection = account.connect().join();
connection.waitSynchronized().join();
// first, subscribe to market dataconnection.subscribeToMarketData("GBPUSD").join();
// read symbols availableSystem.out.println(connection.getSymbols().join());
// read constract specificationSystem.out.println(connection.getSymbolSpecification("GBPUSD").join());
// read current priceSystem.out.println(connection.getSymbolPrice("GBPUSD").join());

Query historical market data via RPC API

Currently this API is supported on G1 and MT4 G2 only.

// retrieve 1000 candles before the specified timeList<MetatraderCandle> candles = account.getHistoricalCandles("EURUSD", "1m", newIsoTime("2021-05-01T00:00:00.000Z"), 1000).join();
// retrieve 1000 ticks after the specified timeList<MetatraderTick> ticks = account.getHistoricalTicks("EURUSD", newIsoTime("2021-05-01T00:00:00.000Z"), 5, 1000).join();
// retrieve 1000 latest ticksList<MetatraderTick> ticks = account.getHistoricalTicks("EURUSD", null, 0, 1000).join();

Use real-time streaming API

Real-time streaming API is good for developing trading applications like trade copiers or automated trading strategies. The API synchronizes the terminal state locally so that you can query local copy of the terminal state really fast.

Synchronizing and reading teminal state

MetatraderAccountaccount = api.getMetatraderAccountApi().getAccount("accountId").join();
// access local copy of terminal stateTerminalStateterminalState = connection.getTerminalState();
// wait until synchronization completedconnection.waitSynchronized().join();
System.out.println(terminalState.isConnected());
System.out.println(terminalState.isConnectedToBroker());
System.out.println(terminalState.getAccountInformation());
System.out.println(terminalState.getPositions());
System.out.println(terminalState.getOrders());
// symbol specificationsSystem.out.println(terminalState.getSpecifications());
System.out.println(terminalState.getSpecification("EURUSD"));
System.out.println(terminalState.getPrice("EURUSD"));
// access history storageHistoryStoragehistoryStorage = connection.getHistoryStorage();
// both orderSynchronizationFinished and dealSynchronizationFinished// should be true once history synchronization have finishedSystem.out.println(historyStorage.isOrderSynchronizationFinished());
System.out.println(historyStorage.isDealSynchronizationFinished());

Overriding local history storage

By default history is stored in memory only. You can override history storage to save trade history to a persistent storage like MongoDB database.

importcloud.metaapi.sdk.metaApi.HistoryStorage;
classMongodbHistoryStorageextendsHistoryStorage {
// implement the abstract methods, see MemoryHistoryStorage for sample// implementation
}
HistoryStoragehistoryStorage = newMongodbHistoryStorage();
// Note: if you will not specify history storage, then in-memory storage// will be used (instance of MemoryHistoryStorage)MetaApiConnectionconnection = account.connect(historyStorage).join();
// access history storageHistoryStoragehistoryStorage = connection.getHistoryStorage();
// invoke other methods provided by your history storage implementationSystem.out.println(((MongodbHistoryStorage) historyStorage).yourMethod().join());

Receiving synchronization events

You can override SynchronizationListener in order to receive synchronization event notifications, such as account/position/order/history updates or symbol quote updates.

importcloud.metaapi.sdk.clients.metaApi.SynchronizationListener;
// receive synchronization event notifications// first, implement your listenerclassMySynchronizationListenerextendsSynchronizationListener {
// override abstract methods you want to receive notifications for
}
// now add the listenerSynchronizationListenerlistener = newMySynchronizationListener();
connection.addSynchronizationListener(listener);
// remove the listener when no longer neededconnection.removeSynchronizationListener(listener);

Retrieve contract specifications and quotes via streaming API

MetaApiConnectionconnection = account.connect().join();
connection.waitSynchronized().join();
// first, subscribe to market dataconnection.subscribeToMarketData("GBPUSD").join();
// read constract specificationSystem.out.println(terminalState.getSpecification("EURUSD"));
// read current priceSystem.out.println(terminalState.getPrice("EURUSD"));
// unsubscribe from market data when no longer neededconnection.unsubscribeFromMarketData("GBPUSD").join();

Execute trades (both RPC and streaming APIs)

MetaApiConnectionconnection = account.connect().join();
connection.waitSynchronized().join();
// tradeTradeOptionsoptions = newTradeOptions() {{ comment = "comment"; clientId = "TE_GBPUSD_7hyINWqAl"; }};
System.out.println(connection.createMarketBuyOrder("GBPUSD", 0.07, 0.9, 2.0, options).join());
System.out.println(connection.createMarketSellOrder("GBPUSD", 0.07, 2.0, 0.9, options).join());
System.out.println(connection.createLimitBuyOrder("GBPUSD", 0.07, 1.0, 0.9, 2.0, options).join());
System.out.println(connection.createLimitSellOrder("GBPUSD", 0.07, 1.5, 2.0, 0.9, options).join());
System.out.println(connection.createStopBuyOrder("GBPUSD", 0.07, 1.5, 0.9, 2.0, options).join());
System.out.println(connection.createStopSellOrder("GBPUSD", 0.07, 1.0, 2.0, 0.9, options).join());
System.out.println(connection.createStopLimitBuyOrder("GBPUSD", 0.07, 1.5, 1.4, 0.9, 2.0, options).join());
System.out.println(connection.createStopLimitSellOrder("GBPUSD", 0.07, 1.0, 1.1, 2.0, 0.9, options).join());
System.out.println(connection.modifyPosition("46870472", 2.0, 0.9).join());
System.out.println(connection.closePositionPartially("46870472", 0.9, null).join());
System.out.println(connection.closePosition("46870472", null).join());
System.out.println(connection.closeBy("46870472", "46870482", null).join());
System.out.println(connection.closePositionsBySymbol("EURUSD", null).join());
System.out.println(connection.modifyOrder("46870472", 1.0, 2.0, 0.9).join());
System.out.println(connection.cancelOrder("46870472").join());
// if you need to, check the extra result information in stringCode and numericCode properties of the responseMetatraderTradeResponseresult = connection.createMarketBuyOrder("GBPUSD", 0.07, 0.9, 2.0, options).join();
System.out.println("Trade successful, result code is " + result.stringCode);

Monitoring account connection health and uptime

You can monitor account connection health using MetaApiConnection.healthMonitor API.

ConnectionHealthMonitormonitor = connection.getHealthMonitor();
// retrieve server-side app health statusSystem.out.println(monitor.getServerHealthStatus());
// retrieve detailed connection health statusSystem.out.println(monitor.getHealthStatus());
// retrieve account connection update measured over last 7 daysSystem.out.println(monitor.getUptime());

Tracking latencies

You can track latencies uring MetaApi.latencyMonitor API. Client-side latencies include network communication delays, thus the lowest client-side latencies are achieved if you host your app in AWS Ohio region.

MetaApiapi = newMetaApi("token", newMetaApi.Options() {{
enableLatencyMonitor = true;
}});
LatencyMonitormonitor = api.getLatencyMonitor();
// retrieve trade latecy statsSystem.out.println(monitor.getTradeLatencies());
// retrieve update streaming latency statsSystem.out.println(monitor.getUpdateLatencies());
// retrieve quote streaming latency statsSystem.out.println(monitor.getPriceLatencies());
// retrieve request latency statsSystem.out.println(monitor.getRequestLatencies());

Managing MetaTrader demo accounts via API

Please note that not all MT4/MT5 servers allows you to create demo accounts using the method below.

Create a MetaTrader 4 demo account

MetatraderDemoAccountdemoAccount = api.getMetatraderDemoAccountApi()
.createMT4DemoAccount(provisioningProfile.getId(), newNewMT4DemoAccount() {{
balance = 100000;
email = "example@example.com";
leverage = 100;
serverName = "Example-Server-Demo";
}}).join();

Create a MetaTrader 5 demo account

MetatraderDemoAccountdemoAccount = api.getMetatraderDemoAccountApi()
.createMT5DemoAccount(provisioningProfile.getId(), newNewMT5DemoAccount() {{
balance = 100000;
email = "example@example.com";
leverage = 100;
serverName = "ICMarketsSC-Demo";
}}).join();

Rate limits & quotas

API calls you make are subject to rate limits. See MT account management API and MetaApi API for details.

MetaApi applies quotas to the number of accounts and provisioning profiles, for more details see the MT account management API quotas

CopyFactory copy trading API

CopyFactory is a powerful trade copying API which makes developing forex trade copying applications as easy as writing few lines of code.

You can find CopyFactory Java SDK documentation here: https://github.com/metaapi/metaapi-copyfactory-java-sdk

MetaStats trading statistics API

MetaStats is a powerful trade statistics API which makes it possible to add forex trading metrics into forex applications.

You can find MetaStats Java SDK documentation here: https://github.com/metaapi/metaapi-metastats-java-sdk

About

Java SDK for MetaApi, a professional cloud forex trading API for MetaTrader platform which supports both MetaTrader MetaTrader 5 and MetaTrader 4. Free usage tier available.

Topics

Resources

Stars

21 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors