Class MetaApiWebsocketClient


  • public class MetaApiWebsocketClient
    extends Object
    MetaApi websocket API client (see https://metaapi.cloud/docs/client/websocket/overview/)
    • Constructor Detail

      • MetaApiWebsocketClient

        public MetaApiWebsocketClient​(String token)
        Constructs MetaApi websocket API client instance. Domain is agiliumtrade.agiliumtrade.ai, timeout for socket requests is 1 minute, timeout for connecting to server is 1 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 token
        domain - domain to connect to
        requestTimeout - timeout for socket requests in milliseconds
        connectTimeout - 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
      • 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 for
        positionId - 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 for
        orderId - 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 for
        ticket - 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 for
        positionId - 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 for
        startTime - start of time range, inclusive
        endTime - end of time range, exclusive
        offset - pagination offset
        limit - 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 for
        ticket - 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 for
        positionId - 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 for
        startTime - start of time range, inclusive
        endTime - end of time range, exclusive
        offset - pagination offset
        limit - 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 for
        trade - 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 synchronize
        synchronizationId - synchronization request id
        startingHistoryOrderTime - 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 account
        symbol - 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 for
        symbol - 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 for
        symbol - symbol to retrieve price for
      • addSynchronizationListener

        public void addSynchronizationListener​(String accountId,
                                               SynchronizationListener listener)
        Adds synchronization listener for specific account
        Parameters:
        accountId - account id
        listener - synchronization listener to add
      • removeSynchronizationListener

        public void removeSynchronizationListener​(String accountId,
                                                  SynchronizationListener listener)
        Removes synchronization listener for specific account
        Parameters:
        accountId - account id
        listener - 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.