public class MetaApiConnection extends SynchronizationListener implements ReconnectListener
| Constructor and Description |
|---|
MetaApiConnection(MetaApiWebsocketClient websocketClient,
MetatraderAccount account,
HistoryStorage historyStorage,
ConnectionRegistry connectionRegistry)
Constructs MetaApi MetaTrader Api connection
|
| Modifier and Type | Method and Description |
|---|---|
void |
addSynchronizationListener(SynchronizationListener listener)
Adds synchronization listener
|
CompletableFuture<MetatraderTradeResponse> |
cancelOrder(String orderId)
Cancels order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
void |
close()
Closes the connection.
|
CompletableFuture<MetatraderTradeResponse> |
closePosition(String positionId,
MarketTradeOptions options)
Fully closes a position (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
closePositionPartially(String positionId,
double volume,
MarketTradeOptions options)
Partially closes a position (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
closePositionsBySymbol(String symbol,
MarketTradeOptions options)
Closes position by a symbol (see https://metaapi.cloud/docs/client/websocket/api/trade/)
|
CompletableFuture<MetatraderTradeResponse> |
createLimitBuyOrder(String symbol,
double volume,
double openPrice,
Double stopLoss,
Double takeProfit,
PendingTradeOptions options)
Creates a limit buy order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
createLimitSellOrder(String symbol,
double volume,
double openPrice,
Double stopLoss,
Double takeProfit,
PendingTradeOptions options)
Creates a limit sell order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
createMarketBuyOrder(String symbol,
double volume,
Double stopLoss,
Double takeProfit,
MarketTradeOptions options)
Creates a market buy order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
createMarketSellOrder(String symbol,
double volume,
Double stopLoss,
Double takeProfit,
MarketTradeOptions options)
Creates a market sell order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
createStopBuyOrder(String symbol,
double volume,
double openPrice,
Double stopLoss,
Double takeProfit,
PendingTradeOptions options)
Creates a stop buy order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
createStopSellOrder(String symbol,
double volume,
double openPrice,
Double stopLoss,
Double takeProfit,
PendingTradeOptions options)
Creates a stop sell order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderAccountInformation> |
getAccountInformation()
Returns account information (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readAccountInformation/).
|
CompletableFuture<MetatraderDeals> |
getDealsByPosition(String positionId)
Returns history deals for a specific position id (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readDealsByPosition/).
|
CompletableFuture<MetatraderDeals> |
getDealsByTicket(String ticket)
Returns history deals with a specific ticket number (see
https://metaapi.cloud/docs/client/websocket/api/retrieveHistoricalData/readDealsByTicket/).
|
CompletableFuture<MetatraderDeals> |
getDealsByTimeRange(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 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 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(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/)
|
HistoryStorage |
getHistoryStorage()
Returns local history storage
|
CompletableFuture<MetatraderOrder> |
getOrder(String orderId)
Returns specific open order (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readOrder/).
|
CompletableFuture<List<MetatraderOrder>> |
getOrders()
Returns open orders (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readOrders/).
|
CompletableFuture<MetatraderPosition> |
getPosition(String positionId)
Returns specific position (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPosition/).
|
CompletableFuture<List<MetatraderPosition>> |
getPositions()
Returns positions (see
https://metaapi.cloud/docs/client/websocket/api/readTradingTerminalState/readPositions/).
|
CompletableFuture<MetatraderSymbolPrice> |
getSymbolPrice(String symbol)
Retrieves specification for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolPrice/).
|
CompletableFuture<MetatraderSymbolSpecification> |
getSymbolSpecification(String symbol)
Retrieves specification for a symbol (see
https://metaapi.cloud/docs/client/websocket/api/retrieveMarketData/getSymbolSpecification/).
|
TerminalState |
getTerminalState()
Returns local copy of terminal state
|
CompletableFuture<Void> |
initialize()
Initializes meta api connection
|
CompletableFuture<Boolean> |
isSynchronized(String synchronizationId)
Returns flag indicating status of state synchronization with MetaTrader terminal
|
CompletableFuture<MetatraderTradeResponse> |
modifyOrder(String orderId,
double openPrice,
double stopLoss,
double takeProfit)
Modifies a pending order (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<MetatraderTradeResponse> |
modifyPosition(String positionId,
Double stopLoss,
Double takeProfit)
Modifies a position (see https://metaapi.cloud/docs/client/websocket/api/trade/).
|
CompletableFuture<Void> |
onConnected()
Invoked when connection to MetaTrader terminal established
|
CompletableFuture<Void> |
onDealSynchronizationFinished(String synchronizationId)
Invoked when a synchronization of history deals on a MetaTrader account have finished
|
CompletableFuture<Void> |
onDisconnected()
Invoked when connection to MetaTrader terminal terminated
|
CompletableFuture<Void> |
onOrderSynchronizationFinished(String synchronizationId)
Invoked when a synchronization of history orders on a MetaTrader account have finished
|
CompletableFuture<Void> |
onReconnected()
Invoked when connection to MetaTrader terminal re-established
|
CompletableFuture<Void> |
reconnect()
Reconnects to the Metatrader terminal (see https://metaapi.cloud/docs/client/websocket/api/reconnect/).
|
CompletableFuture<Void> |
removeHistory()
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/).
|
void |
removeSynchronizationListener(SynchronizationListener listener)
Removes synchronization listener for specific account
|
CompletableFuture<Void> |
subscribe()
Initiates subscription to MetaTrader terminal
|
CompletableFuture<Void> |
subscribeToMarketData(String symbol)
Subscribes on market data of specified symbol (see
https://metaapi.cloud/docs/client/websocket/marketDataStreaming/subscribeToMarketData/).
|
CompletableFuture<Void> |
synchronize()
Requests the terminal to start synchronization process
(see https://metaapi.cloud/docs/client/websocket/synchronizing/synchronize/)
|
CompletableFuture<Void> |
waitSynchronized()
Waits until synchronization to MetaTrader terminal is completed.
|
CompletableFuture<Void> |
waitSynchronized(String synchronizationId)
Waits until synchronization to MetaTrader terminal is completed.
|
CompletableFuture<Void> |
waitSynchronized(String synchronizationId,
Integer timeoutInSeconds,
Integer intervalInMilliseconds)
Waits until synchronization to MetaTrader terminal is completed.
|
onAccountInformationUpdated, onBrokerConnectionStatusChanged, onDealAdded, onHistoryOrderAdded, onOrderCompleted, onOrderUpdated, onPositionRemoved, onPositionUpdated, onSymbolPriceUpdated, onSymbolSpecificationUpdatedpublic MetaApiConnection(MetaApiWebsocketClient websocketClient, MetatraderAccount account, HistoryStorage historyStorage, ConnectionRegistry connectionRegistry)
websocketClient - MetaApi websocket clientaccount - MetaTrader account to connect tohistoryStorage - terminal history storage or null. By default an instance of MemoryHistoryStorage
will be used.connectionRegistry - metatrader account connection registrypublic CompletableFuture<MetatraderAccountInformation> getAccountInformation()
public CompletableFuture<List<MetatraderPosition>> getPositions()
public CompletableFuture<MetatraderPosition> getPosition(String positionId)
positionId - position idpublic CompletableFuture<List<MetatraderOrder>> getOrders()
public CompletableFuture<MetatraderOrder> getOrder(String orderId)
orderId - order id (ticket number)public CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByTicket(String ticket)
ticket - ticket number (order id)public CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByPosition(String positionId)
positionId - position idpublic CompletableFuture<MetatraderHistoryOrders> getHistoryOrdersByTimeRange(IsoTime startTime, IsoTime endTime, int offset, int limit)
startTime - start of time range, inclusiveendTime - end of time range, exclusiveoffset - pagination offsetlimit - pagination limitpublic CompletableFuture<MetatraderDeals> getDealsByTicket(String ticket)
ticket - ticket number (deal id for MT5 or order id for MT4)public CompletableFuture<MetatraderDeals> getDealsByPosition(String positionId)
positionId - position idpublic CompletableFuture<MetatraderDeals> getDealsByTimeRange(IsoTime startTime, IsoTime endTime, int offset, int limit)
startTime - start of time range, inclusiveendTime - end of time range, exclusiveoffset - pagination offsetlimit - pagination limitpublic CompletableFuture<Void> removeHistory()
public CompletableFuture<MetatraderTradeResponse> createMarketBuyOrder(String symbol, double volume, Double stopLoss, Double takeProfit, MarketTradeOptions options)
symbol - symbol to tradevolume - order volumestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> createMarketSellOrder(String symbol, double volume, Double stopLoss, Double takeProfit, MarketTradeOptions options)
symbol - symbol to tradevolume - order volumestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> createLimitBuyOrder(String symbol, double volume, double openPrice, Double stopLoss, Double takeProfit, PendingTradeOptions options)
symbol - symbol to tradevolume - order volumeopenPrice - order limit pricestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> createLimitSellOrder(String symbol, double volume, double openPrice, Double stopLoss, Double takeProfit, PendingTradeOptions options)
symbol - symbol to tradevolume - order volumeopenPrice - order limit pricestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> createStopBuyOrder(String symbol, double volume, double openPrice, Double stopLoss, Double takeProfit, PendingTradeOptions options)
symbol - symbol to tradevolume - order volumeopenPrice - order stop pricestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> createStopSellOrder(String symbol, double volume, double openPrice, Double stopLoss, Double takeProfit, PendingTradeOptions options)
symbol - symbol to tradevolume - order volumeopenPrice - order stop pricestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nulloptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> modifyPosition(String positionId, Double stopLoss, Double takeProfit)
positionId - position id to modifystopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> closePositionPartially(String positionId, double volume, MarketTradeOptions options)
positionId - position id to modifyvolume - volume to closeoptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> closePosition(String positionId, MarketTradeOptions options)
positionId - position id to modifyoptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> closePositionsBySymbol(String symbol, MarketTradeOptions options)
symbol - symbol to tradeoptions - optional trade options or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> modifyOrder(String orderId, double openPrice, double stopLoss, double takeProfit)
orderId - order id (ticket number)openPrice - order stop pricestopLoss - optional stop loss price or nulltakeProfit - optional take profit price or nullTradeException,
check error properties for error code detailspublic CompletableFuture<MetatraderTradeResponse> cancelOrder(String orderId)
orderId - order id (ticket number)TradeException,
check error properties for error code detailspublic CompletableFuture<Void> reconnect()
public CompletableFuture<Void> synchronize()
public CompletableFuture<Void> initialize()
public CompletableFuture<Void> subscribe()
public CompletableFuture<Void> subscribeToMarketData(String symbol)
symbol - symbol (e.g. currency pair or an index)public CompletableFuture<MetatraderSymbolSpecification> getSymbolSpecification(String symbol)
symbol - symbol to retrieve specification forpublic CompletableFuture<MetatraderSymbolPrice> getSymbolPrice(String symbol)
symbol - symbol to retrieve price forpublic TerminalState getTerminalState()
public HistoryStorage getHistoryStorage()
public void addSynchronizationListener(SynchronizationListener listener)
listener - synchronization listener to addpublic void removeSynchronizationListener(SynchronizationListener listener)
listener - synchronization listener to removepublic CompletableFuture<Void> onConnected()
SynchronizationListeneronConnected in class SynchronizationListenerpublic CompletableFuture<Void> onDisconnected()
SynchronizationListeneronDisconnected in class SynchronizationListenerpublic CompletableFuture<Void> onDealSynchronizationFinished(String synchronizationId)
SynchronizationListeneronDealSynchronizationFinished in class SynchronizationListenersynchronizationId - synchronization request idpublic CompletableFuture<Void> onOrderSynchronizationFinished(String synchronizationId)
SynchronizationListeneronOrderSynchronizationFinished in class SynchronizationListenersynchronizationId - synchronization request idpublic CompletableFuture<Void> onReconnected()
ReconnectListeneronReconnected in interface ReconnectListenerpublic CompletableFuture<Boolean> isSynchronized(String synchronizationId)
synchronizationId - optional synchronization request id, last synchronization
request id will be used by defaultpublic CompletableFuture<Void> waitSynchronized()
public CompletableFuture<Void> waitSynchronized(String synchronizationId)
synchronizationId - optional synchronization id, last synchronization request id will be used by defaultpublic CompletableFuture<Void> waitSynchronized(String synchronizationId, Integer timeoutInSeconds, Integer intervalInMilliseconds)
synchronizationId - optional synchronization id, last synchronization request id will be used by defaulttimeoutInSeconds - optional wait timeout in seconds, default is 5mintervalInMilliseconds - optional interval between account reloads while waiting for a change, default is 1spublic void close()
Copyright © 2020. All rights reserved.