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 (90 loc) • 4.16 kB
HTML
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Streaming example</title>
</head>
<body>
<div style="padding: 20px; display: flex; flex-direction: column; width: 200px">
<label for="token-input" style="margin-bottom: 5px">Terminal access token</label>
<input id="token-input" style="margin-bottom: 10px" type="text">
<label for="account-id-input" style="margin-bottom: 5px">Account ID</label>
<input id="account-id-input" type="text" style="margin-bottom: 10px">
<button onclick="onSubmit()">Connect</button>
</div>
<div id='info' style="margin-top: 20px"></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 onSubmit = () => {
const token = document.getElementById('token-input').value;
const accountId = document.getElementById('account-id-input').value;
if (token && accountId) {
const api = new MetaApi.default(token);
return api.metatraderAccountApi.getAccount(accountId)
.then(account => {
console.log(account);
log('Waiting for API server to connect to broker (may take couple of minutes)');
return account.waitConnected()
.then(() => {
let connection = account.getStreamingConnection();
return (connection.connect(), connection);
})
.then(connection => {
log('Waiting for SDK to synchronize to terminal state (may take some time depending on your history size)');
return connection.waitSynchronized()
.then(async () => {
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);
}
return;
})
});
})
.catch(error => console.error(error))
}
}
</script>
</html>