Skip to content

Javascript

Important

All these source codes are only examples used for testing. We don't provide any guarantee or responsibility about it. Use these examples at your own risk.

Generate source code instantly

Select your desired programming language via our web interface to automatically retrieve code:

MTsocketAPI source code

Example 1: Get EURUSD price

const url = 'http://127.0.0.1:81/v1/quote?symbol=EURUSD';
const options = {method: 'GET', headers: {Accept: 'application/json'}};

try {
    const response = await fetch(url, options);
    const data = await response.json();
    console.log(data);
} catch (error) {
    console.error(error);
}

Example 2: Send buy order

const url = 'http://127.0.0.1:81/v1/order?symbol=EURUSD&volume=0.01&type=ORDER_TYPE_BUY';
const options = {
    method: 'POST',
    headers: {'Content-Type': 'application/json', Accept: 'application/json'},
    body: undefined
};

try {
    const response = await fetch(url, options);
    const data = await response.json();
    console.log(data);
} catch (error) {
    console.error(error);
}

Example 3: Stream EURUSD price using websockets

<!DOCTYPE html>
<html>
<body>
  <div id="output"></div>

  <script>
    // WebSocket configuration
    const wsUrl = "ws://127.0.0.1:81";
    const output = document.getElementById("output");

    // Create WebSocket connection
    const ws = new WebSocket(wsUrl);

    // Event handlers
    ws.onopen = () => {
      console.log("Connected to WebSocket");
      output.innerHTML += "Connected! Waiting for data...<br>";
    };

    ws.onmessage = (event) => {
      console.log("Received data:", event.data);
      output.innerHTML += `Message: ${event.data}<br>`;
    };

    ws.onerror = (error) => {
      console.error("WebSocket error:", error);
      output.innerHTML += "Connection error!<br>";
    };

    ws.onclose = () => {
      console.log("WebSocket connection closed");
      output.innerHTML += "Connection closed<br>";
    };

    // Close connection on window unload
    window.addEventListener("beforeunload", () => {
      ws.close();
    });
  </script>
</body>
</html>