UNPKG

metaapi.cloud-sdk

Version:

SDK for MetaApi, a professional cloud forex API which includes MetaTrader REST API and MetaTrader websocket API. Supports both MetaTrader 5 (MT5) and MetaTrader 4 (MT4). CopyFactory copy trading API included. (https://metaapi.cloud)

95 lines (80 loc) 3.15 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Streaming example</title> </head> <body> <div id='info'></div> </body> <script src="https://unpkg.com/metaapi.cloud-sdk"></script> <script> const infoElement = document.getElementById('info'); function log() { if(typeof arguments[1] === 'object'){ arguments[1] = JSON.stringify(arguments[1]); } const el = document.createElement('div'); el.innerHTML = [].join.call(arguments, ' '); infoElement.appendChild(el); console.log(...arguments); } const token = '<put in your token here>'; const accountId = '<put in your account id here>'; const api = new MetaApi.default(token); async function testMetaApiSynchronization() { try { const account = await api.metatraderAccountApi.getAccount(accountId); log('Waiting for API server to connect to broker (may take couple of minutes)'); await account.waitConnected(); // connect to MetaApi API let connection = account.getStreamingConnection(); await connection.connect(); // wait until terminal state synchronized to the local state log('Waiting for SDK to synchronize to terminal state (may take some time depending on your history size)'); await connection.waitSynchronized(); // access local copy of terminal state log('Testing terminal state access'); let terminalState = connection.terminalState; log('connected:', terminalState.connected); log('connected to broker:', terminalState.connectedToBroker); log('account information:', JSON.stringify(terminalState.accountInformation)); log('positions:', JSON.stringify(terminalState.positions)); log('orders:', JSON.stringify(terminalState.orders)); log('specifications:', JSON.stringify(terminalState.specifications)); log('EURUSD specification:', JSON.stringify(terminalState.specification('EURUSD'))); log('EURUSD price:', JSON.stringify(terminalState.price('EURUSD'))); let historyStorage = connection.historyStorage; log('deals:', JSON.stringify(historyStorage.deals.slice(-5))); log('history orders:', JSON.stringify(historyStorage.historyOrders.slice(-5))); // calculate margin required for trade log('margin required for trade', JSON.stringify(await connection.calculateMargin({ symbol: 'GBPUSD', type: 'ORDER_TYPE_BUY', volume: 0.1, openPrice: 1.1 }))); // trade log('Submitting pending order'); try { let result = await connection.createLimitBuyOrder('GBPUSD', 0.07, 1.0, 0.9, 2.0, { comment: 'comm', clientId: 'TE_GBPUSD_7hyINWqAlE', expiration: { type: 'ORDER_TIME_SPECIFIED', time: new Date(Date.now() + 24 * 60 * 60 * 1000) } }); log('Trade successful, result code is ' + result.stringCode); } catch (err) { log('Trade failed with result code ' + err.stringCode); } } catch (err) { log(err); } return; } testMetaApiSynchronization(); </script> </html>