Class MetaApiWebsocketClient
- java.lang.Object
-
- cloud.metaapi.sdk.clients.meta_api.MetaApiWebsocketClient
-
public class MetaApiWebsocketClient extends Object
MetaApi websocket API client (see https://metaapi.cloud/docs/client/websocket/overview/)
-
-
Constructor Summary
Constructors Constructor Description MetaApiWebsocketClient(String token)Constructs MetaApi websocket API client instance.MetaApiWebsocketClient(String token, String domain, long requestTimeout, long connectTimeout)Constructs MetaApi websocket API client instance
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddReconnectListener(ReconnectListener listener)Adds reconnect listenervoidaddSynchronizationListener(String accountId, SynchronizationListener listener)Adds synchronization listener for specific accountvoidclose()Closes connection to MetaApi serverCompletableFuture<Void>connect()Connects to MetaApi server via socket.io protocolCompletableFuture<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/).CompletableFuture<Void>reconnect(String accountId)Reconnects to the Metatrader terminal (see https://metaapi.cloud/docs/client/websocket/api/reconnect/).voidremoveAllListeners()Removes all listeners.CompletableFuture<Void>removeHistory(String accountId)Clears the order and transaction history of a specified account so that it can be synchronized from scratch (see https://metaapi.cloud/docs/client/websocket/api/removeHistory/).voidremoveReconnectListener(ReconnectListener listener)Removes reconnect listenervoidremoveSynchronizationListener(String accountId, SynchronizationListener listener)Removes synchronization listener for specific accountvoidsetUrl(String url)Patch server URL for use in unit testsCompletableFuture<Void>subscribe(String accountId)Subscribes to the Metatrader terminal events (see https://metaapi.cloud/docs/client/websocket/api/subscribe/).CompletableFuture<Void>subscribeToMarketData(String accountId, String symbol)Subscribes on market data of specified symbol (see https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).CompletableFuture<Void>synchronize(String accountId, 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/).
-
-
-
Constructor Detail
-
MetaApiWebsocketClient
public MetaApiWebsocketClient(String token)
Constructs MetaApi websocket API client instance. Domain isagiliumtrade.agiliumtrade.ai, timeout for socket requests is1 minute, timeout for connecting to server is1 minute.- Parameters:
token- authorization token
-
MetaApiWebsocketClient
public MetaApiWebsocketClient(String token, String domain, long requestTimeout, long connectTimeout)
Constructs MetaApi websocket API client instance- Parameters:
token- authorization tokendomain- domain to connect torequestTimeout- timeout for socket requests in millisecondsconnectTimeout- timeout for connecting to server in milliseconds
-
-
Method Detail
-
setUrl
public void setUrl(String url)
Patch server URL for use in unit tests- Parameters:
url- patched server URL
-
connect
public CompletableFuture<Void> connect()
Connects to MetaApi server via socket.io protocol
-
close
public void close()
Closes connection to MetaApi server
-
getAccountInformation
public CompletableFuture<MetatraderAccountInformation> getAccountInformation(String accountId)
Returns account information for a specified MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readAccountInformation/).- Parameters:
accountId- id of the MetaTrader account to return information for
-
getPositions
public CompletableFuture<List<MetatraderPosition>> getPositions(String accountId)
Returns positions for a specified MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPositions/).- Parameters:
accountId- id of the MetaTrader account to return information for
-
getPosition
public CompletableFuture<MetatraderPosition> getPosition(String accountId, String positionId)
Returns specific position for a MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPosition/).- Parameters:
accountId- id of the MetaTrader account to return information forpositionId- position id- Returns:
- completable future resolving with MetaTrader position found
-
getOrders
public CompletableFuture<List<MetatraderOrder>> getOrders(String accountId)
Returns open orders for a specified MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readOrders/).- Parameters:
accountId- id of the MetaTrader account to return information for- Returns:
- completable future resolving with open MetaTrader orders
-
getOrder
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information fororderId- order id (ticket number)- Returns:
- completable future resolving with metatrader order found
-
getHistoryOrdersByTicket
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information forticket- ticket number (order id)
-
getHistoryOrdersByPosition
public 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/)- Parameters:
accountId- id of the MetaTrader account to return information forpositionId- position id
-
getHistoryOrdersByTimeRange
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information forstartTime- start of time range, inclusiveendTime- end of time range, exclusiveoffset- pagination offsetlimit- pagination limit
-
getDealsByTicket
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information forticket- ticket number (deal id for MT5 or order id for MT4)
-
getDealsByPosition
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information forpositionId- position id
-
getDealsByTimeRange
public 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/).- Parameters:
accountId- id of the MetaTrader account to return information forstartTime- start of time range, inclusiveendTime- end of time range, exclusiveoffset- pagination offsetlimit- pagination limit
-
removeHistory
public CompletableFuture<Void> removeHistory(String accountId)
Clears the order and transaction history of a specified account so that it can be synchronized from scratch (see https://metaapi.cloud/docs/client/websocket/api/removeHistory/).- Parameters:
accountId- id of the MetaTrader account to remove history for- Returns:
- completable future resolving when the history is cleared
-
trade
public CompletableFuture<MetatraderTradeResponse> trade(String accountId, MetatraderTrade trade)
Execute a trade on a connected MetaTrader account (see https://metaapi.cloud/docs/client/websocket/api/trade/).- Parameters:
accountId- id of the MetaTrader account to execute trade fortrade- trade to execute (see docs for possible trade types)
-
subscribe
public CompletableFuture<Void> subscribe(String accountId)
Subscribes to the Metatrader terminal events (see https://metaapi.cloud/docs/client/websocket/api/subscribe/).- Parameters:
accountId- id of the MetaTrader account to subscribe to
-
reconnect
public CompletableFuture<Void> reconnect(String accountId)
Reconnects to the Metatrader terminal (see https://metaapi.cloud/docs/client/websocket/api/reconnect/).- Parameters:
accountId- id of the MetaTrader account to reconnect
-
synchronize
public CompletableFuture<Void> synchronize(String accountId, String synchronizationId, IsoTime startingHistoryOrderTime, IsoTime startingDealTime)
Requests the terminal to start synchronization process (see https://metaapi.cloud/docs/client/websocket/synchronizing/synchronize/).- Parameters:
accountId- id of the MetaTrader account to synchronizesynchronizationId- 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.
-
subscribeToMarketData
public CompletableFuture<Void> subscribeToMarketData(String accountId, String symbol)
Subscribes on market data of specified symbol (see https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).- Parameters:
accountId- id of the MetaTrader accountsymbol- symbol (e.g. currency pair or an index)
-
getSymbolSpecification
public CompletableFuture<MetatraderSymbolSpecification> getSymbolSpecification(String accountId, String symbol)
Retrieves specification for a symbol (see https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolSpecification/).- Parameters:
accountId- id of the MetaTrader account to retrieve symbol specification forsymbol- symbol to retrieve specification for
-
getSymbolPrice
public CompletableFuture<MetatraderSymbolPrice> getSymbolPrice(String accountId, String symbol)
Retrieves price for a symbol (see https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolPrice/).- Parameters:
accountId- id of the MetaTrader account to retrieve symbol price forsymbol- symbol to retrieve price for
-
addSynchronizationListener
public void addSynchronizationListener(String accountId, SynchronizationListener listener)
Adds synchronization listener for specific account- Parameters:
accountId- account idlistener- synchronization listener to add
-
removeSynchronizationListener
public void removeSynchronizationListener(String accountId, SynchronizationListener listener)
Removes synchronization listener for specific account- Parameters:
accountId- account idlistener- synchronization listener to remove
-
addReconnectListener
public void addReconnectListener(ReconnectListener listener)
Adds reconnect listener- Parameters:
listener- reconnect listener to add
-
removeReconnectListener
public void removeReconnectListener(ReconnectListener listener)
Removes reconnect listener- Parameters:
listener- listener to remove
-
removeAllListeners
public void removeAllListeners()
Removes all listeners. Intended for use in unit tests.
-
-