public class MetaApiWebsocketClient extends Object implements OutOfOrderListener
| Modifier and Type | Class and Description |
|---|---|
static class |
MetaApiWebsocketClient.ClientOptions
Websocket client options
|
static class |
MetaApiWebsocketClient.PacketLoggerOptions
Packet logger options
|
| Constructor and Description |
|---|
MetaApiWebsocketClient(String token)
Constructs MetaApi websocket API client instance with default parameters
|
MetaApiWebsocketClient(String token,
MetaApiWebsocketClient.ClientOptions opts)
Constructs MetaApi websocket API client instance
|
| Modifier and Type | Method and Description |
|---|---|
void |
addLatencyListener(LatencyListener listener)
Adds latency listener
|
void |
addReconnectListener(ReconnectListener listener)
Adds reconnect listener
|
void |
addSynchronizationListener(String accountId,
SynchronizationListener listener)
Adds synchronization listener for specific account
|
void |
close()
Closes connection to MetaApi server
|
CompletableFuture<Void> |
connect()
Connects to MetaApi server via socket.io protocol
|
CompletableFuture<MetatraderAccountInformation> |
getAccountInformation(String accountId)
Returns account information for a specified MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readAccountInformation/).
|
CompletableFuture<MetatraderDeals> |
getDealsByPosition(String accountId,
String positionId)
Returns history deals for a specific position id (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readDealsByPosition/).
|
CompletableFuture<MetatraderDeals> |
getDealsByTicket(String accountId,
String ticket)
Returns history deals with a specific ticket number (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readDealsByTicket/).
|
CompletableFuture<MetatraderDeals> |
getDealsByTimeRange(String accountId,
IsoTime startTime,
IsoTime endTime,
int offset,
int limit)
Returns history deals with for a specific time range (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readDealsByTimeRange/).
|
CompletableFuture<MetatraderHistoryOrders> |
getHistoryOrdersByPosition(String accountId,
String positionId)
Returns the history of completed orders for a specific position id (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readHistoryOrdersByPosition/)
|
CompletableFuture<MetatraderHistoryOrders> |
getHistoryOrdersByTicket(String accountId,
String ticket)
Returns the history of completed orders for a specific ticket number (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readHistoryOrdersByTicket/).
|
CompletableFuture<MetatraderHistoryOrders> |
getHistoryOrdersByTimeRange(String accountId,
IsoTime startTime,
IsoTime endTime,
int offset,
int limit)
Returns the history of completed orders for a specific time range (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readHistoryOrdersByTimeRange/).
|
CompletableFuture<MetatraderOrder> |
getOrder(String accountId,
String orderId)
Returns specific open order for a MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readOrder/).
|
CompletableFuture<List<MetatraderOrder>> |
getOrders(String accountId)
Returns open orders for a specified MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readOrders/).
|
CompletableFuture<MetatraderPosition> |
getPosition(String accountId,
String positionId)
Returns specific position for a MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPosition/).
|
CompletableFuture<List<MetatraderPosition>> |
getPositions(String accountId)
Returns positions for a specified MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPositions/).
|
CompletableFuture<MetatraderSymbolPrice> |
getSymbolPrice(String accountId,
String symbol)
Retrieves price for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolPrice/).
|
CompletableFuture<MetatraderSymbolSpecification> |
getSymbolSpecification(String accountId,
String symbol)
Retrieves specification for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolSpecification/).
|
void |
onOutOfOrderPacket(String accountId,
int instanceIndex,
long expectedSequenceNumber,
long actualSequenceNumber,
com.fasterxml.jackson.databind.JsonNode packet,
IsoTime receivedAt)
Restarts the account synchronization process on an out of order packet
|
CompletableFuture<Void> |
reconnect(String accountId)
Reconnects to the Metatrader terminal (see https://metaapi.cloud/docs/client/websocket/api/reconnect/).
|
void |
removeAllListeners()
Removes all listeners.
|
CompletableFuture<Void> |
removeApplication(String accountId)
Clears the order and transaction history of a specified application and removes the application (see
https://metaapi.cloud/docs/client/websocket/api/removeApplication/).
|
CompletableFuture<Void> |
removeHistory(String accountId)
Clears the order and transaction history of a specified application so that it can be synchronized from scratch
(see https://metaapi.cloud/docs/client/websocket/api/removeHistory/).
|
CompletableFuture<Void> |
removeHistory(String accountId,
String application)
Clears the order and transaction history of a specified application so that it can be synchronized from scratch
(see https://metaapi.cloud/docs/client/websocket/api/removeHistory/).
|
void |
removeLatencyListener(LatencyListener listener)
Removes latency listener
|
void |
removeReconnectListener(ReconnectListener listener)
Removes reconnect listener
|
void |
removeSynchronizationListener(String accountId,
SynchronizationListener listener)
Removes synchronization listener for specific account
|
protected CompletableFuture<com.fasterxml.jackson.databind.JsonNode> |
rpcRequest(String accountId,
com.fasterxml.jackson.databind.node.ObjectNode request,
Long timeoutInSeconds) |
CompletableFuture<Void> |
saveUptime(String accountId,
Map<String,Double> uptime)
Sends client uptime stats to the server.
|
void |
setUrl(String url)
Patch server URL for use in unit tests
|
CompletableFuture<Void> |
subscribe(String accountId)
Subscribes to the Metatrader terminal events (see https://metaapi.cloud/docs/client/websocket/api/subscribe/).
|
CompletableFuture<Void> |
subscribe(String accountId,
Integer instanceIndex)
Subscribes to the Metatrader terminal events (see https://metaapi.cloud/docs/client/websocket/api/subscribe/).
|
CompletableFuture<Void> |
subscribeToMarketData(String accountId,
int instanceIndex,
String symbol)
Subscribes on market data of specified symbol (see
https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).
|
CompletableFuture<Void> |
synchronize(String accountId,
int instanceIndex,
String synchronizationId,
IsoTime startingHistoryOrderTime,
IsoTime startingDealTime)
Requests the terminal to start synchronization process
(see https://metaapi.cloud/docs/client/websocket/synchronizing/synchronize/).
|
CompletableFuture<MetatraderTradeResponse> |
trade(String accountId,
MetatraderTrade trade)
Execute a trade on a connected MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<com.fasterxml.jackson.databind.JsonNode> |
unsubscribe(String accountId)
Unsubscribe from account (see
https://metaapi.cloud/docs/client/websocket/api/synchronizing/unsubscribe).
|
CompletableFuture<Void> |
unsubscribeFromMarketData(String accountId,
int instanceIndex,
String symbol)
Unsubscribes from market data of specified symbol (see
https://metaapi.cloud/docs/client/websocket/marketDataStreaming/unsubscribeFromMarketData/).
|
CompletableFuture<Void> |
waitSynchronized(String accountId,
int instanceIndex,
String applicationPattern,
Long timeoutInSeconds)
Waits for server-side terminal state synchronization to complete.
|
public MetaApiWebsocketClient(String token) throws IOException
token - authorization tokenIOException - if packet logger is enabled and failed to create the log directorypublic MetaApiWebsocketClient(String token, MetaApiWebsocketClient.ClientOptions opts) throws IOException
token - authorization tokenopts - websocket client optionsIOException - if packet logger is enabled and failed to create the log directorypublic void onOutOfOrderPacket(String accountId, int instanceIndex, long expectedSequenceNumber, long actualSequenceNumber, com.fasterxml.jackson.databind.JsonNode packet, IsoTime receivedAt)
onOutOfOrderPacket in interface OutOfOrderListeneraccountId - account idinstanceIndex - instance indexexpectedSequenceNumber - expected s/nactualSequenceNumber - actual s/npacket - packet datareceivedAt - time the packet was received atpublic void setUrl(String url)
url - patched server URLpublic CompletableFuture<Void> connect()
public void close()
public CompletableFuture<MetatraderAccountInformation> getAccountInformation(String accountId)
accountId - id of the MetaTrader account to return information forpublic CompletableFuture<List<MetatraderPosition>> getPositions(String accountId)
accountId - id of the MetaTrader account to return information forpublic CompletableFuture<MetatraderPosition> getPosition(String accountId, String positionId)
accountId - id of the MetaTrader account to return information forpositionId - position idpublic CompletableFuture<List<MetatraderOrder>> getOrders(String accountId)
accountId - id of the MetaTrader account to return information forpublic CompletableFuture<MetatraderOrder> getOrder(String accountId, String orderId)
accountId - id of the MetaTrader account to return information fororderId - order id (ticket number)public CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByTicket(String accountId, String ticket)
accountId - id of the MetaTrader account to return information forticket - ticket number (order id)public CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByPosition(String accountId, String positionId)
accountId - id of the MetaTrader account to return information forpositionId - position idpublic CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByTimeRange(String accountId, IsoTime startTime, IsoTime endTime, int offset, int limit)
accountId - id of the MetaTrader account to return information forstartTime - start of time range, inclusiveendTime - end of time range, exclusiveoffset - pagination offsetlimit - pagination limitpublic CompletableFuture<MetatraderDeals> getDealsByTicket(String accountId, String ticket)
accountId - id of the MetaTrader account to return information forticket - ticket number (deal id for MT5 or order id for MT4)public CompletableFuture<MetatraderDeals> getDealsByPosition(String accountId, String positionId)
accountId - id of the MetaTrader account to return information forpositionId - position idpublic CompletableFuture<MetatraderDeals> getDealsByTimeRange(String accountId, IsoTime startTime, IsoTime endTime, int offset, int limit)
accountId - id of the MetaTrader account to return information forstartTime - start of time range, inclusiveendTime - end of time range, exclusiveoffset - pagination offsetlimit - pagination limitpublic CompletableFuture<Void> removeHistory(String accountId)
accountId - id of the MetaTrader account to remove history forpublic CompletableFuture<Void> removeHistory(String accountId, String application)
accountId - id of the MetaTrader account to remove history forapplication - application to remove history for, or nullpublic CompletableFuture<Void> removeApplication(String accountId)
accountId - id of the MetaTrader account to remove history and application forpublic CompletableFuture<MetatraderTradeResponse> trade(String accountId, MetatraderTrade trade)
accountId - id of the MetaTrader account to execute trade fortrade - trade to execute (see docs for possible trade types)TradeException, check error properties for error code detailspublic CompletableFuture<Void> subscribe(String accountId)
accountId - id of the MetaTrader account to subscribe topublic CompletableFuture<Void> subscribe(String accountId, Integer instanceIndex)
accountId - id of the MetaTrader account to subscribe toinstanceIndex - instance index, or nullpublic CompletableFuture<Void> reconnect(String accountId)
accountId - id of the MetaTrader account to reconnectpublic CompletableFuture<Void> synchronize(String accountId, int instanceIndex, String synchronizationId, IsoTime startingHistoryOrderTime, IsoTime startingDealTime)
accountId - id of the MetaTrader account to synchronizeinstanceIndex - instance indexsynchronizationId - synchronization request idstartingHistoryOrderTime - from what date to start synchronizing history orders from. If not specified,
the entire order history will be downloaded.startingDealTime - from what date to start deal synchronization from. If not specified, then all
history deals will be downloaded.public CompletableFuture<Void> waitSynchronized(String accountId, int instanceIndex, String applicationPattern, Long timeoutInSeconds)
accountId - id of the MetaTrader account to synchronizeinstanceIndex - instance indexapplicationPattern - MetaApi application regular expression pattern, or null, default is .*timeoutInSeconds - timeout in seconds, or null, default is 300 secondspublic CompletableFuture<Void> subscribeToMarketData(String accountId, int instanceIndex, String symbol)
accountId - id of the MetaTrader accountinstanceIndex - instance indexsymbol - symbol (e.g. currency pair or an index)public CompletableFuture<Void> unsubscribeFromMarketData(String accountId, int instanceIndex, String symbol)
accountId - id of the MetaTrader accountinstanceIndex - instance indexsymbol - symbol (e.g. currency pair or an index)public CompletableFuture<MetatraderSymbolSpecification> getSymbolSpecification(String accountId, String symbol)
accountId - id of the MetaTrader account to retrieve symbol specification forsymbol - symbol to retrieve specification forpublic CompletableFuture<MetatraderSymbolPrice> getSymbolPrice(String accountId, String symbol)
accountId - id of the MetaTrader account to retrieve symbol price forsymbol - symbol to retrieve price forpublic CompletableFuture<Void> saveUptime(String accountId, Map<String,Double> uptime)
accountId - id of the MetaTrader account to retrieve symbol price foruptime - uptime statistics to send to the serverpublic CompletableFuture<com.fasterxml.jackson.databind.JsonNode> unsubscribe(String accountId)
accountId - id of the MetaTrader account to retrieve symbol price forpublic void addSynchronizationListener(String accountId, SynchronizationListener listener)
accountId - account idlistener - synchronization listener to addpublic void removeSynchronizationListener(String accountId, SynchronizationListener listener)
accountId - account idlistener - synchronization listener to removepublic void addLatencyListener(LatencyListener listener)
listener - latency listener to addpublic void removeLatencyListener(LatencyListener listener)
listener - latency listener to removepublic void addReconnectListener(ReconnectListener listener)
listener - reconnect listener to addpublic void removeReconnectListener(ReconnectListener listener)
listener - listener to removepublic void removeAllListeners()
protected CompletableFuture<com.fasterxml.jackson.databind.JsonNode> rpcRequest(String accountId, com.fasterxml.jackson.databind.node.ObjectNode request, Long timeoutInSeconds)
Copyright © 2021. All rights reserved.