Discussing the article: "Market Simulation (Part 12): Sockets (VI)"

 

Check out the new article: Market Simulation (Part 12): Sockets (VI).

In this article, we will look at how to solve certain problems and issues that arise when using Python code within other programs. More specifically, we will demonstrate a common issue encountered when using Excel in conjunction with MetaTrader 5, although we will be using Python to facilitate this interaction. However, this implementation has a minor drawback. It does not occur in all cases, but only in certain specific situations. When it does happen, it is necessary to understand the cause. In today’s article, we will begin explaining how to resolve this issue.

In the previous article, "Market Modeling (Part 11): Sockets (V)", we explained how to create a Python application for use in Excel. The purpose of this application was to demonstrate how to build an echo server in Python. Its distinguishing feature was that data related to connection and disconnection events were displayed directly in Excel.

In truth, this server is not particularly useful for us, mainly because it allows only a single connection—and servers designed to handle only one connection are of limited practical value. However, I do not want you to dwell too much on this detail. The intent was to show how a script written in Python can operate transparently within Excel. But for our actual needs, the server must be somewhat more advanced. To achieve this, we will need to implement several additional features.

The goal here is not to create a production-ready application. As previously mentioned, sockets are a highly complex topic that requires a significant amount of time to study and explore. Do not expect to be able to develop something truly robust and polished in just one day. When dealing with sockets, it is necessary to delve into many details—some of them are simpler, while others are considerably more intricate.

In this article, we will not be working directly with Excel or MQL5. However, in the case of MQL5, we will be able to use it. More precisely, we will make use of what has been developed earlier in this same series. For a complete understanding of the material, it will also be helpful to review what has already been implemented in MQL5.


Author: Daniel Jose

 
I would like to express my deep gratitude for the knowledge and wisdom you share here.
 

Thanks for the information... how can sockets help to connect two MT5 terminals from different brokers? on one forex on the other moex - paired trading from two terminals can be realised through sockets?

I am digging this topic myself... sorry in advance if my question is a little off topic.... I am still reading articles and looking for solutions for two MT5 trading in conjunction and getting quotes of symbols from different exchanges in essence and trading in conjunction after analysing data on quotes of 2-3-4-5 symbols.....

I'll be socketing:

  • Inter-terminal exchange: data goes directly between MT5 A and MT5 B.

  • Events: OnSocketEvent() triggers instantly when data is received.

  • Data flexibility: JSON, binary structures, arrays can be transferred.

  • Speed: latency is an order of magnitude lower than variable polling.

  • Reliability: there are mechanisms for resending and confirmation.

In this context I am planning to make an advanced Python server with spread calculation, ACK/NACK, storing position states and web-interface for monitoring;

// Terminal A
string msg = "{\"cmd\":\"OPEN\", \"symbol\":\"USDRUBF\", \"volume\":1.0}";
SocketSend(socket, msg);

// Terminal B
string cmd;
SocketReceive(socket, cmd);  // Got the full structure of the signal
Посмотрите, вроде работает даже на МТ5
Посмотрите, вроде работает даже на МТ5
  • 2025.10.29
  • www.mql5.com
Вот индикаторы эквити есть - мое прочтение это на МТ5. почему спрашиваю - поставил зеркальные в 4 - прокатило - GBPUSD и EURGBP - индикатор в мт5 исчез Реверс. А по индикаторам с МТ4 там в ветке они описаны как ими пользоваться
 
Roman Shiredchenko paired trading from two terminals can be realised through sockets?

I am digging this topic myself... sorry in advance if my question is a little off topic.... I am still reading articles and looking for solutions for two MT5 trading in a socket and getting quotes of symbols from different exchanges in essence and trading in a socket after analysing data on quotes of 2-3-4-5 symbols....

I will be socketing:

  • Inter-terminal exchange : data goes directly between MT5 A and MT5 B.

  • Events : OnSocketEvent() is triggered instantly when data is received.

  • Data flexibility : JSON, binary structures, arrays can be transmitted.

  • Speed : latency is an order of magnitude lower than variable polling.

  • Reliability : there are resend and acknowledgement mechanisms.

In this context I am planning to make: an advanced Python server with spread calculation, ACK/NACK, storing position states and web-interface for monitoring;

Your question is relevant and interesting. But I think you are making hasty conclusions. Let me clarify: although MQL5 implements sockets, as I explain in my articles, it does not allow you to create a server. Only a client. Therefore, a lot of what you are planning to do is impossible. IT IS IMPOSSIBLE to be implemented in MQL5. You will need external code. In your case, you mention the use of Python, which is already a solution in itself.

In fact, much of what you need can be implemented in Python. However, there is a slight problem with what you are trying to do: interacting directly with the broker. Why am I saying this? For security reasons, brokers generally do not accept access via sockets. There is a special protocol for such interaction, specifically designed to prevent interruptions in the broker's internal mechanisms. But it's not impossible to try. They can tell you the communication protocol so you can access it when it suits you. But don't think this will be easy unless you have a VERY close friend at the brokerage who can provide you with the information you need.

Another thing that caught my attention is the interaction and information exchange between two different instances of MetaTrader 5. In my humble opinion, what you are trying to do is not a good idea. You do not understand some concepts of parallel programming and related problems. If you don't mind, try to study the "producer-consumer" task. It will help you understand the level of complexity and pitfalls you may encounter when transferring information between different MetaTrader 5 instances for trading purposes.

Anyway, good luck with your project 🙂👍

 
Daniel Jose #:

Your question is relevant and interesting. But I think you are making hasty conclusions. Let me clarify: although MQL5 implements sockets, as I explain in my articles, it does not allow you to create a server. Only a client. Therefore, a lot of what you are planning to do is impossible. IT IS IMPOSSIBLE to be implemented in MQL5. You will need external code. In your case, you mention the use of Python, which is already a solution in itself.

In fact, much of what you need can be implemented in Python. However, there is a slight problem with what you are trying to do: interacting directly with the broker. Why am I saying this? For security reasons, brokers generally do not accept access via sockets. There is a special protocol for such interaction, specifically designed to prevent interruptions in the broker's internal mechanisms. But it's not impossible to try. They can tell you the communication protocol so you can access it when it suits you. But don't think this will be easy unless you have a VERY close friend at the brokerage who can provide you with the information you need.

Another thing that caught my attention is the interaction and information exchange between two different instances of MetaTrader 5. In my humble opinion, what you are trying to do is not a good idea. You do not understand some concepts of parallel programming and related problems. If you don't mind, try to study the "producer-consumer" task. This will help you understand the level of complexity and pitfalls you may encounter when transferring information between different MetaTrader 5 instances for trading purposes.

Anyway, good luck with your project 🙂👍

THANK you very much for the feedback you provided..... no friends in brokerages!!! ) have MT5 terminals in two different brokerages! need to make them friends..... ) this is a project for next year!!!

here - trying... thanks for the articles - I'm reading them and studying the content!!! if the speeds will allow - then maybe I'll do it directly through the files... access - like earlier hard discs connected in bios two pieces: one master one slave... )

so here... on a powerful computer two MT5 terminals one master (the main) - the other slave (the second), on one the stock exchange - on the other MT5 forex! maybe realisation through files ) reading - writing will do ... but I would like to implement a faster easy in terms of reading - receiving data variant.... by type of global variables of client terminal (I don't use files - long time), but global variables of client terminal are visible only in this terminal..... you need to connect external processes... maybe make tables in memory... like dll libraries of connection I will realise!!!

Have a nice day!