Skip to content

C++

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

main.c
#include <stdio.h>
#include <curl/curl.h>

int main() {
        CURL *hnd = curl_easy_init();

        curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "GET");
        curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.8.177:81/v1/quote?symbol=EURUSD");

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Accept: application/json");
        curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

        CURLcode ret = curl_easy_perform(hnd);
}
  1. Install dependencies:

    sudo apt update
    sudo apt install build-essential libcurl4-openssl-dev 
    
  2. Compile:

    gcc -o getquote main.c -lcurl 
    
  3. Run:

    ./getquote
    

Example 2: Send buy order

main.c
#include <stdio.h>
#include <curl/curl.h>

int main() {
        CURL *hnd = curl_easy_init();

        curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(hnd, CURLOPT_URL, "http://192.168.8.177:81/v1/order?symbol=EURUSD&volume=0.01&type=ORDER_TYPE_BUY");

        struct curl_slist *headers = NULL;
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "Accept: application/json");
        curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

        CURLcode ret = curl_easy_perform(hnd);
}
  1. Install dependencies:

    sudo apt update
    sudo apt install build-essential libcurl4-openssl-dev 
    
  2. Compile:

    gcc -o sendorder main.c -lcurl 
    
  3. Run:

    ./sendorder
    

Example 3: Stream EURUSD price using websockets

main.cpp
#include <stdio.h>
#include <curl/curl.h>
#include <boost/asio.hpp>
#include <boost/beast.hpp>
#include <iostream>

namespace asio = boost::asio;
namespace beast = boost::beast;
using tcp = asio::ip::tcp;
using websocket = beast::websocket::stream<tcp::socket>;

// Función para la parte WebSocket
void websocket_part() {
    try {
        asio::io_context ioc;
        tcp::resolver resolver(ioc);
        auto const results = resolver.resolve("127.0.0.1", "81");

        websocket ws(ioc);
        asio::connect(ws.next_layer(), results.begin(), results.end());

        ws.handshake("127.0.0.1:81", "/");
        std::cout << "Connected! Waiting for messages...\n";

        while(true) {
            beast::flat_buffer buffer;
            ws.read(buffer);
            std::cout << "Received: " << beast::make_printable(buffer.data()) << "\n";
        }
    }
    catch(const std::exception& e) {
        std::cerr << "WebSocket Error: " << e.what() << "\n";
    }
}

// Función para la parte HTTP POST con libcurl
void http_post() {
    CURL *hnd = curl_easy_init();
    if(!hnd) {
        fprintf(stderr, "Error inicializando cURL\n");
        return;
    }

    curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
    curl_easy_setopt(hnd, CURLOPT_URL, "http://127.0.0.1:81/v1/track/prices?symbols=EURUSD");

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Content-Type: application/json");
    headers = curl_slist_append(headers, "Accept: application/json");
    curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, headers);

    CURLcode ret = curl_easy_perform(hnd);

    // Limpieza
    curl_easy_cleanup(hnd);
    curl_slist_free_all(headers);
}

int main() {
    curl_global_init(CURL_GLOBAL_ALL);  // Inicialización global de libcurl

    http_post();        // Primero hace el POST
    websocket_part();   // Luego conecta el WebSocket

    curl_global_cleanup();
    return 0;
}
  1. Install dependencies:

    sudo apt update
    sudo apt install libcurl4-openssl-dev libboost-all-dev
    
  2. Compile:

    g++ -o streamprices main.cpp -lcurl -lboost_system -pthread -std=c++17
    
  3. Run:

    ./streamprices