C++
Warning
Please note that all source codes provided are for testing purposes only. No guarantee or responsibility is provided for their use. Use of these examples is at your own risk.
Examples
Example 1: Get actual EURUSD price from MT5 with C++
quote.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/*
*
*
* gcc -o quote quote.c (Compile)
* ./quote 127.0.0.1 71 (Run)
*
*/
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char msg[]="{\"MSG\":\"QUOTE\",\"SYMBOL\":\"EURUSD\"}\r\n";
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
return 0;
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket\r\n");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
printf("ERROR connecting\r\n");
return 0;
}
n = write(sockfd, msg, strlen(msg));
if (n < 0)
printf("ERROR writing to socket\r\n");
bzero(buffer,256);
n = read(sockfd, buffer, 255);
if (n < 0)
printf("ERROR reading from socket\r\n");
printf("%s\n", buffer);
close(sockfd);
return 0;
}
Reply
./quote 127.0.0.1 71
{"MSG":"QUOTE","SYMBOL":"EURUSD","ASK":0.99914,"BID":0.99911,"FLAGS":6,"TIME":"2022.09.19 14:44:09.980","VOLUME":0,"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
Example 2: Send Order to MT5 with C++
sendorder.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/*
*
*
* gcc -o sendorder sendorder.c (Compile)
* ./sendorder 127.0.0.1 71 (Run)
*
*/
int main(int argc, char *argv[])
{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char msg[]="{\"MSG\":\"ORDER_SEND\",\"SYMBOL\":\"EURUSD\",\"VOLUME\":0.03,\"TYPE\":\"ORDER_TYPE_BUY\"}\r\n";
char buffer[512];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
return 0;
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
printf("ERROR opening socket\r\n");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
{
printf("ERROR connecting\r\n");
return 0;
}
n = write(sockfd, msg, strlen(msg));
if (n < 0)
printf("ERROR writing to socket\r\n");
bzero(buffer,512);
n = read(sockfd, buffer, 511);
if (n < 0)
printf("ERROR reading from socket\r\n");
printf("%s\n", buffer);
close(sockfd);
return 0;
}
Reply
./sendorder 127.0.0.1 71
{"MSG":"ORDER_SEND","RETCODE":10009,"DEAL":2015119247,"ORDER":2020570871,"VOLUME":0.10,"PRICE":1.064860,"BID":1.064830,"ASK":1.064860,"REQUEST_ID":3136973155,"RETCODE_EXTERNAL":0,"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
Example 3: Stream actual EURUSD price from MT5 with C++
streamprices.c
// Example 3 - Stream actual EURUSD price from MT5
//
// PREREQUISITE: Install MTsocketAPI in your Metatrader 5 using the following link https://www.mtsocketapi.com/doc5/installation.html
//
// WARNING: All these source codes are only examples for testing purposes.
// WARNING: We don’t provide any guarantee or responsibility about it.
// WARNING: Use these examples at your own risk.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/*
*
*
* gcc -o trackprices trackprices.c (Compile)
* ./trackprices 127.0.0.1 (Run)
*
*/
int main(int argc, char *argv[])
{
int sockfd_cmd, portno_cmd, n;
int sockfd_data, portno_data;
struct sockaddr_in serv_addr_cmd;
struct sockaddr_in serv_addr_data;
struct hostent *server_cmd;
struct hostent *server_data;
char msg[]="{\"MSG\":\"TRACK_PRICES\",\"SYMBOLS\":[\"EURUSD\"],\"TIMEFRAME\":\"PERIOD_M1\"}\r\n";
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname cmdPort dataPort\n", argv[0]);
return 0;
}
portno_cmd = atoi(argv[2]);
portno_data = atoi(argv[3]);
sockfd_cmd = socket(AF_INET, SOCK_STREAM, 0);
sockfd_data = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd_cmd < 0)
printf("ERROR opening cmd socket\r\n");
if (sockfd_data < 0)
printf("ERROR opening data socket\r\n");
server_cmd = gethostbyname(argv[1]);
server_data = gethostbyname(argv[1]);
if (server_cmd == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr_cmd, sizeof(serv_addr_cmd));
bzero((char *) &serv_addr_data, sizeof(serv_addr_data));
serv_addr_cmd.sin_family = AF_INET;
serv_addr_data.sin_family = AF_INET;
bcopy((char *)server_cmd->h_addr,
(char *)&serv_addr_cmd.sin_addr.s_addr,
server_cmd->h_length);
bcopy((char *)server_data->h_addr,
(char *)&serv_addr_data.sin_addr.s_addr,
server_data->h_length);
serv_addr_cmd.sin_port = htons(portno_cmd);
serv_addr_data.sin_port = htons(portno_data);
if (connect(sockfd_cmd, (struct sockaddr *) &serv_addr_cmd, sizeof(serv_addr_cmd)) < 0)
{
printf("ERROR connecting to cmd port\r\n");
return 0;
}
if (connect(sockfd_data, (struct sockaddr *) &serv_addr_data, sizeof(serv_addr_data)) < 0)
{
printf("ERROR connecting to data port\r\n");
return 0;
}
n = write(sockfd_cmd, msg, strlen(msg));
if (n < 0)
printf("ERROR writing to socket\r\n");
bzero(buffer,256);
n = read(sockfd_cmd, buffer, 255);
if (n < 0)
printf("ERROR reading from socket\r\n");
printf("%s\n", buffer);
bzero(buffer,256);
while(1) {
n = read(sockfd_data, buffer, 255);
printf("%s", buffer);
}
close(sockfd_cmd);
close(sockfd_data);
return 0;
}
Reply
./127.0.0.1 71 72
{"MSG":"TRACK_PRICES","SUCCESS":["EURUSD"],"ERROR_ID":0,"ERROR_DESCRIPTION":"The operation completed successfully"}
{"TIME":"2022.04.19 10:36:18","SYMBOL":"EURUSD","ASK":1.08011,"BID":1.08009,"VOLUME":29274}
{"TIME":"2022.04.19 10:36:19","SYMBOL":"EURUSD","ASK":1.08012,"BID":1.08009,"VOLUME":29276}
{"TIME":"2022.04.19 10:36:20","SYMBOL":"EURUSD","ASK":1.08011,"BID":1.08009,"VOLUME":29276}
{"TIME":"2022.04.19 10:36:21","SYMBOL":"EURUSD","ASK":1.08009,"BID":1.08008,"VOLUME":29278}
{"TIME":"2022.04.19 10:36:22","SYMBOL":"EURUSD","ASK":1.08009,"BID":1.08007,"VOLUME":29279}
{"TIME":"2022.04.19 10:36:28","SYMBOL":"EURUSD","ASK":1.08009,"BID":1.08006,"VOLUME":29280}
Advanced Examples
Example 1: Get actual EURUSD price from MT5 (using JSON library) with C++
In progress
Example 2: Stream OHLC data from MT5 (using JSON library) with C++
In progress
Example 3: Export MT5 Trade History from MT5 to a CSV file with C++
In progress
Info
Have you found any bug or error? Please notify us.