public class MetaApiWebsocketClient extends Object implements OutOfOrderListener
| Modifier and Type | Class and Description |
|---|---|
static class |
MetaApiWebsocketClient.ClientOptions
Websocket client options
|
static class |
MetaApiWebsocketClient.EventProcessingOptions
Options for processing websocket client events
|
static class |
MetaApiWebsocketClient.PacketLoggerOptions
Packet logger options
|
protected static class |
MetaApiWebsocketClient.SocketInstance |
| Modifier and Type | Field and Description |
|---|---|
protected static int |
resetDisconnectTimerTimeout |
protected List<MetaApiWebsocketClient.SocketInstance> |
socketInstances |
protected Map<String,Integer> |
socketInstancesByAccounts |
| Constructor and Description |
|---|
MetaApiWebsocketClient(cloud.metaapi.sdk.clients.HttpClient httpClient,
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,
String accountId)
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<Void> |
ensureSubscribe(String accountId,
Integer instanceNumber)
Creates a task that ensures the account gets subscribed to the server
|
CompletableFuture<MetatraderAccountInformation> |
getAccountInformation(String accountId)
Returns account information for a specified MetaTrader account (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readAccountInformation/).
|
List<String> |
getAssignedAccounts(int socketInstanceIndex)
Returns list of accounts assigned to instance
|
CompletableFuture<MetatraderBook> |
getBook(String accountId,
String symbol)
Retrieves latest order book for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readBook/).
|
CompletableFuture<MetatraderCandle> |
getCandle(String accountId,
String symbol,
String timeframe)
Retrieves price for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readCandle/).
|
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,
cloud.metaapi.sdk.clients.models.IsoTime startTime,
cloud.metaapi.sdk.clients.models.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,
cloud.metaapi.sdk.clients.models.IsoTime startTime,
cloud.metaapi.sdk.clients.models.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/).
|
String |
getServerUrl()
Loads server url to connect the websocket client to
|
List<MetaApiWebsocketClient.SocketInstance> |
getSocketInstances()
Returns the list of socket instance dictionaries
|
Map<String,Integer> |
getSocketInstancesByAccounts()
Returns the map of socket instances by account ids
|
List<String> |
getSubscribedAccountIds()
Returns the list of subscribed account ids
|
List<String> |
getSubscribedAccountIds(Integer socketInstanceIndex)
Returns the list of subscribed account ids
|
CompletableFuture<MetatraderSymbolPrice> |
getSymbolPrice(String accountId,
String symbol)
Retrieves price for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readSymbolPrice/).
|
CompletableFuture<List<String>> |
getSymbols(String accountId)
Retrieves symbols available on an account (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readSymbols/).
|
CompletableFuture<MetatraderSymbolSpecification> |
getSymbolSpecification(String accountId,
String symbol)
Retrieves specification for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readSymbolSpecification/).
|
CompletableFuture<MetatraderTick> |
getTick(String accountId,
String symbol)
Retrieves latest tick for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/readTick/).
|
boolean |
isConnected(Integer socketInstanceIndex)
Returns websocket client connection status
|
CompletableFuture<Void> |
lockSocketInstance(int socketInstanceIndex,
cloud.metaapi.sdk.clients.error_handler.TooManyRequestsException.TooManyRequestsExceptionMetadata metadata)
Locks subscription for a socket instance based on TooManyRequestsError metadata
|
void |
onOutOfOrderPacket(String accountId,
int instanceIndex,
long expectedSequenceNumber,
long actualSequenceNumber,
com.fasterxml.jackson.databind.JsonNode packet,
cloud.metaapi.sdk.clients.models.IsoTime receivedAt)
Restarts the account synchronization process on an out of order packet
|
void |
queueEvent(String accountId,
java.util.function.Supplier<CompletableFuture<Void>> event)
Queues account event for processing
|
void |
queuePacket(com.fasterxml.jackson.databind.JsonNode packet)
Queues an account packet for processing
|
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 instanceNumber)
Subscribes to the Metatrader terminal events (see https://metaapi.cloud/docs/client/websocket/api/subscribe/).
|
CompletableFuture<Void> |
subscribeToMarketData(String accountId,
Integer instanceNumber,
String symbol,
List<MarketDataSubscription> subscriptions)
Subscribes on market data of specified symbol (see
https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).
|
CompletableFuture<Boolean> |
synchronize(String accountId,
Integer instanceIndex,
String host,
String synchronizationId,
cloud.metaapi.sdk.clients.models.IsoTime startingHistoryOrderTime,
cloud.metaapi.sdk.clients.models.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 instanceNumber,
String symbol,
List<MarketDataUnsubscription> subscriptions)
Unsubscribes from market data of specified symbol (see
https://metaapi.cloud/docs/client/websocket/marketDataStreaming/unsubscribeFromMarketData/).
|
CompletableFuture<Void> |
waitSynchronized(String accountId,
Integer instanceNumber,
String applicationPattern,
Long timeoutInSeconds)
Waits for server-side terminal state synchronization to complete.
|
protected static int resetDisconnectTimerTimeout
protected List<MetaApiWebsocketClient.SocketInstance> socketInstances
public MetaApiWebsocketClient(cloud.metaapi.sdk.clients.HttpClient httpClient,
String token,
MetaApiWebsocketClient.ClientOptions opts)
throws IOException,
cloud.metaapi.sdk.clients.error_handler.ValidationException
httpClient - HTTP clienttoken - authorization tokenopts - websocket client optionsIOException - if packet logger is enabled and failed to create the log directorycloud.metaapi.sdk.clients.error_handler.ValidationException - if options are invalidpublic void onOutOfOrderPacket(String accountId, int instanceIndex, long expectedSequenceNumber, long actualSequenceNumber, com.fasterxml.jackson.databind.JsonNode packet, cloud.metaapi.sdk.clients.models.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 List<MetaApiWebsocketClient.SocketInstance> getSocketInstances()
public Map<String,Integer> getSocketInstancesByAccounts()
public List<String> getSubscribedAccountIds()
public List<String> getSubscribedAccountIds(Integer socketInstanceIndex)
socketInstanceIndex - socket instance indexpublic boolean isConnected(Integer socketInstanceIndex)
socketInstanceIndex - socket instance indexpublic List<String> getAssignedAccounts(int socketInstanceIndex)
socketInstanceIndex - socket instance indexpublic CompletableFuture<Void> lockSocketInstance(int socketInstanceIndex, cloud.metaapi.sdk.clients.error_handler.TooManyRequestsException.TooManyRequestsExceptionMetadata metadata)
socketInstanceIndex - socket instance indexmetadata - TooManyRequestsException metadatapublic 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, cloud.metaapi.sdk.clients.models.IsoTime startTime, cloud.metaapi.sdk.clients.models.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, cloud.metaapi.sdk.clients.models.IsoTime startTime, cloud.metaapi.sdk.clients.models.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> ensureSubscribe(String accountId, Integer instanceNumber)
accountId - account id to subscribeinstanceNumber - instance index number, or nullpublic CompletableFuture<Void> subscribe(String accountId)
accountId - id of the MetaTrader account to subscribe topublic CompletableFuture<Void> subscribe(String accountId, Integer instanceNumber)
accountId - id of the MetaTrader account to subscribe toinstanceNumber - instance index number, or nullpublic CompletableFuture<Void> reconnect(String accountId)
accountId - id of the MetaTrader account to reconnectpublic CompletableFuture<Boolean> synchronize(String accountId, Integer instanceIndex, String host, String synchronizationId, cloud.metaapi.sdk.clients.models.IsoTime startingHistoryOrderTime, cloud.metaapi.sdk.clients.models.IsoTime startingDealTime)
accountId - id of the MetaTrader account to synchronizeinstanceIndex - instance indexhost - name of host to synchronize withsynchronizationId - 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, Integer instanceNumber, String applicationPattern, Long timeoutInSeconds)
accountId - id of the MetaTrader account to synchronizeinstanceNumber - instance index numberapplicationPattern - MetaApi application regular expression pattern, or null, default is .*timeoutInSeconds - timeout in seconds, or null, default is 300 secondspublic CompletableFuture<Void> subscribeToMarketData(String accountId, Integer instanceNumber, String symbol, List<MarketDataSubscription> subscriptions)
accountId - id of the MetaTrader accountinstanceNumber - instance index numbersymbol - symbol (e.g. currency pair or an index)subscriptions - array of market data subscription to create or updatepublic CompletableFuture<Void> unsubscribeFromMarketData(String accountId, int instanceNumber, String symbol, List<MarketDataUnsubscription> subscriptions)
accountId - id of the MetaTrader accountinstanceNumber - instance index numbersymbol - symbol (e.g. currency pair or an index)subscriptions - array of subscriptions to cancelpublic CompletableFuture<List<String>> getSymbols(String accountId)
accountId - id of the MetaTrader account to retrieve symbols forpublic 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<MetatraderCandle> getCandle(String accountId, String symbol, String timeframe)
accountId - id of the MetaTrader account to retrieve candle forsymbol - symbol to retrieve candle fortimeframe - defines the timeframe according to which the candle must be generated. Allowed values for
MT5 are 1m, 2m, 3m, 4m, 5m, 6m, 10m, 12m, 15m, 20m, 30m, 1h, 2h, 3h, 4h, 6h, 8h, 12h, 1d, 1w, 1mn. Allowed values
for MT4 are 1m, 5m, 15m 30m, 1h, 4h, 1d, 1w, 1mnpublic CompletableFuture<MetatraderTick> getTick(String accountId, String symbol)
accountId - id of the MetaTrader account to retrieve symbol tick forsymbol - symbol to retrieve tick forpublic CompletableFuture<MetatraderBook> getBook(String accountId, String symbol)
accountId - id of the MetaTrader account to retrieve symbol order book forsymbol - symbol to retrieve order book 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, String accountId)
listener - reconnect listener to addaccountId - account id of listenerpublic void removeReconnectListener(ReconnectListener listener)
listener - listener to removepublic void removeAllListeners()
public void queuePacket(com.fasterxml.jackson.databind.JsonNode packet)
packet - packet to processpublic void queueEvent(String accountId, java.util.function.Supplier<CompletableFuture<Void>> event)
accountId - account idevent - event to executeprotected CompletableFuture<com.fasterxml.jackson.databind.JsonNode> rpcRequest(String accountId, com.fasterxml.jackson.databind.node.ObjectNode request, Long timeoutInSeconds)
Copyright © 2022. All rights reserved.